3

I am trying to create a simple animation which draws random rectangles when a button is pressed. So far I managed to create rectangle on the press of a button. I want to further develop the code so that when I press the button, more than multiple random rectangles are created. I tried to create a for loop which asks the inner class to repaint itself but it still didn't work. can anyone help me please.

    public class TwoButtonsRandomRec {

    JFrame frame;
    private int width = 500;
    private int height = 500;
    private DrawPanel dp;

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }


    public static void main (String[] args)
    {   
        TwoButtonsRandomRec test = new TwoButtonsRandomRec(); 
        test.go();
    }

    public void go()
    {   
        dp = new DrawPanel();

        JButton start = new JButton("Start");
        start.addActionListener(new startListener());
        JButton stop = new JButton("Stop");
        stop.addActionListener(new stopListener());

        frame = new JFrame();
        frame.setSize(getWidth(), getHeight());
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(BorderLayout.NORTH, start);
        frame.getContentPane().add(BorderLayout.SOUTH, stop);
    }

    class startListener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            frame.getContentPane().add(BorderLayout.CENTER, dp);
            frame.repaint();
            frame.getRootPane().revalidate();       
            for(int i=0; i<10; i++){
                dp.repaint();
            }
        }
    }

    class stopListener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            System.out.println("stop");
        }
    }

    class DrawPanel extends JPanel{

        public void paintComponent(Graphics g){
            int w = 5+(int)(Math.random() * width-5);
            int h = 5+(int)(Math.random() * height-5);
            int maxX = width-w; // diffX & diffY are used to ensure that rectangle is
            int maxY = width-h; // draw completely inside the window
            int x = (int)(Math.random() * maxX);
            int y = (int)(Math.random() * maxY);
            Color color = new Color((int) (Math.random()*256), // random red
                    (int) (Math.random()*256), // random green
                    (int) (Math.random()*256));// random blue
            g.setColor(color);
            g.fillRect(x,y,w,h);
        }
    }
}
4

3 回答 3

3

repaint()只是告诉 Swing “当你有时间时,请重新粉刷这个区域”。因此,如果您在循环中添加矩形并在每次迭代时调用 repaint,则所有矩形只会在循环完成并且操作事件已处理后才会出现。

要制作动画,您需要在单独的线程中循环。最简单的方法是使用Swing Timer。启动“开始”按钮时,启动一个计时器,该计时器添加一个随机矩形并repaint()每 X 毫秒调用一次。按下停止按钮时,停止计时器。

于 2012-12-30T13:02:16.763 回答
0

您应该做的是将循环放在 paintComponent 方法中,而不是在循环中调用 repaint。所以你的paintComponent方法应该是这样的:

    public void paintComponent(Graphics g){
        for (int i = 0; i < 10; i++) {
        int w = 5+(int)(Math.random() * width-5);
            int h = 5+(int)(Math.random() * height-5);
            int maxX = width-w; // diffX & diffY are used to ensure that rectangle is
            int maxY = width-h; // draw completely inside the window
            int x = (int)(Math.random() * maxX);
            int y = (int)(Math.random() * maxY);
            Color color = new Color((int) (Math.random()*256), // random red
                    (int) (Math.random()*256), // random green
                    (int) (Math.random()*256));// random blue
            g.setColor(color);
            g.fillRect(x,y,w,h);
        }
    }

您执行的操作应如下所示:

public void actionPerformed(ActionEvent event){
    frame.getContentPane().add(BorderLayout.CENTER, dp);
    frame.repaint();
    frame.getRootPane().revalidate();       
    dp.repaint();
}
于 2012-12-30T13:12:45.617 回答
0

好吧,在这里我为你做了一个简短的 EG。它在随机屏幕位置显示随机矩形、随机时间。(您可以根据自己的要求设置自己的随机化值和屏幕位置最大界限。

在此处输入图像描述

还要注意

            int i=(int)(Math.random()*10);

             int j=(int)(Math.random()*10);

             for(;i<j;i++)

有时我可能比 j.So,loop 可能无法在一个或两个 cliks 上工作。根据您的需要进行更改。

这是工作代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SimpleStamper extends JApplet {


  public void init() {

     Display display = new Display();
     setContentPane(display);
  }


  class Display extends JPanel implements MouseListener {

     Display() {

         setBackground(Color.black);
         addMouseListener(this);
     }

     public void mousePressed(MouseEvent evt) {


         if ( evt.isShiftDown() ) {

            repaint();
            return;
         }

         int x = evt.getX(); 
         int y = evt.getY();  

         Graphics g = getGraphics();  

            //***MODIFY THE FOLLOWING LINES****//   
             int i=(int)(Math.random()*10);
             int j=(int)(Math.random()*10);
             for(;i<j;i++)
             { g.setColor(Color.red);

               x=(int)(Math.random()*100);
               y=(int)(Math.random()*100);
             g.fillRect( x , y , 60, 30 );
             g.setColor(Color.black);
             g.drawRect(x , y , 60, 30 );}


         g.dispose();  
      } 
     public void mouseEntered(MouseEvent evt) { }
     public void mouseExited(MouseEvent evt) { }
     public void mouseClicked(MouseEvent evt) { }
     public void mouseReleased(MouseEvent evt) { }

   } 

  } 
于 2012-12-30T13:44:53.733 回答