java 用Swing、ActionListener类画圆遇边框弹回小程序升级(增加了移速控制按钮)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class SimpleCarton2{
JFrame frame;
MyPanel figurePanel; //the container displays the move of figure
JPanel controlPanel; //the container which contains the control buttons
JButton startOrStopButton;
int x = 50; //the start x coordinate
int y = 50; //the start y coordinate
static int step = 1; //the figure moves one pixel for each time
boolean startOrStop = false; // if ture,figure moves;if false,figure stops
boolean xMinus = false; //if true,minus
boolean xAdd = true;//if true,add;initialize with true,the oval can move
boolean yMinus = false; //if true,minus
boolean yAdd = true;//if true,add;initialize with true,the oval can move
int w = 50;//the width of oval
int h = 50;//the height of oval
public static void main(String[] args){
SimpleCarton2 sc = new SimpleCarton2();
sc.setGUI();
sc.go();
}
public void setGUI(){ //initialize the GUI
frame = new JFrame("Simple Carton2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
figurePanel = new MyPanel();
frame.getContentPane().add(BorderLayout.CENTER, figurePanel);
controlPanel = new JPanel();
controlPanel.setBackground(Color.darkGray);
startOrStopButton = new JButton("start");
JButton fastButton = new JButton("fast");
JButton slowButton = new JButton("slow");
startOrStopButton.addActionListener(new StartOrStopListener());
fastButton.addActionListener(new FastListener());
slowButton.addActionListener(new SlowListener());
controlPanel.add(startOrStopButton);
controlPanel.add(fastButton);
controlPanel.add(slowButton);
frame.getContentPane().add(BorderLayout.EAST, controlPanel);
frame.setSize(700, 500);
frame.setVisible(true);
}
public void go(){ //change the coordinate and repaint the figure
while(true){
figurePanel.repaint(); // paint the start figure and medial figure
while(startOrStop){ //if true,figure moves; if false,figure stops
changeCoordinate();
figurePanel.repaint();
try{
Thread.sleep(50); //program delay 50 milliseconds every time
}catch(Exception ex){ }
}
}
}
public void changeCoordinate(){
if(x >= (figurePanel.getWidth()-w)){ //oval moves to the right border
xMinus = true;
xAdd = false;
}
if(x <= 0){ //oval moves to the left border
xAdd = true;
xMinus = false;
}
if(xMinus == true){
x -= step;
}
if(xAdd == true){
x += step;
}
if(y >= (figurePanel.getHeight()-h)){ //oval moves to the bottom
yMinus = true;
yAdd = false;
}
if(y <= 0){ //oval moves to the top
yAdd = true;
yMinus = false;
}
if(yMinus == true){
y -= step;
}
if(yAdd == true){
y += step;
}
}
public class StartOrStopListener implements ActionListener{
public void actionPerformed(ActionEvent a){
if(startOrStopButton.getText().equals("start")){ //click start
startOrStop = true;
startOrStopButton.setText("stop");
}else if(startOrStopButton.getText().equals("stop")){ //click stop
startOrStop = false;
startOrStopButton.setText("start");
}
}
}
public class FastListener implements ActionListener{
public void actionPerformed(ActionEvent a){
step += 1;
}
}
public class SlowListener implements ActionListener{
public void actionPerformed(ActionEvent a){
if((step-1) >= 1){
step -= 1;
}else{
step=step;
}
}
}
class MyPanel extends JPanel{
public void paintComponent(Graphics g){
//before each repaint,set background to be white so as to erase the last time imprint
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
//use the new coordinate to repaint new figure
g.setColor(Color.red);
g.fillOval(x, y, w, h);
}
}
}
初始界面如下: