First look at the effect chart:
directly on the code.
Adjust the button and draw a few circles on the canvas, then realize the 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 ("Bouncing Ball");
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 center location 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[];//store all the 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's change 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 the wall, change the direction of the ball if (balls[i].x<=0| |
Balls[i].x>=this.getwidth () -24) {balls[i].left =!balls[i].left;//Toggle Direction}//Let the coordinates of each ball change---(Y-coordinate) Balls[i].y = balls[i].up?
balls[i].y-10:balls[i].y+10; When the ball hits the wall, change the direction of the ball if (balls[i].y<=0| | Balls[i].y>=this.getheight () -22) {balls[i].up =!balls[i].up;//Toggle Direction} g.filloval (balls[i].x, Bal
LS[I].Y, 20, 20);
@Override public void actionperformed (ActionEvent e) {//system.out.println ("AAA");
Repaint (),//Refresh 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 the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.