Graphics2D 和 JComponent
我没怎么用过Swing/G2D,所以请耐心等待。 我有以下类,它是我的 GUI 上的一个组件(意味着是一种可以绘制的 Canvas):
import javax.swing.*;
import java.awt.*;
public class DrawPanel extends JComponent{
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.black);
g2.fillRect(0, 0, getWidth(), getHeight());
BrushStroke bs = new BrushStroke();
add(bs);
}
}
我一直在尝试将以下内容添加到上面的 JComponent:
import javax.swing.*;
import java.awt.*;
public class BrushStroke extends JComponent{
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.red);
g2.fillOval(0, 0, 10, 10);
}
}
BrushStroke 不显示在 DrawPanel 上。
我一直在寻找答案,而我看到的每个例子似乎都是矛盾的。
如果有人尝试过我的做法,那么我们将不胜感激。另外,如果我采取了完全错误的方法,请说出来。
I have not used Swing/G2D much, so please be patient.
I have the following class which is a component on my GUI (meant to be a kind of Canvas to draw on):
import javax.swing.*;
import java.awt.*;
public class DrawPanel extends JComponent{
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.black);
g2.fillRect(0, 0, getWidth(), getHeight());
BrushStroke bs = new BrushStroke();
add(bs);
}
}
I have been trying to add the following to the above JComponent:
import javax.swing.*;
import java.awt.*;
public class BrushStroke extends JComponent{
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.red);
g2.fillOval(0, 0, 10, 10);
}
}
The BrushStroke does not show on the DrawPanel.
I have been searching forever for an answer, and each example I look at seems to be contradictory.
If anybody has attempted what I am, then help would be greatly appreciated. Also, if I am taking the completely wrong approach, please do say.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您永远不应该以任何绘画方法将组件添加到面板。每当 Swing 确定需要绘制组件时,就会调用绘制方法。因此,您将多次将组件添加到面板。
当您进行自定义绘制时,您负责重写 getPreferredSize() 方法以给出组件的大小。这样布局管理器就可以正确定位组件。如果您不这样做,则首选大小为 0,因此无需绘制任何内容。
有关详细信息,请阅读 Swing 教程中关于 自定义绘制 的部分和例子。
You should never add a component to a panel in any painting method. The painting methods are invoked whenever Swing determines a component needs to be painted. Therefore you would be adding the component to the panel multiple times.
When you do custom painting you are responsible for overriding the getPreferredSize() method to give the size of the component. This way the layout managers can position the components properly. If you don't do this then the preferred size is 0, so there is nothing to paint.
Read the section from the Swing tutorial on Custom Painting for more information and examples.
在
JComponent.add
方法上,文档 说:添加元素后,您应该刷新 DrawPanel。注意不要在
painComponent
方法中执行此操作,否则您将陷入无限递归。请执行以下操作:
编辑
这是一个完全有效的解决方案(扩展 JPanels 而不是 JComponent)
输出给出以下内容:
On the
JComponent.add
method, the documentation says:You should refresh your DrawPanel after adding an element to it. Watch out not to do it in the
painComponent
method, you will end up in an infinite recursion.Do the following instead:
EDIT
Here is a fully working solution (extending JPanels instead of JComponent)
The output gives the following: