Java小程序之高级画板重绘篇I
2016-11-15 23:13
260 查看
Java小程序之高级画板重绘篇I
前言:我们完成的高级画板在最小化后,然后最大化,画板上绘制的图形全都会消失不见了。原因已在前面的博客中解释,这里不再累赘;今天就让我们一起来解决这个小bug吧!
画板重绘思路:
1、定义直线,椭圆,矩形,圆角矩形等类;
2、在绘制图形的时候生成相应的对象;
3、利用容器将对象进行保存;
4、利用重绘方法,将容器中的对象拿出来,进行重绘;
难点:容器中装了各种各样的对象,如何判断取出来的是什么对象?
利用instanceof关键字解决
重绘时如何把颜色和粗细进行重绘?
在保存对象的时候,把画笔的颜色和、粗细一并保存到对象中
附:工具中的图片以上传到我的资源;需要的可以自行下载;
源代码:
Line类:
Rect类:
RonundRect类:
Oval类:
Point类:
DrawListener类:
ButtonListener类:
MyPaint类:
测试类:
好了,现在将我们的高级画板最小化后再最大化,刚才绘制的图形都会还会在哟!
总结:
1、重点在DrawListener类中和MyPaint中,DrawListener中,每绘制一个图形,要new相应的一个对象,并将该对象添加到容器中进行保存;MyPaint中,需要在peanelcenterchild中重写paint方法,利用for循环,挨次将容器中的对象取出来,判断取出来的对象类型,根据对象类型进行图形的重绘
2、容器中返回的类型是Object类型,利用instanceof关键字可以判断该对象是否可以转换成某一对象;如果该对象可以转换成直线类型,那么就将取出来的对象进行直线的重绘;以此类推;
前言:我们完成的高级画板在最小化后,然后最大化,画板上绘制的图形全都会消失不见了。原因已在前面的博客中解释,这里不再累赘;今天就让我们一起来解决这个小bug吧!
画板重绘思路:
1、定义直线,椭圆,矩形,圆角矩形等类;
2、在绘制图形的时候生成相应的对象;
3、利用容器将对象进行保存;
4、利用重绘方法,将容器中的对象拿出来,进行重绘;
难点:容器中装了各种各样的对象,如何判断取出来的是什么对象?
利用instanceof关键字解决
重绘时如何把颜色和粗细进行重绘?
在保存对象的时候,把画笔的颜色和、粗细一并保存到对象中
附:工具中的图片以上传到我的资源;需要的可以自行下载;
源代码:
Line类:
package com.huaxin.zhou; import java.awt.Color; import java.awt.Stroke; public class Line { public int x1, y1, x2, y2; public Color color; public Stroke s; public Line(int x1, int y1, int x2, int y2,Color color,Stroke s){ //保存直线的坐标 this.x1=x1; this.y1=y1; this.x2=x2; this.y2=y2; this.color=color;//保存直线的颜色 this.s=s;//保存直线的粗细 } }
Rect类:
package com.huaxin.zhou; import java.awt.Color; import java.awt.Stroke; public class Rect { public int x1, y1, x2, y2; public Color color; public Stroke s; public Rect(int x1, int y1, int x2, int y2,Color color, Stroke s){ this.x1=x1; this.y1=y1; this.x2=x2; this.y2=y2; this.color=color; this.s=s; } }
RonundRect类:
package com.huaxin.zhou; import java.awt.Color; import java.awt.Stroke; public class RoundRect { public int x1, y1, x2, y2,i,j; public Color color; public Stroke s; public RoundRect(int x1, int y1, int x2, int y2,int i,int j,Color color, Stroke s){ this.x1=x1; this.y1=y1; this.x2=x2; this.y2=y2; this.i=i; this.j=j; this.color=color; this.s=s; } }
Oval类:
package com.huaxin.zhou; import java.awt.Color; import java.awt.Stroke; public class Oval { public int x1, y1, x2, y2; public Color color; public Stroke s; public Oval(int x1, int y1, int x2, int y2,Color color,Stroke s){ this.x1=x1; this.y1=y1; this.x2=x2; this.y2=y2; this.color=color; this.s=s; } }
Point类:
package com.huaxin.zhou; import java.awt.Color; import java.awt.Stroke; public class Point { public int x1, y1; public Color color; public Stroke s; public Point(int x1, int y1,Color color,Stroke s){ this.x1=x1; this.y1=y1; this.color=color; this.s=s; } }
DrawListener类:
package com.huaxin.zhou; import java.awt.AWTException; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Stroke; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.Random; import javax.swing.ButtonGroup; import javax.swing.ButtonModel; public class DrawListener implements MouseListener,MouseMotionListener{ public Graphics2D g; public int x1,x2,y1,y2,old_x,old_y,x4,y4; public ButtonGroup bg; public String command; public ArrayList list; public Random r = new Random(); public boolean flag=true; //持有对方引用 public Mypaint db; public Color color; //重载DrawListener方法 public DrawListener(Graphics g,ButtonGroup bg,Mypaint db1,ArrayList list){ this.g=(Graphics2D )g; this.bg=bg; db=db1; this.list=list; } //鼠标按下时间 public void mousePressed(MouseEvent e) { x1=e.getX(); y1=e.getY(); //确认是哪个按钮按下 ButtonModel bm= bg.getSelection(); command=bm.getActionCommand(); Stroke s1=new BasicStroke(1); g.setStroke(s1); if("pic7".equals(command)){ Stroke s=new BasicStroke(10); g.setStroke(s); } } public void mouseReleased(MouseEvent e) { int x=e.getXOnScreen(); int y=e.getYOnScreen(); x2=e.getX(); y2=e.getY(); //如果是直线按钮按下,画直线 if("pic10".equals(command)){ g.drawLine(x1, y1, x2, y2); Line line = new Line(x1, y1, x2, y2,g.getColor(),g.getStroke());//保存绘制的直线 list.add(line); //如果是矩形按钮按下,画矩形 }else if("pic12".equals(command)){ //矩形的画法的纠正 g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1)); Rect rect = new Rect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1),g.getColor(),g.getStroke()); list.add(rect); }else if("pic14".equals(command)){ //椭圆画法的纠正 g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1)); Oval oval = new Oval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1),g.getColor(),g.getStroke()); list.add(oval); } //画曲线 else if("pic13".equals(command)){ if(flag){ g.drawLine(x1, y1, x2, y2); Line line = new Line(x1, y1, x2, y2,g.getColor(),g.getStroke()); list.add(line); flag=false; //记录第一次按下的x和y的坐标 x4=x1; y4=y1; } else { g.drawLine(old_x,old_y, x2, y2); Line line = new Line(old_x,old_y, x2, y2,g.getColor(),g.getStroke()); list.add(line); } old_x=x2; old_y=y2; } //取色功能 else if("pic4".equals(command)){ try { Robot robot = new Robot(); Rectangle rect = new Rectangle(x,y,1,1); BufferedImage bi=robot.createScreenCapture(rect); int c=bi.getRGB(0, 0); Color bicolor=new Color(c); db.color=bicolor; } catch (AWTException e1) { e1.printStackTrace(); } } //圆角矩形功能 else if("pic15".equals(command)){ g.drawRoundRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1),10, 10); RoundRect roundrect = new RoundRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1),10, 10,g.getColor(),g.getStroke()); list.add(roundrect); } } public void mouseClicked(MouseEvent e) { int count=e.getClickCount(); if("pic13".equals(command) && count==2){ g.drawLine(x4, y4, x2, y2); Line line = new Line(x4, y4, x2, y2,g.getColor(),g.getStroke()); list.add(line); flag=true; } } public void mouseEntered(MouseEvent e) { // 拿到画笔颜色 color=db.color; //设置画笔颜色 g.setColor(color); } public void mouseExited(MouseEvent e) { } public void mouseDragged(MouseEvent e) { //拿到当前的坐标 x2=e.getX(); y2=e.getY(); //铅笔画线 if("pic6".equals(command)){ g.drawLine(x1, y1, x2, y2); Line line = new Line(x1, y1, x2, y2,g.getColor(),g.getStroke()); list.add(line); x1=x2; y1=y2; } //画粗线线 else if("pic7".equals(command)){ g.drawLine(x1, y1, x2, y2); Line line = new Line(x1, y1, x2, y2,g.getColor(),g.getStroke()); list.add(line); x1=x2; y1=y2; } //橡皮擦功能实现 else if("pic2".equals(command)){ g.setColor(Color.white); Stroke bs = new BasicStroke(15); g.setStroke(bs); g.drawLine(x1, y1, x2, y2); Line line =new Line(x1, y1, x2, y2,g.getColor(),g.getStroke()); list.add(line); x1=x2; y1=y2; } //喷桶功能 else if("pic8".equals(command)){ for(int i=0;i<30;i++){ int xp=r.nextInt(21)-10; int yp=r.nextInt(21)-10; g.drawLine(x2+xp, y2+yp, x2+xp, y2+yp); Point point = new Point(x2+xp,y2+yp,g.getColor(),g.getStroke()); list.add(point); } } } public void mouseMoved(MouseEvent arg0) { } }
ButtonListener类:
package com.huaxin.zhou; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; //按钮监听器类 public class ButtonListener implements ActionListener{ //持有对方引用,可以拿到对方的所有属性 public Mypaint db; //常在方法 public ButtonListener(Mypaint db1){ db=db1; } //按钮监听事件的具体实现 public void actionPerformed(ActionEvent e) { JButton bt =(JButton)e.getSource(); Color c=bt.getBackground(); db.color=c; db.btt.setBackground(c); } }
MyPaint类:
package com.huaxin.zhou; import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Stroke; import java.util.ArrayList; import javax.swing.ButtonGroup; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.border.BevelBorder; public class Mypaint extends JFrame{ public ArrayList list = new ArrayList(); public Color color=Color.BLACK; public JButton btt; public void initFrame(){ //设置窗体属性 this.setSize(600,500); this.setDefaultCloseOperation(3); this.setTitle("我的画板"); this.setLocationRelativeTo(null); //给窗体添加主面板 JPanel panel = new JPanel(); //设置窗体布局 panel.setLayout(new BorderLayout()); //添加主面板 this.add(panel); //给窗体添加菜单 JMenuBar bar= new JMenuBar(); panel.add(bar,BorderLayout.NORTH); String []str={"文件","视图","帮助","查看"}; String []str1={"打开文件","查看文件","帮助","查看"}; for(int i=0;i<4;i++){ JMenu menu = new JMenu(str[i]); for(int j=0;j<4;j++){ JMenuItem item = new JMenuItem(str1[j]); menu.add(item); } bar.add(menu); } //添加中间面板 JPanel panelcenter = new JPanel(); panelcenter.setLayout(new FlowLayout(FlowLayout.LEFT,10,10)); panel.setBackground(Color.gray); panel.add(panelcenter); //添加中间面板的子面板 JPanel panelcenterchild = new JPanel(){ //画板重绘 public void paint(Graphics g1) { super.paint(g1); Graphics2D g=(Graphics2D)g1; for (int i = 0; i <list.size(); i++) { //如果容器中使直线对象,则重新绘制直线 if(list.get(i) instanceof Line){ Line line = (Line)list.get(i); g.setColor(line.color); Stroke s =(BasicStroke)(line.s); g.setStroke(s); g.drawLine(line.x1, line.y1, line.x2, line.y2); } //重新绘制矩形 else if(list.get(i) instanceof Rect){ Rect rect = (Rect)list.get(i); g.setColor(rect.color); Stroke s =(BasicStroke)(rect.s); g.setStroke(s); g.drawRect(rect.x1, rect.y1,rect.x2, rect.y2); }//重绘椭圆 else if(list.get(i) instanceof Oval){ Oval oval = (Oval)list.get(i); g.setColor(oval.color); Stroke s =(BasicStroke)(oval.s); g.setStroke(s); g.drawOval(oval.x1, oval.y1, oval.x2, oval.y2); } //喷桶重绘 else if(list.get(i) instanceof Point){ Point point = (Point)list.get(i); g.setColor(point.color); Stroke s =(BasicStroke)(point.s); g.setStroke(s); g.drawLine(point.x1, point.y1,point.x1,point.y1); } //圆角矩形重绘 else if(list.get(i) instanceof RoundRect){ RoundRect rr = (RoundRect)list.get(i); g.setColor(rr.color); Stroke s =(BasicStroke)(rr.s); g.setStroke(s); g.drawRoundRect(rr.x1,rr.y1,rr.x2,rr.y2,rr.i,rr.j); } } } }; panelcenterchild.setBackground(Color.WHITE); panelcenterchild.setPreferredSize(new Dimension(500,330)); panelcenter.add(panelcenterchild); //添加左边面板 JPanel panelleft = new JPanel(); panelleft.setLayout(new FlowLayout(FlowLayout.LEFT,0,0)); panelleft.setBackground(new Color(235,233,238)); panelleft.setPreferredSize(new Dimension(50, 0)); //给左边面板添加按钮 ButtonGroup bg = new ButtonGroup(); for(int i=0;i<16;i++){ //给每个按钮添加四种不同的图片 ImageIcon img1 = new ImageIcon("images/draw"+i+".jpg"); ImageIcon img2 = new ImageIcon("images/draw"+i+"-1"+".jpg"); ImageIcon img3 = new ImageIcon("images/draw"+i+"-2"+".jpg"); ImageIcon img4 = new ImageIcon("images/draw"+i+"-3"+".jpg"); JRadioButton jrb = new JRadioButton(); jrb.setActionCommand("pic"+i); //设置默认选中项 if(i==10){ jrb.setSelected(true); } bg.add(jrb); jrb.setIcon(img1);//设置默认图谱按 jrb.setRolloverIcon(img2);//设置鼠标停留在按钮上的图片 jrb.setPressedIcon(img3);//设置鼠标按下按钮的图谱按 jrb.setSelectedIcon(img4);//设置鼠标选中按钮的图片 jrb.setBorder(null);//设置按钮的边框为空,按钮凹下去效果 panelleft.add(jrb); } //下边面板 JPanel paneldown = new JPanel(); paneldown.setBackground(Color.gray); paneldown.setLayout(null); paneldown.setPreferredSize(new Dimension(0, 80)); //下边面板的子面板 JPanel paneldownchild = new JPanel(); paneldownchild.setLayout(null); paneldownchild.setBounds(15, 10, 300, 60); paneldownchild.setBackground(Color.green); //子面板中的左面板 JPanel downleft = new JPanel(); downleft.setLayout(null); downleft.setBackground(Color.white); downleft.setBounds(0, 0, 60, 60); //子面板中的右面板 JPanel downright = new JPanel(); downright.setBackground(Color.magenta); downright.setLayout(new FlowLayout(FlowLayout.LEFT,0,0)); downright.setBounds(60, 0, 240, 60); //子面板添加到相应的父面板中 panel.add(panelleft,BorderLayout.WEST); panel.add(paneldown, BorderLayout.SOUTH); paneldown.add(paneldownchild); paneldownchild.add(downleft); paneldownchild.add(downright); //面板中添加两个颜色按钮 btt = new JButton(); btt.setBackground(Color.black); btt.setBounds(10, 10, 30, 30); JButton bt1 = new JButton(); bt1.setBackground(Color.white); bt1.setBounds(25, 25, 30, 30); downleft.add(btt); downleft.add(bt1); //设置按钮特效 BevelBorder bb = new BevelBorder(0,Color.gray,Color.white); BevelBorder bb1 = new BevelBorder(1,Color.gray,Color.white); downleft.setBorder(bb); btt.setBorder(bb1); bt1.setBorder(bb1); //给添加右面板颜色按钮 ButtonListener bl= new ButtonListener(this);//按钮监听器 Color colors[] ={Color.BLUE,Color.red,Color.black,Color.ORANGE,Color.gray, Color.CYAN,Color.GREEN,Color.YELLOW,Color.PINK,Color.magenta,new Color(234,45,78),new Color(67,123,9)}; for(int i=0;i<12;i++){ JButton bt3 = new JButton(); bt3.setBackground(colors[i]); bt3.setBorder(bb); bt3.setPreferredSize(new Dimension(40,30)); //给每个按钮添加按钮监听器 bt3.addActionListener(bl); downright.add(bt3); } this.setVisible(true); //拿到白色画板的画笔 Graphics g=panelcenterchild.getGraphics(); DrawListener dl = new DrawListener(g,bg,this,list); //白色画板添加 panelcenterchild.addMouseListener(dl); panelcenterchild.addMouseMotionListener(dl); } }
测试类:
package com.huaxin.zhou; public class Test { //主函数,用来测试 public static void main(String[] args) { Mypaint paint = new Mypaint(); paint.initFrame(); } }
好了,现在将我们的高级画板最小化后再最大化,刚才绘制的图形都会还会在哟!
总结:
1、重点在DrawListener类中和MyPaint中,DrawListener中,每绘制一个图形,要new相应的一个对象,并将该对象添加到容器中进行保存;MyPaint中,需要在peanelcenterchild中重写paint方法,利用for循环,挨次将容器中的对象取出来,判断取出来的对象类型,根据对象类型进行图形的重绘
2、容器中返回的类型是Object类型,利用instanceof关键字可以判断该对象是否可以转换成某一对象;如果该对象可以转换成直线类型,那么就将取出来的对象进行直线的重绘;以此类推;
相关文章推荐
- Java小程序之高级画板重绘篇II
- Java小程序之自定义数组队列的实现(高级画板重绘基础)
- Java小程序之高级画板图形保存篇
- Java小程序之高级画板功能篇I
- Java小程序之高级画板功能篇II
- Java小程序之高级画板UI篇
- java应用——高仿XP画板(三:加上重绘功能)
- java高级编程,JNI的使用。c程序调用java代码
- Java完成的一个“画板”小程序
- JAVA高级视频_IO输入与输出 Java程序与其他进程的数据通讯 学习笔记
- java高级编程,JNI的使用。java代码调用c程序
- eclipse调试java程序的高级技巧
- Java GUI小程序--画板
- 高级Java程序能力要求小集
- Android开发:java程序调用linux命令(高级)
- Java小程序之简易网络画板
- JAVA高级视频_IO输入与输出 Java程序与其他进程的数据通讯 学习笔记
- Java小程序之画板入门
- 谈Java编程高级之程序开发的规范性
- Android开发:java程序调用linux命令(高级)