简体   繁体   English

逐步在JPanel上绘图

[英]Drawing on JPanel step by step

I would like to create a JPanel and draw things on it one by one. 我想创建一个JPanel并在其上逐个绘制东西。 I would like to see them added after each other. 我希望看到他们彼此加入。 The problem is, I always have to wait until everything is finished by paintComponent method. 问题是,我总是必须等到paintComponent方法完成所有操作。 Is there a way to achieve what I desire? 有没有办法实现我想要的? Thanks in advance! 提前致谢!

package javapaintui;

import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

class JavaPaintUI extends JFrame {

    private JPanel jPanel2;

    public JavaPaintUI() {
        initComponents();
    }

    private void initComponents() {
        jPanel2 = new Panel2();
        this.setContentPane(jPanel2);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }

    class Panel2 extends JPanel {

        Panel2() {
            setPreferredSize(new Dimension(420, 420));
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.drawString("BLAH", 20, 20);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
                Logger.getLogger(JavaPaintUI.class.getName()).log(Level.SEVERE, null, ex);
            }
            g.drawRect(200, 200, 200, 200);
        }
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JavaPaintUI().setVisible(true);
            }
        });
    }
}

Never, ever, ever use Thread.sleep(...) in a Swing application's event dispatch thread. 永远不要在Swing应用程序的事件分派线程中使用Thread.sleep(...) That goes 100-fold true for the paintComponent method as you put it to sleep. 当您进入睡眠状态时,paintComponent方法的正确性达到了100倍。 The solution is to use a Swing Timer. 解决方案是使用Swing计时器。

eg, 例如,

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.*;

class JavaPaintUI extends JFrame {
   private static final Color FILL_COLOR = Color.BLUE;
   private static final Color BORDER_COLOR = Color.RED;
   public static final Stroke STROKE = new BasicStroke(4f);

   private JPanel jPanel2;

   public JavaPaintUI() {
      initComponents();
   }

   private void initComponents() {
      jPanel2 = new Panel2();
      this.setContentPane(jPanel2);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      pack();
   }

   class Panel2 extends JPanel {
      private static final int TIMER_DELAY = 2000;
      private Random random = new Random();
      private List<Shape> shapeList = new ArrayList<>();

      Panel2() {
         setPreferredSize(new Dimension(420, 420));
         new Timer(TIMER_DELAY, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
               int width = random.nextInt(100);
               int height = random.nextInt(100);
               int x = random.nextInt(getWidth() - width);
               int y = random.nextInt(getHeight() - height);
               shapeList.add(new Rectangle(x, y, width, height));
               repaint();
            }
         }).start();
      }

      @Override
      public void paintComponent(Graphics g) {
         super.paintComponent(g);
         g.drawString("BLAH", 20, 20);
         Graphics2D g2 = (Graphics2D) g;
         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
               RenderingHints.VALUE_ANTIALIAS_ON);
         g2.setStroke(STROKE);
         for (Shape shape : shapeList) {
            g2.setColor(FILL_COLOR);
            g2.fill(shape);
            g2.setColor(BORDER_COLOR);
            g2.draw(shape);
         }
      }
   }

   public static void main(String args[]) {
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            new JavaPaintUI().setVisible(true);
         }
      });
   }
}

Edit 编辑
You ask: 你问:

Now the problem is, I intend to draw a binary tree. 现在的问题是,我打算绘制一个二叉树。 The drawing mechanism works this way: I pass the tree's root to the drawing function, which will iterate through it, and draw a tree on the panel. 绘制机制的工作方式如下:我将树的根传递给绘图函数,该函数将对其进行迭代,然后在面板上绘制一棵树。 I use drawString, drawOval and drawLine functions, which seems to be hard to implement this way. 我使用drawString,drawOval和drawLine函数,这似乎很难实现。 Do you have a solution for it maybe? 您是否有解决方案?

Actually I've given you the perfect solution for this. 实际上,我已经为您提供了完美的解决方案。 The shapeList List<Shape> variable will accept anything that implements the Shape interface which means you can add Line2D, Ellipse2D and similar objects as well as text to it. shapeList List<Shape>变量将接受任何实现Shape接口的内容,这意味着您可以向其中添加Line2D,Ellipse2D和类似对象以及文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM