画图板

来源:互联网 发布:淘宝调研网站 编辑:程序博客网 时间:2024/05/09 21:21

画图板

1.画图所需要的API

JFrame 窗体类

MouseListener 鼠标事件接口类

MouseEvent  鼠标对象

Graphics 画笔画布类


2.界面实现

1)定义一个DrawFrame类,继承JFrame类,类中定义主函数,定义一个初始化界面方法

2)主函数中实例化DrawFrame对象,调用初始化界面方法

3)在初始化界面的方法中,设置窗体各种属性

3.功能实现

1)寻找事件源,确定事件监听方法,以及接口,这都是对应的

2)定义一个事件处理类,实现确定的接口

3)在处理类中将接口中的方法实例化,并做相应的处理

4)在DrawFrame中实例化事件处理类的对象

5)给这个对象加上方法,别忘了要以add开头

6)窗体可见之后获取画笔g,调用处理类的对象的“借”方法把要借的借给事件处理类

7)事件处理类要写一个“借”的方法


3.画图功能大全

"直线","矩形","圆圈","立体球","圆弧","刷子","喷枪","填充弧角矩形","铅笔","填充圆弧","填充矩形","填充圆形","文字",

"图片","3D矩形","3D填充矩形"

 

都是调用画笔g的相关属性进行。


4.画图实现

1)界面

 

2)"直线","矩形","圆圈","立体球"

 

3)"圆弧","刷子","喷枪","填充弧角矩形",

 

4)"铅笔","填充圆弧","填充矩形","填充圆形",

 

5"文字","图片","3D矩形","3D填充矩形"

 


5.关于主面板

1)主函数继承面板,在左侧,下方,中间各放置一个面板,设置面板的大小与颜色

左侧面板:

JPanel westpanel =new JPanel();

westpanel.setBackground(Color.LIGHT_GRAY);

westpanel.setPreferredSize(new Dimension(100, 0));

frame.add(westpanel, BorderLayout.WEST);

下方:

JPanel southpanel =new JPanel();

southpanel.setLayout(new FlowLayout(FlowLayout.LEFT));

southpanel.setBackground(Color.LIGHT_GRAY);

southpanel.setPreferredSize(new Dimension(0, 50));

frame.add(southpanel, BorderLayout.SOUTH);

中间:

this.setBackground(Color.WHITE);

frame.add(this, BorderLayout.CENTER);

frame.setVisible(true);

this.addMouseListener(di);

this.addMouseMotionListener(di);

2)面板上的按钮

下方面板放颜色按钮:(以红色为例,其他一样的)

JButton buttonsouth1 =new JButton();

buttonsouth1.setBackground(Color.red);

buttonsouth1.setPreferredSize(new Dimension(30, 30));

southpanel.add(buttonsouth1);

buttonsouth1.addActionListener(di);

3)更多颜色的选择

 


6.关于重绘

在画图板实现时,会发现一个问题,就是当改变画板的大小时,原本画在上面的内容不见了,这是因为改变大小时,原来绘制的组件就消失了,新的组件会重新绘制一次。

然而我们绘制图形并不会再绘制一次。

所以我们需要借用自动绘制组件的方法。来绘制界面

重绘代码实现

1.定义抽象类,在类中定义图形的属性和绘制图形的抽象方法。

2.定义不同图形 的类,实现抽象类,重写绘制图形的方法。

3.将原来绘制图形的代码,改写成图形对象。

4.定义一个图形对象数组,用来存储你画过的图形对象。

5.paint方法中,将图形对象都调用再画一次。

1)定义数组储存

String[] array = {"直线","矩形","圆圈","立体球","圆弧","刷子","喷枪","填充弧角矩形","铅笔","填充圆弧","填充矩形","填充圆形","文字",

"图片","3D矩形","3D填充矩形"}

2//重写JPanel中的paint方法

public void paint(Graphicsgr) {

super.paint(gr);//调用父类的画方法

Graphics2D g = (Graphics2D)gr;

 

for (int i = 0;i < shapeArray.length;i++) {

Shape shape =shapeArray[i];

if (shape !=null) {

shape.draw((Graphics2D)g);

 

} else {

break;

}

}

}

3)定义shape父类

public abstract class Shape {

 

public int x1,x2, y1,y2;

public Colorcolor;

public BasicStrokestroke;

 

public Shape(int x1,int y1,int x2,int y2, Colorcolor, BasicStroke stroke) {

this.x1 =x1;

this.x2 =x2;

this.y1 =y1;

this.y2 =y2;

this.color =color;

this.stroke =stroke;

}

 

public abstract void draw(Graphics2Dg);

 

}

4)定义各子类继承父类

 

5)监听类中的数组储存

if (shape !=null) {

shapeArray[index] =shape;

index++;

if (index == 1000) {

index = 0;

}

}

以直线为例:(其余都差不多)

直线继承子类:

public class Shapelineextends Shape{

public Shapeline(int x1,int y1,int x2,int y2,Colorcolor,BasicStroke stroke){

super(x1,y1,x2,y2,color,stroke);

}

public  void draw(Graphics2Dg){

g.setColor(color);

g.setStroke(stroke);

g.drawLine(x1,y1,x2,y2);

}

}

监听类中的直线调用:

else if (type.equals("直线")) {

 

shape = new Shapeline(x1,y1, x2,y2, color,new BasicStroke(1));

shape.draw(g);}


7.关于立体球的实现

立体球实际上就是改变半径,不断的画圆,然后让颜色不断的变化,营造出渐变的立体效果。

if (type.equals("立体球")) {            int x = Math.min(x1, x2);int y = Math.min(y1, y2);int r = Math.abs(x1 - x2) / 2;g.setColor(color);for (int i = r; i > 0; i--) {// 让半径逐渐缩小if (i % 2 == 0) {x++;// 因为画圆的时候是从左上角开始的,所以让它慢慢向右下角移动,从而保证在中间变颜色的效果y++;}shape = new Shapefoval(x, y, 2 * i, 2 * i, g.getColor(), new BasicStroke(1));shape.draw(g);Color color = g.getColor();// 获得当前颜色int red = color.getRed();red += 5;// 让三色盘数字变化if (red > 255)red = 255;int green = color.getGreen();green += 5;if (green > 255)green = 255;int blue = color.getBlue();blue += 5;if (blue > 255)blue = 255;color = new Color(red, green, blue);g.setColor(color);// 让颜色变成当前的颜色shapeArray[index] = shape;index++;} 


0 0
原创粉丝点击