如何使用paintComponent()多线程?

时间:2017-01-07 22:29:20

标签: java multithreading swing graphics2d

我创建了一个应用程序,其中包含一个方块,每当它触及框架的边缘时就会反弹。我没有在应用程序中解决问题,问题是我不知道如何创建各种线程,以便在帧内有多个方块。 我尝试了很多东西,但我无法弄清楚我应该在哪里创建线程。 我还注意到,只有当我直接在框架内添加它时,方形才可见,而不是当我将它放在JPanel中时。

Square.java

public class Square extends JComponent implements ActionListener {

  int width = 20;
  int height = 20;
  double y = Math.random() * 360;
  double x = Math.random() * 360;
  boolean xMax = false;
  boolean yMax = false;
  boolean xMin = true;
  boolean yMin = true;
  Rectangle2D.Double square = new Rectangle2D.Double(x, y, width, height);

  public Square() {
    Timer t = new Timer(2, this);
    t.start();
  }

  public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    super.paintComponent(g);
    g2.setColor(Color.BLUE);
    g2.fill(square);

    x_y_rules();


  }
  public void x_y_rules() {
    if (xMax == true) {
        x = x - 0.5;
        if (x <= 0) {
            xMax = false;
        }
    } else {
        x = x + 0.5;
        if (x >= this.getWidth()) {
            xMax = true;
        }
    }
    if (yMax == true) {
        y = y - 0.5;
        if (y <= 0) {
            yMax = false;
        }
    } else {
        y = y + 0.5;
        if (y >= this.getHeight()) {
            yMax = true;
        }
    }
    square.setFrame(x, y, width, height);
  }

 @Override
 public void actionPerformed(ActionEvent arg0) {
    repaint();
 }
}

App.java

public class App extends JFrame {

public static void main(String[] args) {
    JFrame jf = new JFrame();
    Square sqr = new Square();
    jf.setSize(400, 400);
    jf.setVisible(true);
    jf.add(sqr);
    jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
    jf.setLocationRelativeTo(null);
 }
}

尽管我在计时器内放置了2个时间,这个方块移动得非常慢,这是正常的吗?

1 个答案:

答案 0 :(得分:2)

的问题:

  1. 您在paintComponent方法中获得了程序逻辑,x_y_rules()方法调用。把它拿出去,因为它不属于那里,而是将它放入Timer所属的ActionListener代码中。
  2. 如果需要,您可以为每个Square提供自己的Swing Timer。这并不是一个线程问题,因为每个Timer的ActionListener都将在EDT上运行。
  3. 两毫秒是期望在Swing Timer中使用的不切实际的时间片,没有定时器会快速运行。 11到13是最快的期望或希望。
  4. 如果您希望精灵移动得更快,请在移动代码中为delta-x和delta-y赋予更大的值。
  5. 您的JComponent没有定义首选大小,这可能是它没有出现在JPanel中的原因,因为默认的FlowLayout会将其大小调整为[0,0]。覆盖其getPreferredSize()并让它返回合理的Dimension值。
  6. 在添加所有组件之前,您已在JFrame上调用setVisible(true),禁止使用。
  7.   

    好吧,我把一个getPrefferedSize()放在方形类中,但我遇到了一个问题:方块不是&#34;在一起&#34;它就像它们弹跳一样在单独的小组上

    然后你的程序结构被破坏了。你真的不想创建单独的Swing组件,事实上你的Square类不应该扩展JComponent或JPanel。相反

    • Square应该是逻辑类,一个从无延伸(默认对象除外)的类。
    • 给它一个绘图方法,比如public void draw(Graphics g) {....}
    • 创建一个扩展JPanel的类,称为DrawingPanel,并覆盖其paintComponent方法。
    • 为DrawingPanel类指定ArrayList<Square>,以便它可以容纳多个Square对象。
    • 为DrawingPanel类提供一个Swing Timer
    • 在DrawingPanel类的Timer中,让它更新ArrayList中所有Squares的位置,然后调用repaint()
    • 在paintComponent方法中,使用for循环遍历列表中的所有Squares,并调用每个方法的draw方法。

    例如:

    enter image description here

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class DrawingPanel extends JPanel {
        private static final int PREF_W = 600;
        private static final int PREF_H = PREF_W;
        private static final int TIMER_DELAY = 20;
        private static final Color[] SQUARE_COLOR = { Color.BLUE, Color.CYAN, Color.DARK_GRAY,
                Color.BLACK, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
                Color.PINK, Color.RED, Color.YELLOW };
        List<Square> squareList = new ArrayList<>();
    
        public DrawingPanel() {
            // create a bunch of squares
            for (int i = 0; i < SQUARE_COLOR.length; i++) {
                squareList.add(new Square(SQUARE_COLOR[i], PREF_W, PREF_H));
            }
    
            setBackground(Color.WHITE);
    
            // create and start the timer
            new Timer(TIMER_DELAY, new TimerListener()).start();
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            // simply draw all the squares in the list
            for (Square square : squareList) {
                square.draw(g);
            }
        }
    
        // set size of JPanel
        @Override
        public Dimension getPreferredSize() {
            if (isPreferredSizeSet()) {
                return super.getPreferredSize();
            }
            return new Dimension(PREF_W, PREF_H);
        }
    
        private class TimerListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {            
                // simply iterate through list and move all squares
                for (Square square : squareList) {
                    square.move();
                }
                repaint(); // then repaint the GUI
            }
        }
    
        private static void createAndShowGui() {
            DrawingPanel mainPanel = new DrawingPanel();
    
            JFrame frame = new JFrame("Drawing Panel");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(mainPanel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> createAndShowGui());
        }
    }
    
    // this class does *not* extend JPanel or JComponent
    class Square {
        public static final int WIDTH = 20;
    
        // location of Square
        private double sqrX;
        private double sqrY;
    
        // X and Y speed
        private double deltaX;
        private double deltaY;
    
        // width and height of DrawingPanel JPanel
        private int dpWidth;
        private int dpHeight;
    
        // image to draw
        private Image image;
    
        public Square(Color color, int dpWidth, int dpHeight) {
            this.dpWidth = dpWidth;
            this.dpHeight = dpHeight;
    
            // create square at random location with random speed
            sqrX = Math.random() * (dpWidth - WIDTH);
            sqrY = Math.random() * (dpHeight - WIDTH);
            deltaX = Math.random() * 10 - 5;
            deltaY = Math.random() * 10 - 5;
    
            // one way to draw it is to create an image and draw it
            image = new BufferedImage(WIDTH, WIDTH, BufferedImage.TYPE_INT_ARGB);
            Graphics g = image.getGraphics();
            g.setColor(color);
            g.fillRect(0, 0, WIDTH, WIDTH);
            g.dispose();
        }
    
        public void move() {
    
            // check that we're not hitting boundaries
            if (sqrX + deltaX < 0) {
                deltaX = Math.abs(deltaX);
            }
            if (sqrX + deltaX + WIDTH >= dpWidth) {
                deltaX = -Math.abs(deltaX);
            }
            sqrX += deltaX;
    
            // check that we're not hitting boundaries
            if (sqrY + deltaY < 0) {
                deltaY = Math.abs(deltaY);
            }
            if (sqrY + deltaY + WIDTH >= dpHeight) {
                deltaY = -Math.abs(deltaY);
            }
            sqrY += deltaY;
    
        }
    
        public void draw(Graphics g) {
            int x = (int) sqrX;
            int y = (int) sqrY;
            g.drawImage(image, x, y, null);
        }
    }