简易绘图板
简易绘图板的构建,用到了窗体的构建,监听器的加入,Graphics类使用的相关知识,主要思路是在创建了窗体之后完善窗体结构,得到窗体对象的Graphics类之后,向窗体加入监听器,对Graphcis对象进行操作从而实现简易绘图板的功能。
绘图板基本功能:
1.选择绘制直线、矩形、圆、三角形和多边形这些不同的图形
2.选择不同的颜色进行绘图
3.能显示当前选取的颜色
4.保存当前已经绘制的图形,不因拖动窗口和最小化等原因消失
建立三个类:
1.DrawUI类,构建窗体对象,加入各种按钮,提供Graphics对象以操作。
2.DrawListener类,作为动作监听器和鼠标监听器,完成对于Graphics对象的绘图以及绘图选项转换,颜色转换等功能。
3.Shape类,用于保存已经画出的图形对象使重新产生Graphics对象时能够出现原有的图形
监听器:
监听器的功能是用于监听事件源中产生的事件
DrawUI代码:
public class DrawUI extends JFrame{
public static void main(String arg[]) {
DrawUI d = new DrawUI();
d.initUI();
}
Shape[] arrayShape;
public void initUI() {
JFrame jf = new JFrame();
this.setTitle("绘图");
this.setSize(1000,800);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
//流式布局
FlowLayout flow =new FlowLayout();
this.setLayout(flow);
//建立监听器
DrawListener dl = new DrawListener();
this.addMouseListener(dl);
String[] btnstrs = {"直线","矩形","圆","三角形","多边形"};
Color[] colors = {Color.white,Color.BLACK,Color.gray,Color.blue,Color.red,Color.green,Color.yellow};
//当前颜色按钮
JButton bucolor = new JButton();
bucolor.setBackground(Color.BLACK);
bucolor.setPreferredSize(new Dimension(80,50));
this.add(bucolor);
//画图按钮
for(int i=0;i<btnstrs.length;i++) {
addButton(btnstrs[i],jf,dl);
}
//颜色按钮
for(int i=0;i<colors.length;i++) {
addButton(colors[i],jf,dl);
}
//让窗体可视化并传窗体的图形到监听器中
this.setVisible(true);
Graphics g = this.getGraphics();
dl.g = g;
dl.bucolor = bucolor;
arrayShape = dl.getarrayShape();
}
public void addButton(String btnstr,JFrame jf,ActionListener al) {
JButton bu = new JButton(btnstr);
bu.setPreferredSize(new Dimension(80,50));
this.add(bu);
bu.addActionListener(al);
}
public void addButton(Color co,JFrame jf,ActionListener al) {
JButton bu =new JButton();
bu.setBackground(co);
bu.setPreferredSize(new Dimension(50,30));
this.add(bu);
bu.addActionListener(al);
}
public void paint(Graphics g) {
super.paint(g);
for(int i=0;i<arrayShape.length;i++) {
Shape tempShape = arrayShape[i];
tempShape.drawShape(g);
}
// System.out.println("1");
}
}
监听器DrawListener代码:
public class DrawListener implements MouseListener,ActionListener{
int x1,x2,y1,y2,x3,y3,x4,y4,x5,y5;
Graphics g;
String str = "";
int count = 0;
int[] px = new int[100];
int[] py = new int[100];
int pcount = 0;
JButton bucolor = new JButton();
private Shape arrayShape[] = new Shape[100];
private int index = 0;
Color color;
public Shape[] getarrayShape() {
return arrayShape;
}
public void actionPerformed(ActionEvent e) {
String temp = e.getActionCommand(); //获取按钮上的文字
if (temp.equals("")) { //若无文字
JButton tempbu = (JButton)e.getSource(); //强制向下转型
Color tempco = tempbu.getBackground(); //获取按钮的颜色
g.setColor(tempco); //改变颜色
bucolor.setBackground(tempco); //改变当前颜色显示
color = tempco;
// color
}
else { //若有文字
str = temp;
}
if (str.equals("三角形") || str.equals("多边形")) {
count = 0;
}
}
/**
* Invoked when the mouse button has been clicked (pressed
* and released) on a component.
*/
public void mouseClicked(MouseEvent e) {
if (str.equals("三角形")) {
if (count==0) {
x3 = e.getX();
y3 = e.getY();
g.fillOval(x3-5, y3-5, 10, 10);
count++;
}
else if (count==1) {
x4 = e.getX();
y4 = e.getY();
g.fillOval(x4-5, y4-5, 10, 10);
g.drawLine(x3, y3, x4, y4);
count++;
}
else if (count==2) {
x5 = e.getX();
y5 = e.getY();
g.fillOval(x5-5, y5-5, 10, 10);
g.drawLine(x3, y3, x5, y5);
g.drawLine(x4, y4, x5, y5);
count=0;
Shape tempShape = new Shape("三角形",x3,y3,x4,y4,x5,y5,color);
arrayShape[index++] = tempShape;
}
}
else if (str.equals("多边形")) {
if (count==0) {
x3 = e.getX();
y3 = e.getY();
g.fillOval(x3-5, y3-5, 10, 10);
count++;
px[pcount] = e.getX();
py[pcount] = e.getY();
pcount++;
}
else if (count==1) {
x4 = e.getX();
y4 = e.getY();
g.fillOval(x4-5, y4-5, 10, 10);
g.drawLine(x3, y3, x4, y4);
count++;
px[pcount] = e.getX();
py[pcount] = e.getY();
pcount++;
}
else if (e.getButton()==3) { //点击右键时结束
x5 = e.getX();
y5 = e.getY();
g.fillOval(x5-5, y5-5, 10, 10);
g.drawLine(x4, y4, x5, y5);
g.drawLine(x3, y3, x5, y5);
count=0;
px[pcount] = e.getX();
py[pcount] = e.getY();
int[] tpx = new int[px.length];
int[] tpy = new int[py.length];
System.arraycopy(px,0,tpx,0,px.length);
System.arraycopy(py,0,tpy,0,py.length);
Shape tempShape = new Shape("多边形",tpx,tpy,pcount,color);
arrayShape[index++] = tempShape;
pcount=0;
}
else {
x5 = e.getX();
y5 = e.getY();
g.fillOval(x5-5, y5-5, 10, 10);
g.drawLine(x4, y4, x5, y5);
x4 = x5;
y4 = y5;
count++;
px[pcount] = e.getX();
py[pcount] = e.getY();
pcount++;
}
}
}
/**
* Invoked when a mouse button has been pressed on a component.
*/
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
}
/**
* Invoked when a mouse button has been released on a component.
*/
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
if (str.equals("直线")) { //画线
g.drawLine(x1,y1,x2,y2);
Shape tempShape = new Shape("直线",x1,y1,x2,y2,color);
arrayShape[index++] = tempShape;
}
else if (str.equals("矩形")) { //画矩形
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));
Shape tempShape = new Shape("矩形",x1,y1,x2,y2,color);
arrayShape[index++] = tempShape;
}
else if (str.equals("圆")) { //画圆
g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));
Shape tempShape = new Shape("圆",x1,y1,x2,y2,color);
arrayShape[index++] = tempShape;
}
}
/**
* Invoked when the mouse enters a component.
*/
public void mouseEntered(MouseEvent e) {
}
/**
* Invoked when the mouse exits a component.
*/
public void mouseExited(MouseEvent e) {
}
}
Shape类代码:
public class Shape {
private int x1,y1,x2,y2,x3,y3;
private String name;
private int[] px;
private int[] py;
private int pcount;
private Color color;
//构造直线、矩形、圆
public Shape(String name,int x1,int y1,int x2,int y2,Color color) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
this.name = name;
this.color = color;
}
//构造多边形
public Shape(String name,int[] px,int[] py,int pcount,Color color) {
this.name = name;
this.px = px;
this.py = py;
this.pcount = pcount;
this.color = color;
}
//构造三角形
public Shape(String name,int x1,int y1,int x2,int y2,int x3,int y3,Color color) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
this.x3 = x3;
this.y3 = y3;
this.name = name;
this.color = color;
}
public void drawShape(Graphics g) {
g.setColor(color);
switch (name) {
case ("直线"):{
g.drawLine(x1, y1, x2, y2);
break;
}
case("矩形"):{
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));
break;
}
case("圆"):{
g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));
break;
}
case("三角形"):{
g.fillOval(x1-4, y1-4, 8, 8);
g.fillOval(x2-4, y2-4, 8, 8);
g.fillOval(x3-4, y3-4, 8, 8);
g.drawLine(x1, y1, x2, y2);
g.drawLine(x1, y1, x3, y3);
g.drawLine(x2, y2, x3, y3);
}
case("多边形"):{
for(int i=1;i<=pcount;i++) {
g.fillOval(px[i-1]-4, py[i-1]-4, 8, 8);
g.drawLine(px[i-1], py[i-1], px[i], py[i]);
if (i == pcount) {
g.drawLine(px[0], py[0], px[i], py[i]);
g.fillOval(px[i]-4, py[i]-4, 8, 8);
}
}
}
}
}
}