So I'm trying to move a rectangle... but it leaves footprints...repaint(); doesn't seem to work in actionPerformed
Thanks!
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class myFrame extends JPanel implements KeyListener, ActionListener { private static final long serialVersionUID = 1L; Timer t = new Timer(5, this); double rectX = 15; double rectY = 15; int rectWidth = 80; int rectHeight = 20; double rectVeloX = 0.0; double rectVeloY = 0.0; double ballX = 60; double ballY = 60; int ballWH = 20; public myFrame() { t.start(); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); } public void paintComponent (Graphics draw2D){ //PAD super.paintComponents(draw2D); Graphics2D draw = (Graphics2D) draw2D; draw.setColor(Color.red); draw.fill(new Rectangle2D.Double(rectX,rectY, rectWidth, rectHeight)); draw.getClipBounds(); //BALL /*draw.setColor(Color.blue); draw.fill(new Ellipse2D.Double(ballX,ballY,ballWH,ballWH));*/ } public void actionPerformed (ActionEvent e) { repaint(); rectX += rectVeloX; rectY += rectVeloY; } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_RIGHT ) { rectVeloX = 1.5; rectVeloY = 0; } if (key == KeyEvent.VK_LEFT ) { rectVeloX = -1.5; rectVeloY = 0; //Left arrow key code } } public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_RIGHT) { rectVeloX = 0; rectVeloY = 0; } if (key == KeyEvent.VK_LEFT) { rectVeloX = 0; rectVeloY = 0; } } public void keyTyped(KeyEvent e) {} } import javax.swing.JFrame; public class arkanoidGame { public static void main(String[] args) { JFrame frame = new JFrame(); myFrame s = new myFrame(); frame.add(s); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800,600); } }