使用 Java (awt) 绘制多个矩形
这是我的代码:
class Ramka extends JFrame
{
public static final int SZEROKOSC = 800;
public static final int WYSOKOSC = 600;
Container powZawartosci = getContentPane();
public Ramka()
{
setSize(SZEROKOSC, WYSOKOSC);
setTitle("Siatka bryły by Paweł Mysior");
}
public void addRectangle(int startX, int startY, int sizeX)
{
drawRectangle rect = new drawRectangle(startX, startY, sizeX);
powZawartosci.add(rect);
}
class drawRectangle extends JPanel
{
private int a, startX, startY;
public drawRectangle(int startX, int startY, int a) // square
{
this.a = a;
this.startX = startX;
this.startY = startY;
}
public void paintComponent(Graphics g)
{
Rectangle2D rect = new Rectangle2D.Double(startX, startY, a, a);
Graphics2D g1 = (Graphics2D) g;
g1.draw(rect);
}
}
public class Main
{
public static void main(String[] args)
{
Ramka ramka = new Ramka();
ramka.addRectangle(200, 200, 50);
ramka.addRectangle(100, 100, 100);
ramka.addRectangle(300, 300, 150);
ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ramka.setVisible(true);
}
}
我想要它做的是绘制三个矩形(抛开这样做的功能和意义,我仍然在学习)。
但它只绘制最后一个,从 300 和 300 开始。我不太明白 PaintComponent 的东西...
提前感谢您的帮助, 保罗
Here's my code:
class Ramka extends JFrame
{
public static final int SZEROKOSC = 800;
public static final int WYSOKOSC = 600;
Container powZawartosci = getContentPane();
public Ramka()
{
setSize(SZEROKOSC, WYSOKOSC);
setTitle("Siatka bryły by Paweł Mysior");
}
public void addRectangle(int startX, int startY, int sizeX)
{
drawRectangle rect = new drawRectangle(startX, startY, sizeX);
powZawartosci.add(rect);
}
class drawRectangle extends JPanel
{
private int a, startX, startY;
public drawRectangle(int startX, int startY, int a) // square
{
this.a = a;
this.startX = startX;
this.startY = startY;
}
public void paintComponent(Graphics g)
{
Rectangle2D rect = new Rectangle2D.Double(startX, startY, a, a);
Graphics2D g1 = (Graphics2D) g;
g1.draw(rect);
}
}
public class Main
{
public static void main(String[] args)
{
Ramka ramka = new Ramka();
ramka.addRectangle(200, 200, 50);
ramka.addRectangle(100, 100, 100);
ramka.addRectangle(300, 300, 150);
ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ramka.setVisible(true);
}
}
What I want it to do is draw three rectangles (set aside the functionality and sense of doing so, I'm still just learning).
But it draws only the last one, starting at 300 and 300. I don't really understand the paintComponent thing...
Thanks in advance for any help,
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您正在相互添加三个
JPanel
。这似乎是一种奇怪的绘制矩形的方法,但对于这种设计,您需要使用LayoutManager
。查看此链接,并尝试学习。不过下面的代码应该可以解决问题。
在您的
JPanel
衍生产品中,您可以跟踪需要绘制的矩形
。我正在自发地编写下面的代码,因此请先检查是否有错误。I beleive that you are adding three
JPanel
s on top of each other. This seems like an odd way to draw rectangles, but with this design, you need to use aLayoutManager
.Check out this link, and try to learn. The code below should do the trick though.
In your
JPanel
derivative, you can keep track of theRectangle
s that you need to draw. I am writing the code below spontanously, so check for errors first.问题基本上在于您在这里使用了两种不同的抽象级别。
首先,您向 JFrame 添加一个组件,这在某些时候是没问题的。
您正在添加“DrawRectangle”实例,就像添加新按钮、标签或另一个面板一样。当您在同一位置添加组件时,问题就会出现。 JFrame 的主面板(内容窗格)使用 " 边框“布局管理器,如果您不添加任何约束,则将组件放置在中间。
因此,这一行:
始终将您的组件添加到“中心”,覆盖前一个组件。这就是为什么你只看到一个矩形。
这里使用的第二个抽象层次是自己绘制组件。这是低级别的,您必须告诉组件谁在哪里绘制每条线。
这很好,但是如果您想在同一个组件中绘制多个矩形,则必须保存每个矩形的引用(使用像列表这样的集合),然后迭代该集合并绘制所有矩形。
像这样:
许多http://img40.imageshack.us/img40/8125/capturadepantalla201001nd。 png
我获取了你的代码,并对其进行了更改,以反映我所说的内容。最终结果使用相同的组件,但该组件依次绘制所有矩形。
另请注意命名/大括号样式,虽然不是强制性的,但在 Java 编程中很常见
The problem basically is that you're using two different levels of abstraction here.
In the first, you are adding a component to your JFrame, which is fine at some point.
You're adding your "DrawRectangle" instance, just the same way you would add a new button, a label or another panel. The problem comes when you add components in the same position. JFrame's main panel ( the content pane ) uses a "Border" layout manager that places the component in the middle if you don't add any constraint.
So, this line:
Always adds your component in the "center", overriding the previous one. That's why you only saw one rectangle.
The second level of abstraction used here is painting the component yourself. This is low level and you have to tell the component who to draw each line and where.
That's fine, but if you want to draw several rectangles in the same component, you have to hold the references for each one ( using a collection like a list ) and then iterate that collection and draw them all.
Like this:
many http://img40.imageshack.us/img40/8125/capturadepantalla201001nd.png
I took your code, and changed it, to reflect what I'm saying. The final result, uses the same component, but this component in turn draws all the rectangles.
Notice also the naming/brace style, while is not mandatory it is common while programming in Java