0

I need to draw a x-y axis coordinate system on a JPanel. I want to implement a function that when resize JFrame, the x,y axis coordinate can resize automatically.

public void paintComponent(Graphics gl) {

 Graphics2D g = (Graphics2D) gl;
 g.setColor(new Color(222, 222, 222));
 g.fillRect(0, 0, this.getWidth(), this.getHeight());
 g.setColor(new Color(0, 0, 0));
 int x=15;
 int y=15;
 g.drawString("20", 0, 10);
 for(int i=1;i<=20;i++) {
     g.drawLine(x, y+(35*(i-1)), x, y+(35*i));
     g.drawString(""+(20-i), 0, y+(35*i));
 }
 for(int i=1;i<=10;i++) {
     g.drawLine(x+(70*(i-1)),715, x+(70*i), 715);
     g.drawString(""+i,  x+(70*i),730);
 }
}

This is how I draw the x y coordinate system.
Could you give me hint for resizing it ?

mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

0

Use an ComponentListener and listen to the resize Events fired up. Invalidate your UI to force an repaint which cause your paintComponent will be called.

ComponentListener

Kitesurfer
  • 3,438
  • 2
  • 29
  • 47