简体   繁体   English

在JPanel中绘制

[英]drawing in a JPanel

i have my class declared: 我宣布我的课:

public class myRightPanel extends JPanel

then i override the paintComponent of my super class like this: 然后我像这样重写我的超类的paintComponent:

public void paintComponent(Graphics g){  
        super.paintComponents(g);
                //Draw bunch of things here
}

Now it turns out i also need a method which takes in two integer(x,y) parameters and adds something to my already drawn myRightPanel at that coordinates. 现在事实证明,我还需要一种方法,该方法接受两个integer(x,y)参数,并在该坐标处向已绘制的myRightPanel添加一些内容。 How do i do this when i have already overridden my paintComponent() ? 当我已经覆盖我的paintComponent()时该怎么办?

Store the x,y as a Point as an attribute of the class so it is accessible within the paint method. 将x,y作为Point为类的属性,以便可以在paint方法中访问它。 Call repaint() . 调用repaint()

You need to use the Graphics object to draw any content you want. 您需要使用Graphics对象绘制所需的任何内容。

For example: 例如:

public void paintComponent(Graphics g){  
  super.paintComponents(g);
  g.drawString("Hello test", 0, 0);
}

I recommend reading Java 2D tutorial: http://docs.oracle.com/javase/tutorial/2d/index.html 我建议阅读Java 2D教程: http : //docs.oracle.com/javase/tutorial/2d/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM