Let’s take a look at the renderings first:
Just upload the code.
Fine-tune the button and add the canvas to draw a few circles, and then monitor. . .
package cn.hncu.threadDemo.thread2;import java.awt.Canvas;import java.awt.Color;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JSpinner;import javax.swing.Timer;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;public class BallsJFrame extends JFrame implements ChangeListener{ private BallsCanvas ball; private JSpinner spinner; public BallsJFrame(){ super("Pineball"); this.setBounds(300, 200, 400, 300); this.setDefaultCloseOperation(EXIT_ON_CLOSE); Color colors[] = {Color.red,Color.green,Color.blue,Color.magenta,Color.cyan}; ball = new BallsCanvas(colors,100); this.getContentPane().add(ball);//Default is the CENTER position JPanel panel = new JPanel(); this.getContentPane().add(panel,"South"); panel.add(new JLabel("Delay")); spinner = new JSpinner(); spinner.setValue(100); panel.add(spinner); spinner.addChangeListener(this); this.setVisible(true); } @Override public void stateChanged(ChangeEvent e) { int value = Integer.parseInt(""+spinner.getValue()); ball.setDelay(value); } public static void main(String[] args) { new BallsJFrame(); }}class BallsCanvas extends Canvas implements ActionListener, FocusListener{ private Ball balls[];//Storage all balls private Timer timer;//javax.swing.Timer public BallsCanvas(Color colors[] ,int delay){ this.balls = new Ball[colors.length]; for(int i=0,x=40;i<colors.length;i++,x+=20){ this.balls[i] = new Ball(x,x,colors[i]); } //Let the current canvas monitor the focus event this.addFocusListener(this); timer = new Timer(delay,this); timer.start(); } public void setDelay(int delay){ timer.setDelay(delay); } @Override public void paint(Graphics g) { for(int i=0;i<this.balls.length;i++){ g.setColor(balls[i].color); //Let the coordinates of each ball---(x coordinates) balls[i].x = balls[i].left ? balls[i].x-10:balls[i].x+10; //When the ball hits a wall, change the direction of the ball if(balls[i].x<=0||balls[i].x>=this.getWidth()-24){ balls[i].left = !balls[i].left;//Switch direction} //Let the coordinates of each ball change --- (y coordinates) balls[i].y = balls[i].up ? balls[i].y-10:balls[i].y+10; //When the ball hits a wall, change the direction of the ball if(balls[i].y<=0||balls[i].y>=this.getHeight()-22){ balls[i].up = !balls[i].up;//Switch direction} g.fillOval(balls[i].x, balls[i].y, 20, 20); } } @Override public void actionPerformed(ActionEvent e) { //System.out.println("aaa"); repaint();//Refresh the canvas. Call paint(Graphics g) } @Override public void focusGained(FocusEvent e) { timer.stop(); } @Override public void focusLost(FocusEvent e) { timer.restart(); } private static class Ball{ int x,y; boolean up,left; Color color; public Ball(int x, int y, Color color) { this.x = x; this.y = y; this.color = color; up = left = false; } }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.