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 ?

4

1 回答 1

0

使用 ComponentListener 并监听触发的调整大小事件。使您的 UI 无效以强制重新绘制,这会导致您的 paintComponent 将被调用。

组件监听器

于 2013-03-31T11:10:22.750 回答