我想创建一个JPanel
并逐一绘制它。我想看到他们互相追加。问题是,我总是要等到所有事情都由paintComponent
方法完成。有没有办法实现我的愿望?提前谢谢!
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);
}
});
}
}
答案 0 :(得分:4)
永远不要在Swing应用程序的事件派发线程中使用Thread.sleep(...)
。当你把它放到睡眠状态时,对于paintComponent方法来说这是真实的100倍。解决方案是使用Swing Timer。
如,
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);
}
});
}
}
修改强>
你问:
现在的问题是,我打算画一棵二叉树。绘图机制以这种方式工作:我将树的根传递给绘图函数,绘图函数将迭代它,并在面板上绘制一个树。我使用drawString,drawOval和drawLine函数,这似乎很难以这种方式实现。你有解决方案吗?
其实我已经为你提供了完美的解决方案。 shapeList List<Shape>
变量将接受实现Shape
接口的任何内容,这意味着您可以向其添加Line2D,Ellipse2D和类似对象以及text。