Reputation: 31
So I can get the rectangle to render to the screen but when I run the timer it just paints a growing rectangle across the screen rather than moving it as the rectangle it started as. Any help would be great, thank you.
Game Class
public class Game implements ActionListener{
static Game game;
Render render;
int x;
int y;
int velx = 2;
Game(){
Timer timer = new Timer(10, this);
render = new Render();
JFrame frame = new JFrame("My Game");
frame.setSize(500, 500);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(render);
timer.start();
}
public void render(Graphics g){
g.setColor(Color.red);
g.fillRect(x, y, 50, 50);
}
public static void main(String [] args){
game = new Game();
}
@Override
public void actionPerformed(ActionEvent e) {
int velx = 2;
x = x + velx;
render.repaint();
}
}
Render Class
public class Render extends JPanel {
public void paintComponent(Graphics g){
super.paintComponents(g);
Game.game.render((Graphics)g);
}
}
Upvotes: 0
Views: 30
Reputation: 31
The Problem was when I did this.
super.paintComponents(g);
I changed it to this and it worked.
super.paintComponent(g);
Upvotes: 1