import java.awt.Color; import java.awt.Graphics; import java.awt.Image; // Used to create an image buffer import java.awt.event.ActionEvent; // Needed by the Timer import java.awt.event.ActionListener; // Needed by the Timer import java.awt.Graphics2D; // Used for anti-aliasing import java.awt.RenderingHints; // Used for anti-aliasing import javax.swing.JFrame; import javax.swing.Timer; public class GraphicsMoveObjectsWithTimerAndJFrame extends JFrame implements ActionListener { Timer timer; Image image; Graphics graphics; int width = 400, height = 250; int dotSize = 25, dotBlueX = 150, dotBlueY = 70, dotMagentaX = 230, dotMagentaY = 190; int directionBlue = 1, speedBlue = 2, directionMagenta = 1, speedMagenta = 4; public static void main(String[] args) { // Create an instance of the 'GraphicsMoveObjectsWithTimerAndJFrame' // class and set the frame background color GraphicsMoveObjectsWithTimerAndJFrame frame = new GraphicsMoveObjectsWithTimerAndJFrame(); frame.setBackground(Color.WHITE); } // Constructor public GraphicsMoveObjectsWithTimerAndJFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(width, height); setTitle("Object Movement with a Timer on a JFrame"); setLocationRelativeTo(null); setResizable(false); setVisible(true); // Set and start a new Swing timer to fire every 10/1000 milliseconds // (after an initial delay of 500/1000 milliseconds); this timer, // along with the distance (number of pixels) that the blue dots // move with each cycle, controls how fast the dots move on the frame timer = new Timer(10, this); timer.setInitialDelay(500); timer.start(); } // This method is called automatically by the timer public void actionPerformed(ActionEvent e) { // Update the position/direction of the blue dot dotBlueX += speedBlue * directionBlue; if (dotBlueX < 16 || dotBlueX > width - 18) directionBlue = -directionBlue; // Update the position/direction of the magenta dot dotMagentaX += speedMagenta * directionMagenta; if (dotMagentaX < 16 || dotMagentaX > width - 18) directionMagenta = -directionMagenta; repaint(); } public void paint(Graphics g) { // Prepare to create the image on a separate 'Image' instance; this // should prevent flickering by using a "buffer" on which the frame // image will first be drawn before it is displayed to the user image = createImage(width, height); graphics = image.getGraphics(); // This line causes graphics and text to be rendered with anti-aliasing // turned on, making the overall display look smoother and cleaner ((Graphics2D) graphics).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Draw/Redraw the blue dot graphics.setColor(Color.BLUE); graphics.fillOval(dotBlueX - dotSize / 2, dotBlueY - dotSize / 2, dotSize, dotSize); // Draw/Redraw the magenta dot graphics.setColor(Color.MAGENTA); graphics.fillOval(dotMagentaX - dotSize / 2, dotMagentaY - dotSize / 2, dotSize, dotSize); // Display (Draw) the buffered image on the frame g.drawImage(image, 0, 0, null); } }