使用 Java Swing 绘制多条线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2531352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing multiple lines with Java Swing
提问by rize
I'm learning drawing lines with Java Swing in order to draw a labyrinth. I can draw one line at a specified position and it shows just fine. But when I want to draw multiple lines, only the last one shows. My code:
我正在学习使用 Java Swing 绘制线条以绘制迷宫。我可以在指定位置画一条线,它显示得很好。但是当我想画多条线时,只显示最后一条。我的代码:
public class LabyrinthGUI extends JFrame {
...
Line line;
for (int i = 0; i < 10; i++) {
line = new Line(i*25, 0, (i+1)*25, 50);
this.getContentPane().add(line);
}
}
public class Line extends JPanel{
private int x1, y1, x2, y2;
public Line(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void paintComponent (Graphics g) {
g.drawLine(x1, y1, x2, y2);
}
I probably have to refresh something, to display all the lines drawn with for-loop, but don't know what.
我可能需要刷新一些东西,以显示所有用 for 循环绘制的线条,但不知道是什么。
采纳答案by Pindatjuh
Why your example doesn't work is a simple one; Swing uses a layout manager to place every component added to a Container
onto the screen. This way, the lines do not overlap.
为什么你的例子不起作用是一个简单的例子;Swing 使用布局管理器将添加到 a 的每个组件放置Container
到屏幕上。这样,线条就不会重叠。
Instead, use one Component
in which every line is drawn. A solution for drawing a maze would be:
取而代之的是,使用其中Component
绘制了每条线的方法。绘制迷宫的解决方案是:
public class Labyrinth extends JPanel {
private final ArrayList<Line> lines = new ArrayList<Line>();
public void addLine(int x1, int y1, int x2, int y2) {
this.lines.add(new Line(x1, y1, x2, y2));
}
public void paintComponent(Graphics g) {
for(final Line r : lines) {
r.paint(g);
}
}
}
public static class Line {
public final int x1;
public final int x2;
public final int y1;
public final int y2;
public Line(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
public void paint(Graphics g) {
g.drawLine(this.x1, this.y1, this.x2, this.y2);
}
}
And then use Labyrinth.addLine
to add lines to your labyrinth. Also; specify a width and height for your Labyrinth
, by calling setBounds
or similar, because Swing may be cropping the graphics.
然后用于Labyrinth.addLine
在迷宫中添加线条。还; Labyrinth
通过调用setBounds
或类似方法为您的 指定宽度和高度,因为 Swing 可能会裁剪图形。
回答by Laurence Gonsalves
Your problem pretty much boils down to this:
您的问题几乎可以归结为:
public class Line extends JPanel
Think of each JPanel as an opaque card with something drawn on it. You're creating a bunch of these, each with a single line drawn on it, and then stacking them up on top of each other. That's why you can only see the most recent line.
把每个 JPanel 想象成一张不透明的卡片,上面画了一些东西。您正在创建一堆这些,每个上面都画了一条线,然后将它们堆叠在一起。这就是为什么您只能看到最近的一行。
Instead, you should have only one component that draws all of your lines in its paintComponent
method.
相反,您应该只有一个组件可以在其paintComponent
方法中绘制所有线条。