java 用swing、ActionListener类画的圆遇边框可以弹回的升级版3.0(添加了可以变大变小、变颜色、可以控制方向的功能)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class SimpleCarton3{
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 coordinate of x
int y = 50; //the start coordinate of y
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 xFixed = false; //if true, x is fixed; if false, x can change
boolean yMinus = false; //if true,minus
boolean yAdd = true;//if true,add;initialize with true,the oval can move
boolean yFixed = false; //if true, y is fixed; if false, y can change
int w = 50;//the width of oval
int h = 50;//the height of oval
int sizeChangeFactor = 3; //figure changes 3 for each time
Color color = Color.red; //the color of figure,initialized with red
public static void main(String[] args){
SimpleCarton3 sc = new SimpleCarton3();
sc.setGUI();
sc.go();
}
public void setGUI(){ //initialize the GUI
frame = new JFrame("Simple Carton3");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
figurePanel = new MyPanel();
frame.getContentPane().add(BorderLayout.CENTER, figurePanel);
//use GridLayout to arrange buttons' position
GridLayout grid = new GridLayout(7, 3, 1, 1);
controlPanel = new JPanel(grid);
controlPanel.setBackground(Color.darkGray);
//build buttons
startOrStopButton = new JButton("start");
JButton fastButton = new JButton("fast");
JButton slowButton = new JButton("slow");
JButton biggerButton = new JButton("bigger");
JButton smallerButton = new JButton("smaller");
JButton colorButton = new JButton("color");
JButton northwestButton = new JButton("northwest");
JButton northButton = new JButton("north");
JButton northeastButton = new JButton("northeast");
JButton westButton = new JButton("west");
JButton blankButton = new JButton(" ");
JButton eastButton = new JButton("east");
JButton southwestButton = new JButton("southwest");
JButton southButton = new JButton("south");
JButton southeastButton = new JButton("southeast");
//add listener to each button
startOrStopButton.addActionListener(new StartOrStopListener());
fastButton.addActionListener(new FastListener());
slowButton.addActionListener(new SlowListener());
biggerButton.addActionListener(new BiggerListener());
smallerButton.addActionListener(new SmallerListener());
colorButton.addActionListener(new ColorListener());
northwestButton.addActionListener(new NorthwestListener());
northButton.addActionListener(new NorthListener());
northeastButton.addActionListener(new NortheastListener());
westButton.addActionListener(new WestListener());
eastButton.addActionListener(new EastListener());
southwestButton.addActionListener(new SouthwestListener());
southButton.addActionListener(new SouthListener());
southeastButton.addActionListener(new SoutheastListener());
//add each button to controlPanel
controlPanel.add(startOrStopButton);
controlPanel.add(fastButton);
controlPanel.add(slowButton);
controlPanel.add(biggerButton);
controlPanel.add(smallerButton);
controlPanel.add(colorButton);
controlPanel.add(northwestButton);
controlPanel.add(northButton);
controlPanel.add(northeastButton);
controlPanel.add(westButton);
controlPanel.add(blankButton);
controlPanel.add(eastButton);
controlPanel.add(southwestButton);
controlPanel.add(southButton);
controlPanel.add(southeastButton);
//add controlPanel to frame, its position is at east of frame
frame.getContentPane().add(BorderLayout.EAST, controlPanel);
frame.setSize(900, 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(xFixed == true){
x = x;
}
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;
}
if(yFixed == true){
y = y;
}
}
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");
}
}
}
//step add 1 for each click, its max value is 9
public class FastListener implements ActionListener{
public void actionPerformed(ActionEvent a){
if((step + 1)<= 9){
step += 1;
}else{
step = step;
}
}
}
//step decreases 1 for each click, its min value is 1
public class SlowListener implements ActionListener{
public void actionPerformed(ActionEvent a){
if((step-1) >= 1){
step -= 1;
}else{
step=step;
}
}
}
//the width and height of oval increases the value of sizeChangeFactor for each click, its max values are 90
public class BiggerListener implements ActionListener{
public void actionPerformed(ActionEvent a){
if((w + sizeChangeFactor)<= 90 && (h + sizeChangeFactor)<= 90){
w += sizeChangeFactor;
h += sizeChangeFactor;
}else{
w = w;
h = h;
}
}
}
//the width and height of oval decrease the value of sizeChangeFactor for each click, its min values are 30
public class SmallerListener implements ActionListener{
public void actionPerformed(ActionEvent a){
if((w - sizeChangeFactor)>=30 && (h - sizeChangeFactor)>=30){
w -= sizeChangeFactor;
h -= sizeChangeFactor;
}else{
w = w;
h = h;
}
}
}
//change random color of the oval
public class ColorListener implements ActionListener{
public void actionPerformed(ActionEvent a){
int red = (int)(Math.random() * 255);
int green = (int)(Math.random() * 255);
int blue = (int)(Math.random() * 255);
color = new Color(red, green, blue);
}
}
//set xMinus and yMinus to be true, the oval can move to northwest
public class NorthwestListener implements ActionListener{
public void actionPerformed(ActionEvent a){
xMinus = true;
xAdd = false;
xFixed = false;
yMinus = true;
yAdd = false;
yFixed = false;
}
}
//set xFixed and yMinus to be true, the oval can move to north; x keeps to be fixed
public class NorthListener implements ActionListener{
public void actionPerformed(ActionEvent a){
xMinus = false;
xAdd = false;
xFixed = true;
yMinus = true;
yAdd = false;
yFixed = false;
}
}
//set xAdd and yMinus to be true, the oval can move to northeast
public class NortheastListener implements ActionListener{
public void actionPerformed(ActionEvent a){
xMinus = false;
xAdd = true;
xFixed = false;
yMinus = true;
yAdd = false;
yFixed = false;
}
}
//set xMinus and yFixed to be true, the oval can move to west; y keeps to be fixed
public class WestListener implements ActionListener{
public void actionPerformed(ActionEvent a){
xMinus = true;
xAdd = false;
xFixed = false;
yMinus = false;
yAdd = false;
yFixed = true;
}
}
//set xAdd and yFixed to be true, the oval can move to east; y keeps to be fixed
public class EastListener implements ActionListener{
public void actionPerformed(ActionEvent a){
xMinus = false;
xAdd = true;
xFixed = false;
yMinus = false;
yAdd = false;
yFixed = true;
}
}
//set xMinus and yAdd to be true, the oval can move to southwest
public class SouthwestListener implements ActionListener{
public void actionPerformed(ActionEvent a){
xMinus = true;
xAdd = false;
xFixed = false;
yMinus = false;
yAdd = true;
yFixed = false;
}
}
//set xFixed and yAdd to be true, the oval can move to south; x keeps to be fixed
public class SouthListener implements ActionListener{
public void actionPerformed(ActionEvent a){
xMinus = false;
xAdd = false;
xFixed = true;
yMinus = false;
yAdd = true;
yFixed = false;
}
}
//set xAdd and yAdd to be true, the oval can move to southeast
public class SoutheastListener implements ActionListener{
public void actionPerformed(ActionEvent a){
xMinus = false;
xAdd = true;
xFixed = false;
yMinus = false;
yAdd = true;
yFixed = false;
}
}
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);
g.fillOval(x, y, w, h);
}
}
}
程序的初始界面如下: