java 用Swing、ActionListener类画圆遇边框弹回小程序升级(增加了移速控制按钮)

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);
           }
     }
}

初始界面如下:
运行初始界面

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值