0

i have my class declared:

public class myRightPanel extends JPanel

then i override the paintComponent of my super class like this:

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. How do i do this when i have already overridden my paintComponent()?

Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420
Sahil Chaudhary
  • 493
  • 2
  • 8
  • 27

2 Answers2

3

Store the x,y as a Point as an attribute of the class so it is accessible within the paint method. Call repaint().

Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420
0

You need to use the Graphics object to draw any content you want.

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

18bytes
  • 5,751
  • 7
  • 41
  • 67
  • Absent any descenders, the text will be invisible, as shown [here](http://stackoverflow.com/a/2658663/230513); see also this [answer](http://stackoverflow.com/a/13333956/230513). – trashgod Nov 16 '12 at 14:31