目标是实现一个简单的画板,可以通过点击上方的按钮改变要画的形状,颜色。大致效果如下图
一、画图板界面实现
所需用到的主要API类:JFrame,JButton,FlowLayout,Graphics以及一些相关的类;
先创建窗体,设置相关属性,再添加按钮。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Draw extends JFrame{
public static void main(String args[]){
Draw dr = new Draw();
dr.showdraw();
}
public void showdraw(){
//继承后JFrame后无需实例化
this.setTitle("画图板");
this.setSize(700,500);
//frame.setResizable(false);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
FlowLayout f1 = new FlowLayout();
this.setLayout(f1);
//实例化监听器
DrawListener lis = new DrawListener();
//创建图形按钮和颜色按钮
String[] array ={"Line","Pencil","Rect","Oval","RoundRect","fillRect",
"fillArc","fillOval","Text","Image","Triangle","Polygon"};
for(int i=0 ; i
this.add(button);
button.addActionListener(lis);
}
Color[] colorArray ={Color.black,Color.blue,
Color.green,Color.red};
for(int i=0 ; i
button.setBackground(colorArray[i]);
button.setPreferredSize(new Dimension(30,30));//设置按钮大小
this.add(button);
button.addActionListener(lis);
}
this.setVisible(true); //setVisible设置窗体可见,放在所有组件后
Graphics g = this.getGraphics();
this.addMouseListener(lis);//用this指代frame可能出错
this.addMouseMotionListener(lis);
lis.setG(g);
}
二、监听类的实现
所需用到的主要API类:MouseAdapter,BasicStroke,Color,Graphics,JButton以及相关的类。以及ActionListener接口
监听按钮上对应的图形或颜色,并根据得到的内容实现对应的操作,具体的画图在Shape类中实现
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
//先继承类,后继承接口
public class DrawListener extends MouseAdapter implements ActionListener{
private int x1, y1, x2, y2,x3,y3,sx,sy,ex,ey;
private Graphics2D g2;
private String str = "Line";
private Color color;
private int flag = 1;//用于标记是否是第一次画线
public void setG(Graphics g){
g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);// 设置画笔开启抗锯齿
}
public void actionPerformed(ActionEvent e){JButton button = (JButton) e.getSource();//getSource返回事件源类型,也可以用getActionCommand返回String类型
if(button.getText() != "")//如果为图形按钮str = button.getText();
else{}
三、实现画图形的Shape类
需要定义多个构造方法,传递不同图形需要的参数值,再进行绘制
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Stroke;
public class Shape {
public int x1,x2,y1,y2;
public Stroke stroke;
public Color color;
public String type;
public Image img;
public String text;
public Shape(int x1,int y1,int x2,int y2,Color color,
Stroke stroke,String type){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.color=color;
this.stroke=stroke;
this.type=type;
}
public Shape(int x1,int y1,int x2,int y2,
Image img,String type){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.img=img;
this.type=type;
}
public Shape(int x1,int y1,
String text,String type){
this.x1=x1;
this.y1=y1;
this.text=text;
this.type=type;
}
public void draw(Graphics2D g){
if(type.equals("Line") || type.equals("Pencil")){
g.setColor(color);
g.setStroke(stroke);
g.drawLine(x1, y1, x2, y2);
}
else if(type.equals("Image")){
g.drawImage(img,x1,y1,x2,y2,null);
}
else if(type.equals("Rect")){
g.setColor(color);
g.setStroke(stroke);
g.drawRect(x1, y1, x2, y2);
}
else if(type.equals("Oval")){
g.setColor(color);
g.setStroke(stroke);
g.drawOval(x1, y1, x2, y2);
}
else if(type.equals("fillRect")){
g.setColor(color);
g.setStroke(stroke);
g.fillRect(x1, y1, x2, y2);
}
else if(type.equals("RoundRect")){
g.setColor(color);
g.setStroke(stroke);
g.drawRoundRect(x1, y1, x2, y2, 10, 10);
}
else if(type.equals("fillOval")){
g.setColor(color);
g.setStroke(stroke);
g.fillOval(x1, y1, x2, y2);
}
else if(type.equals("fillArc")){
g.setColor(color);
g.setStroke(stroke);
g.fillArc(x1, y1, x2, y2,0,-90);
}
else if(type.equals("Text")){
g.drawString(text, x1, y1);
}
else if(type.equals("Triangle")){
g.setColor(color);
g.setStroke(stroke);
g.drawLine(x1,y1,x2,y2);
g.drawLine(2*x1-x2, y2, x2, y2);
g.drawLine(x1,y1,2*x1-x2, y2);
}
}
}
四、画图板的重绘
在实现以上类后,已经可以实现点击对应按钮,画出对应的图形和颜色,但是一旦窗体改变,画的图形就会消失。这是因为图形没有被储存,一旦窗体改变后就生成了一个新窗体,旧的图形不会在新窗体上显示,因此需要保存图形数据,并在新窗体上重绘。
//draw类
lis.setArray(shapeArray);//增加对图形数组的监听
public Shape[] shapeArray = new Shape[1000];//储存图形的数组
public void paint(Graphics g){
super.paint(g);//super为父类
System.out.println("绘制了");
Graphics2D g2 = (Graphics2D) g;
for(int i=0 ; i
if(s != null)
s.draw(g2);
}
}
//drawListener类,增加图形数组,并收集数据
private Shape[] array;
private int index=0;
public void setArray(Shape[] array){
this.array = array;
}
//以画空心矩形为例
if(str.equals("Rect")){
Shape s = new Shape(x2,y2,80,40,color,
new BasicStroke(1),"Rect");
s.draw(g2);
if(index<1000)
array[index++] = s;
}