在JPanel中绘制一个JComponent [英] Drawing a JComponent inside a JPanel
问题描述
我正在尝试在JPanel中显示一个JComponent. 我使用的是空布局,因为可以在运行时更改组件的位置,并且必须对其进行控制.
I am trying to display a JComponent inside a JPanel. I am using null layout because location of the components can be changed during runtime and I have to control them.
但是以下代码不起作用.如果我明确调用"paintComponent"方法,则JComponent仅在显示时可见,我认为这不是一个好习惯.
But the following code does not work. The JComponent only becomes visible on display if I explicity call the "paintComponent" method, which I think is not good practice.
我的JComponent类
My JComponent Class
public class MyIcon extends JComponent
{
private double xPos;
private double yPos;
private double radius = 30;
public MyIcon(double xPos, double yPos)
{
this.xPos = xPos;
this.yPos = yPos;
this.setBounds((int)xPos, (int)yPos, (int)radius, (int)radius);
this.setPreferredSize(new Dimension((int)radius, (int)radius ) );
}
public Dimension getPreferredSize()
{
return ( new Dimension( (int)radius, (int)radius ) );
}
public Dimension getMinimumSize()
{
return new Dimension((int)radius, (int)radius);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.drawOval( (int)xPos, (int)yPos, (int)radius, (int)radius);
g2d.fillOval((int)xPos, (int)yPos, (int)radius, (int)radius);
System.out.println("Icon.paintComponnet() called");
}
}
我的小组课程
public class MyPanel extends JPanel
{
private MyIcon myIcon;
private Graphics2D graphics;
private int width = 700;
private int height = 500;
public MyPanel()
{
myIcon = new MyIcon(20,30);
init();
}
private void init()
{
setLayout(null);
setBackground(Color.WHITE);
setPreferredSize( new Dimension(width, height) );
setBorder(BorderFactory.createLineBorder(Color.BLACK));
add( myIcon );
myIcon.repaint();
}
public void paintComponent(Graphics g)
{
graphics = (Graphics2D) g;
super.paintComponent(g);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
System.out.println("MyPanel.paintComponnet() called");
// why my Icon only gets painted if I explicitly call for it ?
//myIcon.paintComponent(g);
}
}
我的相框课
public class Editor {
public static void main(String[] args)
{
Editor editor = new Editor();
}
private MyPanel myPanel;
private JFrame frame;
private JToolBar toolBar;
public Editor()
{
myPanel = new MyPanel();
init();
}
private void init()
{
frame = new JFrame("Editor");
Container content = frame.getContentPane();
frame.setSize(800, 600);
frame.setLayout(new BorderLayout(15,15));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
content.add(myPanel,BorderLayout.WEST);
frame.pack();
frame.setVisible(true);
}
}
推荐答案
也许一个好的方法是Icon类不会扩展JComponent
,而仅仅是一个简单的对象.
Maybe a good approach would be that the Icon class would not extend JComponent
but would be just a simple object.
然后,将当前的Icon.paintComponent
重命名为类似drawIcon
的名称,然后直接从MyPanel.paintComponent
调用drawIcon
方法并将引用传递给Graphics对象.
Then, you rename the current Icon.paintComponent
you have to something like drawIcon
and call the drawIcon
method from the MyPanel.paintComponent
directly and passing the reference to the Graphics object.
这样,您不必担心使用空布局,只需使用Graphics(2d)
API就可以控制显示图标的位置.
This way you don't have to wonder about using a null layout and you can control the place to display the icon to by just using the Graphics(2d)
APIs.
这篇关于在JPanel中绘制一个JComponent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!