下面我将分享用Java制作简易画图板的过程。
?
version 1
?
Draw.java
?
import javax.swing.JFrame; /** * * @author yangzhenlin * */ public class Draw extends JFrame { public void initDraw() { this.setTitle("画图板"); this.setSize(600, 500); this.setDefaultCloseOperation(3); this.setVisible(true); /** * 从窗体上获取画布对象 获取窗体在屏幕上占据的区域,这块区域是允许改变颜色的。 */ java.awt.Graphics g = this.getGraphics(); DrawListener dlis = new DrawListener(g); this.addMouseListener(dlis); } }
?
?
DrawListener.java
?
import java.awt.event.MouseEvent; /** * * @author yangzhenlin * */ /** * 画板监听器,实现鼠标监听器接口 */ public class DrawListener implements java.awt.event.MouseListener { // private int x1, x2, y1, y2; private java.awt.Graphics g; public DrawListener(java.awt.Graphics g) { this.g = g; } public void mouseClicked(MouseEvent e) { System.out.println("mouseClicked"); } public void mousePressed(MouseEvent e) { System.out.println("mousePressed"); } public void mouseReleased(MouseEvent e) { System.out.println("mouseReleased"); } public void mouseEntered(MouseEvent e) { System.out.println("mouseEntered"); } public void mouseExited(MouseEvent e) { System.out.println("mouseExited"); } }
?
?
Start.java
?
/** * * @author yangzhenlin * */ public class Start { public static void main(String args[]){ Draw dr =new Draw(); dr.initDraw(); } }
?
?
从 system.out.println中可以看出鼠标各项操作对应的方法。
继承接口 java.awt.event.MouseListener 时,接口中的所有方法必须补全方法体。
?
version 2
?
Draw.java
?
?
import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JRadioButton; import javax.swing.ButtonGroup; import javax.swing.JTextField; import java.awt.Dimension; /** * * @author yangzhenlin * */ public class Draw extends JFrame { /** * 初始化窗体 */ public void initDraw() { this.setTitle("画图板"); this.setSize(800, 600); this.setDefaultCloseOperation(3); this.setLocationRelativeTo(null); /** * 设置画布背景色 */ this.getContentPane().setBackground(java.awt.Color.WHITE); /** * 设置布局 */ FlowLayout layout = new FlowLayout(); this.setLayout(layout); /** * 加组件 */ String texts[] = { "line", "rect", "oval", "arc" ,"triangle"}; ButtonGroup group = new ButtonGroup(); for (int i = 0; i < texts.length; i++) { JRadioButton shapeBtn = new JRadioButton(texts[i]); /** * 设置按钮的动作命令 */ shapeBtn.setActionCommand(texts[i]); /** * 设置按钮大小 */ Dimension dim=new Dimension(140,30); shapeBtn.setPreferredSize(dim); /** * 设置文本居中 */ shapeBtn.setHorizontalAlignment(JTextField.CENTER); /** * 将按钮分组 */ group.add(shapeBtn); /** * 将按钮显示在窗体上 */ this.add(shapeBtn); if (i == 0) { shapeBtn.setSelected(true); } } /** * 设置颜色组选项 */ String colortexts[] = { "black", "blue", "green", "yellow", "orange", "pink", "red" }; ButtonGroup colorgroup = new ButtonGroup(); for (int i = 0; i < colortexts.length; i++) { JRadioButton shapeColorBtn = new JRadioButton(colortexts[i]); /** * 设置按钮的动作命令 */ shapeColorBtn.setActionCommand(colortexts[i]); /** * 设置按钮的大小 */ Dimension dim=new Dimension (100,30); shapeColorBtn.setPreferredSize(dim); /** * 设置文本居中 */ shapeColorBtn.setHorizontalAlignment(JTextField.CENTER); /** * 将按钮分组 */ colorgroup.add(shapeColorBtn); /** * 将按钮显示在窗体上 */ this.add(shapeColorBtn); if (i == 0) { shapeColorBtn.setSelected(true); } } // ------------ /** * 显示窗体 */ this.setVisible(true); // ------------ /** * 从窗体上获取画布对象 */ java.awt.Graphics g = this.getGraphics(); /** * 给窗体加上鼠标监听器对象,创建监听器对象的时候讲画布对象传给监听器 */ DrawListener dlis = new DrawListener(g, group,colorgroup); this.addMouseListener(dlis); } }
?
?
DrawListener.java
?
import java.awt.Color; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; /** * 画布的监听器,实现鼠标监听器接口 * * @author yangzhenlin; * */ public class DrawListener implements MouseListener { private int x1, x2, y1, y2; private java.awt.Graphics g; private javax.swing.ButtonGroup group; private javax.swing.ButtonGroup colorgroup; private String type = "line"; private String colortype="black"; private Color color = Color.RED;// 设置颜色 public DrawListener(java.awt.Graphics g, javax.swing.ButtonGroup group, javax.swing.ButtonGroup colorgroup) { this.g = g; this.group = group; this.colorgroup = colorgroup; } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { /** * 要绘制的时候才需要知道即将绘制的形状和颜色 * 得到按钮组中被选中的按钮 */ javax.swing.ButtonModel bm = group.getSelection(); javax.swing.ButtonModel colorbm =colorgroup.getSelection(); /** * 得到按钮的动作命令,做为要绘制的形状类型 */ type = bm.getActionCommand(); colortype=colorbm.getActionCommand(); if(colortype.equals("black")){ color =Color.BLACK; }else if(colortype.equals("blue")){ color=Color.BLUE; }else if(colortype.equals("green")){ color=Color.GREEN; }else if(colortype.equals("yellow")){ color=Color.YELLOW; }else if(colortype.equals("orange")){ color=Color.ORANGE; }else if(colortype.equals("pink")){ color=Color.PINK; }else if(colortype.equals("red")){ color=Color.RED; } /** * 设置绘制颜色 */ g.setColor(color); x1 = e.getX(); y1 = e.getY(); } public void mouseReleased(MouseEvent e) { x2 = e.getX(); y2 = e.getY(); if (type.equals("line")) { /** * 画直线 */ g.drawLine(x1, y1, x2, y2); }else if (type.equals("rect")) { /** * 画矩形 */ g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2)); } else if (type.equals("oval")) { g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2)); } else if (type.equals("arc")) { g.drawArc(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2), 90, 90); }else if (type.equals("triangle")){ g.drawLine(Math.min(x1,x2), Math.min(y1,y2),Math.max(x1, x2) , Math.max(y1, y2)); g.drawLine(Math.min(x1, x2), Math.min(y1,y2), Math.min(x1,x2),Math.max(y1, y2)); g.drawLine(Math.min(x1, x2), Math.max(y1,y2),Math.max(x1,x2),Math.max(y1, y2)); } } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } }
?
?
Start.java
?
/** * * @author yangzhenlin * */ public class Start { public static void main(String args[]) { Draw dr = new Draw(); dr.initDraw(); } }
?
?
上述代码实现了通过获取图形的类型和颜色、鼠标的点下和松开位置的坐标进而绘制一个图形的程序。
其中,实现鼠标监听器的方法较为关键。
创建单选按钮并分组才可以分别选择多种属性。
另外,通过if-else语句实现了类型与颜色的选择。
?
?
version 3
?
在 version 2 的基础上,实现画边框(draw)、填充(fill)和清除(clear)的选择。
?
Draw.java
?
import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JRadioButton; import javax.swing.ButtonGroup; import javax.swing.JTextField; import java.awt.Dimension; /** * * @author yangzhenlin * */ public class Draw extends JFrame { /** * 初始化窗体 */ public void initDraw() { this.setTitle("画图板——杨振林"); this.setSize(800, 600); this.setDefaultCloseOperation(3); this.setLocationRelativeTo(null); /** * 设置画布背景色 */ this.getContentPane().setBackground(java.awt.Color.WHITE); /** * 设置布局 */ FlowLayout layout = new FlowLayout(); this.setLayout(layout); /** * 加组件 */ String texts[] = { "line", "rect", "oval", "arc", "triangle" }; ButtonGroup group = new ButtonGroup(); for (int i = 0; i < texts.length; i++) { JRadioButton shapeBtn = new JRadioButton(texts[i]); /** * 设置按钮的动作命令 */ shapeBtn.setActionCommand(texts[i]); /** * 设置按钮大小 */ Dimension dim = new Dimension(148, 20); shapeBtn.setPreferredSize(dim); /** * 设置文本居中 */ shapeBtn.setHorizontalAlignment(JTextField.CENTER); /** * 将按钮分组 */ group.add(shapeBtn); /** * 将按钮显示在窗体上 */ this.add(shapeBtn); if (i == 0) { shapeBtn.setSelected(true); } } String typetexts[] = { "draw", "fill", "clear" }; ButtonGroup typegroup = new ButtonGroup(); for (int i = 0; i < typetexts.length; i++) { JRadioButton shapeTypeBtn = new JRadioButton(typetexts[i]); /** * 设置按钮的动作命令 */ shapeTypeBtn.setActionCommand(typetexts[i]); /** * 设置按钮的大小 */ Dimension dim = new Dimension(250, 20); shapeTypeBtn.setPreferredSize(dim); /** * 设置文本居中 */ shapeTypeBtn.setHorizontalAlignment(JTextField.CENTER); /** * 将按钮分组 */ typegroup.add(shapeTypeBtn); /** * 将按钮显示在窗体上 */ this.add(shapeTypeBtn); if (i == 0) { shapeTypeBtn.setSelected(true); } } /** * 设置颜色组选项 */ String colortexts[] = { "black", "blue", "green", "yellow", "orange", "pink", "red" }; ButtonGroup colorgroup = new ButtonGroup(); for (int i = 0; i < colortexts.length; i++) { JRadioButton shapeColorBtn = new JRadioButton(colortexts[i]); /** * 设置按钮的动作命令 */ shapeColorBtn.setActionCommand(colortexts[i]); /** * 设置按钮的大小 */ Dimension dim = new Dimension(104, 20); shapeColorBtn.setPreferredSize(dim); /** * 设置文本居中 */ shapeColorBtn.setHorizontalAlignment(JTextField.CENTER); /** * 将按钮分组 */ colorgroup.add(shapeColorBtn); /** * 将按钮显示在窗体上 */ this.add(shapeColorBtn); if (i == 0) { shapeColorBtn.setSelected(true); } } // ------------ /** * 显示窗体 */ this.setVisible(true); // ------------ /** * 从窗体上获取画布对象 */ java.awt.Graphics g = this.getGraphics(); /** * 给窗体加上鼠标监听器对象,创建监听器对象的时候讲画布对象传给监听器 */ DrawListener dlis = new DrawListener(g, group, colorgroup, typegroup); this.addMouseListener(dlis); } }
?
?
DrawListener.java
?
import java.awt.Color; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.ButtonGroup; import javax.swing.ButtonModel; /** * 画布的监听器,实现鼠标监听器接口 * * @author yangzhenlin; * */ public class DrawListener implements MouseListener { private int x1, x2, y1, y2; private java.awt.Graphics g; private ButtonGroup group; private ButtonGroup colorgroup; private ButtonGroup typegroup; private String type = "line"; private String colortype = "black"; private String typetype = "draw"; private Color color = Color.RED;// 设置颜色 public DrawListener(java.awt.Graphics g, ButtonGroup group, ButtonGroup colorgroup, ButtonGroup typegroup) { this.g = g; this.group = group; this.colorgroup = colorgroup; this.typegroup = typegroup; } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { /** * 要绘制的时候才需要知道即将绘制的形状和颜色 得到按钮组中被选中的按钮 */ ButtonModel bm = group.getSelection(); ButtonModel colorbm = colorgroup.getSelection(); ButtonModel typebm = typegroup.getSelection(); /** * 得到按钮的动作命令,做为要绘制的形状类型 */ type = bm.getActionCommand(); colortype = colorbm.getActionCommand(); typetype = typebm.getActionCommand(); if (colortype.equals("black")) { color = Color.BLACK; } else if (colortype.equals("blue")) { color = Color.BLUE; } else if (colortype.equals("green")) { color = Color.GREEN; } else if (colortype.equals("yellow")) { color = Color.YELLOW; } else if (colortype.equals("orange")) { color = Color.ORANGE; } else if (colortype.equals("pink")) { color = Color.PINK; } else if (colortype.equals("red")) { color = Color.RED; } /** * 设置绘制颜色 */ g.setColor(color); x1 = e.getX(); y1 = e.getY(); } public void mouseReleased(MouseEvent e) { x2 = e.getX(); y2 = e.getY(); if (type.equals("line")) { /** * 画直线 */ g.drawLine(x1, y1, x2, y2); } else if (type.equals("rect")) { /** * 画矩形 */ if (typetype.equals("draw")) { g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2)); } else if (typetype.equals("fill")) { g.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2)); } else if (typetype.equals("clear")) { g.setColor(Color.WHITE); g.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2)); } } else if (type.equals("oval")) { if (typetype.equals("draw")) { g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2)); } else if (typetype.equals("fill")) { g.fillOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2)); } else if (typetype.equals("clear")) { g.setColor(Color.WHITE); g.fillOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2)); } } else if (type.equals("arc")) { if (typetype.equals("draw")) { g.drawArc(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2), 90, 90); } else if (typetype.equals("fill")) { g.fillArc(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2), 90, 90); } else if (typetype.equals("clear")) { g.setColor(Color.WHITE); g.fillArc(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2), 90, 90); } } else if (type.equals("triangle")) { if (typetype.equals("draw")) { g.drawLine(Math.min(x1, x2), Math.min(y1, y2), Math.max(x1, x2), Math.max(y1, y2)); g.drawLine(Math.min(x1, x2), Math.min(y1, y2), Math.min(x1, x2), Math.max(y1, y2)); g.drawLine(Math.min(x1, x2), Math.max(y1, y2), Math.max(x1, x2), Math.max(y1, y2)); } else if (typetype.equals("fill")) { int xPoints[] = { Math.min(x1, x2), Math.min(x1, x2), Math.max(x1, x2) }; int yPoints[] = { Math.min(y1, y2), Math.max(y1, y2), Math.max(y1, y2) }; g.fillPolygon(xPoints, yPoints, 3); } else if (typetype.equals("clear")) { int xPoints[] = { Math.min(x1, x2), Math.min(x1, x2), Math.max(x1, x2) }; int yPoints[] = { Math.min(y1, y2), Math.max(y1, y2), Math.max(y1, y2) }; g.setColor(Color.WHITE); g.fillPolygon(xPoints, yPoints, 3); } } } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } }
?
?
Start.java
?
/** * * @author yangzhenlin * */ public class Start { public static void main(String args[]) { Draw dr = new Draw(); dr.initDraw(); } }
?
?
在实现填充三角形的方法中,采用了 java.awt.Graphics.fillPolygon 方法,该方法是向一串坐标点连接起来的图形内部填充颜色。使用方法前需要分别把横坐标和纵坐标写入xPoints和yPoints数组。
?