画图板重绘

package 画图板重绘;

import java.awt.Graphics;

import javax.swing.JFrame;

public class Rejframe extends JFrame{
Shape[] s;

public void paint(Graphics g){
//一定要先调用父类的paint方法,用来绘制窗体
super.paint(g);
//写自己重绘的方法
for(int i=0;i<s.length;i++)
{
Shape shape = s[i];
if(shape!=null){

if("直线".equals(shape.type))
{
g.drawLine(shape.x1, shape.y1,shape.x2, shape.y2);
}
if("矩形".equals(shape.type))
{
g.drawRect(Math.min(shape.x1,shape.x2), Math.min(shape.y1, shape.y2), Math.abs(shape.x1-shape.x2), Math.abs(shape.y1-shape.y2));
}
}
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
我们需要在监听器中设置一个Shape(图形类)数组来记录画图需要的数据:

package 画图板重绘;
//图形类
public class Shape {
int x1,x2,y1,y2;
String type;
}

---------------------

原文地址:https://www.cnblogs.com/hyhy904/p/11342595.html