Problem with JFrame Painting

Do these. Circle paint at 100,100 then moves to 50,50 for no reason. Then when I move it and it gets to either point of 0 or 250, it squishes.

[code]import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* w=87;a=65;s=83;d=68
*/
public class Screen extends JFrame implements ActionListener,WindowListener,KeyListener
{
private Container c = getContentPane();
private Timer t1;
private int keyCode;

private Player player;
private MovePlayer mp;

public Screen()
{
t1 = new Timer(10,this);
keyCode = 0;

mp = new MovePlayer();
player = new Player();

c.add(player);

addWindowListener(this);
addKeyListener(this);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource() == t1) {
player.movePlayer();
}
}

public void windowDeactivated(WindowEvent e) { }
public void windowActivated(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowClosed(WindowEvent e) { }
public void windowClosing(WindowEvent e)
{
t1.stop();
}
public void windowOpened(WindowEvent e)
{
t1.start();
}

public void keyReleased(KeyEvent e)
{
keyCode = 0;
}
public void keyPressed(KeyEvent e)
{
keyCode = e.getKeyCode();
}
public void keyTyped(KeyEvent e) {

}

public static void main(String[] args)
{
Screen screen = new Screen();
screen.setVisible(true);
screen.setSize(500,500);
}
}[/code]

[code]import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
/**
* Write a description of class Player here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player extends JPanel implements KeyListener
{
private int x;
private int y;
private int keyCode;

public Player()
{
x = 100;
y = 100;

addKeyListener(this);
}
public int getX() { return x; }
public int getY() { return y; }

public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawOval(x-20,y-20,20,20);
}
public void changeX(int change)
{
x += change;
}
public void changeY(int change)
{
y += change;
}
public void keyReleased(KeyEvent e)
{
keyCode = 0;
}
public void keyPressed(KeyEvent e)
{
keyCode = e.getKeyCode();
}
public void keyTyped(KeyEvent e) {

}

public void movePlayer()
{
if(keyCode == 87)
y--;

else if(keyCode == 65)
x--;

else if(keyCode == 83)
y++;

else if(keyCode == 68)
x++;
repaint();
}
}[/code]

Any help would be appreciated

Comments

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion