下面是一些相关代码:
//******************左边面板添加形状选择工具**************// //添加图片及按钮 ButtonGroup group =new ButtonGroup(); String[] strs ={ "0", "1", "easer", "3", "4", "5", "pencial", "brush", "spray", "9", "line", "11", "rect", "polygon", "oval", "roundrect"}; for(int i=0;i<16;i++){ JRadioButton btn1 =new JRadioButton(); left.add(btn1); group.add(btn1); btn1.setActionCommand(strs[i]); btn1.setSelected(true); ImageIcon defaultIcon = new ImageIcon("img/draw"+i+".jpg"); btn1.setIcon(defaultIcon); ImageIcon rolloverIcon =new ImageIcon("img/draw"+i+"-1.jpg"); btn1.setRolloverIcon(rolloverIcon); ImageIcon pressedIcon = new ImageIcon("img/draw"+i+"-2.jpg"); btn1.setPressedIcon(pressedIcon); ImageIcon selectedIcon =new ImageIcon("img/draw"+i+"-3.jpg"); btn1.setSelectedIcon(selectedIcon); }
//******************中间面板添加****************// JPanel drawpJPanel=new JPanel(); Dimension drawDim=new Dimension(400,300); drawpJPanel.setPreferredSize(drawDim); drawpJPanel.setBackground(Color.WHITE); FlowLayout f1=new FlowLayout(FlowLayout.LEFT); center.setLayout(f1); center.add(drawpJPanel);
//*******************底部面板添加颜色选择工具******// JLabel frontLabel =new JLabel(); JLabel backLabel=new JLabel(); foot.setLayout(null); frontLabel.setBounds(50, 20, 40, 60); backLabel.setBounds(110, 20, 40, 60); frontLabel.setBackground(Color.BLACK); backLabel.setBackground(Color.BLUE); frontLabel.setOpaque(true); backLabel.setOpaque(true); foot.add(frontLabel); foot.add(backLabel); //颜色 Color[] cs={Color.BLACK, Color.GRAY, new Color(128, 0, 0),Color.RED, new Color(255, 128, 0), Color.YELLOW, Color.GREEN, new Color(0, 128, 255), Color.BLUE, Color.MAGENTA, new Color(255,128,128), new Color(128, 0, 125), new Color(128, 255, 0), new Color(128, 0, 255), new Color(0, 128, 120),new Color(128, 0, 20), new Color(128, 128, 0), new Color(100, 128, 255), new Color(128, 30, 50), new Color(128, 110, 50)}; ColorListener mlis =new ColorListener(frontLabel,backLabel); for(int i=0;i<20;i++){ JLabel colorLabel= new JLabel(); colorLabel.addMouseListener(mlis); if(i<10){ colorLabel.setBounds(160+30*i, 20, 25, 25); }else{ colorLabel.setBounds(160+30*(i-10), 50, 25, 25); } colorLabel.setBackground(cs[i]); colorLabel.setOpaque(true); foot.add(colorLabel); } //添加监听器 DrawListener dlis = new DrawListener(drawpJPanel,group,frontLabel,backLabel); drawpJPanel.addMouseListener(dlis); drawpJPanel.addMouseMotionListener(dlis);
//添加菜单栏 private static JMenuBar craeteMenu() { JMenuBar jmb=new JMenuBar(); String[] array={"文件","颜色","编辑","图像","帮助"}; String[][] array2={{"新建", "保存", "打开", "退出"},{"编辑颜色"},{"字体"},{"修改"},{"关于画图"}}; for(int i=0;i
//直线、矩形、椭圆、圆角矩形、多边形的绘制 public void mouseReleased(MouseEvent e){ x2=e.getX(); y2=e.getY(); int x=x1
//刷子、橡皮擦、 铅笔、 喷枪的绘制 public void mouseDragged(MouseEvent e){ x2=e.getX(); y2=e.getY(); //刷子 if(str.equals("brush")){ Graphics2D g2d=(Graphics2D)g; g2d.setStroke(new BasicStroke(8)); g2d.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; g2d.setStroke(new BasicStroke(1)); } //橡皮擦 else if(str.equals("easer")){ g.setColor(Color.WHITE); Graphics2D g2d=(Graphics2D)g; g2d.setStroke(new BasicStroke(8)); g2d.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; g2d.setStroke(new BasicStroke(1)); } //铅笔 else if(str.equals("pencial")){ g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } //喷枪 else if(str.equals("spray")){ Random random=new Random(); for(int i=0;i<30;i++){ int x=random.nextInt(10); int y=random.nextInt(10); g.drawLine(x2+x, y2+y, x2+x, y2+y); x1=x2; y1=y2; } } }
//鼠标左右键选取颜色 public void mouseReleased(MouseEvent e) { JLabel label=(JLabel)e.getSource(); Color c=label.getBackground(); int num = e.getButton(); if(num==1){ frontLabel.setBackground(c); }else if(num==3){ backLabel.setBackground(c); } }
但是还有一些按钮的功能没有实现:虚线五角星,虚线矩形,油漆,取色器,放大镜,写字,以及曲线;菜单栏按钮的功能没有实现;还有将画图板放大后,图像会消失,这个也没有解决。以后将会慢慢把它补充完整。
附上完整代码(求改进):
package com.newer.yt.draw.copy; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.ButtonGroup; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JRadioButton; /** * 主函数 * @author leaf-stop * */ public class DrawUI{ public static void main(String[] args) { DrawUI dui=new DrawUI(); dui.shoeUI(); } /** * 画图板主窗口 * 画图板布局及各个板块存放的东西 */ public void shoeUI(){ // 创建窗体 JFrame jf = new JFrame(); jf.setTitle("画图板"); jf.setSize(600,500); jf.setDefaultCloseOperation(3); //指定布局为边框布局 BorderLayout layout = new BorderLayout(); jf.setLayout(layout); //左边布局 JPanel left =new JPanel(); Dimension leftDim=new Dimension(100,1); left.setPreferredSize(leftDim); //中间布局 JPanel center =new JPanel(); center.setBackground(Color.GRAY); //底部布局 JPanel foot =new JPanel(); Dimension footdim=new Dimension(1,100);// foot.setPreferredSize(footdim); //设置位置 jf.add(left,BorderLayout.WEST); jf.add(center,BorderLayout.CENTER); jf.add(foot,BorderLayout.SOUTH); jf.setJMenuBar(craeteMenu()); //******************左边面板添加形状选择工具***************************************// //添加图片及按钮 ButtonGroup group =new ButtonGroup(); //定义一个一维数组来存放工具栏图片的名字,数字表示还没有实现的按钮 String[] strs ={ "0", "1", "easer", "3", "4", "5", "pencial", "brush", "spray", "9", "line", "11", "rect", "polygon", "oval", "roundrect"}; for(int i=0;i<16;i++){ JRadioButton btn1 =new JRadioButton(); left.add(btn1); group.add(btn1); btn1.setActionCommand(strs[i]); btn1.setSelected(true); // 设置按钮的图片 ImageIcon defaultIcon = new ImageIcon("img/draw"+i+".jpg"); btn1.setIcon(defaultIcon); ImageIcon rolloverIcon =new ImageIcon("img/draw"+i+"-1.jpg"); btn1.setRolloverIcon(rolloverIcon); ImageIcon pressedIcon = new ImageIcon("img/draw"+i+"-2.jpg"); btn1.setPressedIcon(pressedIcon); ImageIcon selectedIcon =new ImageIcon("img/draw"+i+"-3.jpg"); btn1.setSelectedIcon(selectedIcon); } //******************中间面板添加***********************************************// JPanel drawpJPanel=new JPanel(); Dimension drawDim=new Dimension(400,300); drawpJPanel.setPreferredSize(drawDim); drawpJPanel.setBackground(Color.WHITE); // 面板默认的布局是流式布局 // 指定中间面板的布局为流式布局左对齐 FlowLayout f1=new FlowLayout(FlowLayout.LEFT); center.setLayout(f1); center.add(drawpJPanel); //*******************底部面板添加颜色选择工具****************************************// JLabel frontLabel =new JLabel(); JLabel backLabel=new JLabel(); // 使用绝对定位布局 foot.setLayout(null); frontLabel.setBounds(50, 20, 40, 60); backLabel.setBounds(110, 20, 40, 60); frontLabel.setBackground(Color.BLACK); backLabel.setBackground(Color.BLUE); // 是否允许背景色显示出来 frontLabel.setOpaque(true); backLabel.setOpaque(true); foot.add(frontLabel); foot.add(backLabel); //颜色 Color[] cs={Color.BLACK, Color.GRAY, new Color(128, 0, 0),Color.RED, new Color(255, 128, 0), Color.YELLOW, Color.GREEN, new Color(0, 128, 255), Color.BLUE, Color.MAGENTA, new Color(255,128,128), new Color(128, 0, 125), new Color(128, 255, 0), new Color(128, 0, 255), new Color(0, 128, 120),new Color(128, 0, 20), new Color(128, 128, 0), new Color(100, 128, 255), new Color(128, 30, 50), new Color(128, 110, 50)}; ColorListener mlis =new ColorListener(frontLabel,backLabel); for(int i=0;i<20;i++){ JLabel colorLabel= new JLabel(); // 给颜色Label添加鼠标监听器 colorLabel.addMouseListener(mlis); if(i<10){ colorLabel.setBounds(160+30*i, 20, 25, 25); }else{ colorLabel.setBounds(160+30*(i-10), 50, 25, 25); } colorLabel.setBackground(cs[i]); colorLabel.setOpaque(true); foot.add(colorLabel); } jf.setVisible(true); //调用创建监听器的方法,并传入参数 DrawListener dlis = new DrawListener(drawpJPanel,group,frontLabel,backLabel); drawpJPanel.addMouseListener(dlis); drawpJPanel.addMouseMotionListener(dlis); } //*******************菜单栏的实现****************************************// /** * 创建菜单栏 * @return 返回JMenuBar的对象 */ private static JMenuBar craeteMenu() { //实例化一个JMenuBar类的对象jmb JMenuBar jmb=new JMenuBar(); String[] array={"文件","颜色","编辑","图像","帮助"}; String[][] array2={{"新建", "保存", "打开", "退出"},{"编辑颜色"},{"字体"},{"修改"},{"关于画图"}}; for(int i=0;i