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
  • 168,117
  • 40
  • 217
  • 433
Sahil Chaudhary
  • 493
  • 2
  • 10
  • 29

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
  • 168,117
  • 40
  • 217
  • 433
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,951
  • 7
  • 42
  • 69
  • 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