学会了使用按钮监听以及鼠标监听器之后,我们就可以制作一个属于自己的画图板了,以下是我之前制作的一个简易的画图板,功能方面尚还不足,不过已经可以完成铅笔,橡皮,画直线,圆等基本工作了,还可以完成颜色的自由选择,大家不放自己也动手做做吧。
首先是界面类:
Java代码
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; @SuppressWarnings("serial") public class Draw extends JFrame{ public Color color=Color.BLACK;//设置初始颜色 private Color colorselector; private JButton colorselectorbu[]=new JButton[6]; //创建对象时需要new一个对象,否则他的默认值就是NULL private JPanel colorjp=new JPanel(); public static void main(String args[]){ Draw dr=new Draw(); dr.showframe(); } public void showframe(){ JPanel north=new JPanel(); //上方区域,存放颜色按钮以及颜色选择器 JPanel west=new JPanel(); //左边区域,存放图形按钮 JPanel center=new JPanel(); //中间区域,存放画板 JPanel south=new JPanel(); //下面区域,用来显示当前鼠标所在前位置以及所画图形的大小 //-----------------中间区域--------------- center.setLayout(new FlowLayout(FlowLayout.LEFT));//将中间区域的布局设置为左对齐 JPanel drawjp=new JPanel(); drawjp.setBackground(Color.white); drawjp.setPreferredSize(new Dimension(600,500)); center.add(drawjp); //-----------------上方区域---------------- JButton colorchooserbu=new JButton("更多颜色"); colorchooserbu.addActionListener(colorlistener); colorjp.setPreferredSize(new Dimension(120,60)); colorjp.setLayout(new GridLayout(3,6,0,0)); //创建一个颜色组 Color color[]=new Color[]{Color.BLACK,Color.BLUE,Color.CYAN,Color.DARK_GRAY,Color.GRAY,Color.GREEN ,Color.LIGHT_GRAY,Color.GREEN,Color.MAGENTA,Color.ORANGE,Color.PINK,Color.YELLOW}; Dimension d=new Dimension(20,20); for(int i=0;i<color.length;i++){ JButton colorbu=new JButton(); colorbu.setBackground(color[i]); colorbu.setPreferredSize(d); colorjp.add(colorbu); colorbu.addActionListener(listener); } //按钮选择器的自定义颜色组 for(int j=0;j<colorselectorbu.length;j++){ colorselectorbu[j]=new JButton(); colorselectorbu[j].setPreferredSize(d); colorjp.add(colorselectorbu[j]); colorselectorbu[j].addActionListener(listener); } north.add(colorjp); north.add(colorchooserbu); //-----------------左边区域---------------- String img[]=new String[]{"Line", "oval", "rect", "roundrect", "eraser", "fill", "pencil", "select"}; String img1[]=new String[]{"Line1","oval1","rect1","roundrect1","eraser1","fill1", "pencil1","select1"}; ButtonGroup bg=new ButtonGroup(); for(int i=0;i<img.length;i++){ //创建Icon组 ImageIcon shapeicon=new ImageIcon("back/"+img[i]+".jpg"); ImageIcon shapeicon1=new ImageIcon("back/"+img1[i]+".jpg"); JRadioButton shapebu=new JRadioButton(shapeicon); bg.add(shapebu); shapebu.setPressedIcon(shapeicon1); //设置选中时的图案 shapebu.setSelectedIcon(shapeicon1); shapebu.setActionCommand(img[i]); west.add(shapebu); } //---------------下方区域------------- JLabel la1=new JLabel(); JLabel la2=new JLabel(); JLabel la3=new JLabel("600x500像素"); JLabel la4=new JLabel(); south.add(la4); south.add(la1); south.add(la2); south.add(la3); south.setLayout(new GridLayout(1,4,20,20)); north.setPreferredSize(new Dimension(200,80)); west.setPreferredSize(new Dimension(80,200)); south.setPreferredSize(new Dimension(200,20)); north.setBackground(Color.GRAY); west.setBackground(Color.DARK_GRAY); center.setBackground(Color.LIGHT_GRAY); south.setBackground(Color.WHITE); this.setLayout(new BorderLayout()); this.add(north,BorderLayout.NORTH); this.add(west,BorderLayout.WEST); this.add(south,BorderLayout.SOUTH); this.add(center,BorderLayout.CENTER); this.setTitle("画图板复习"); this.setSize(900,700); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(3); this.setVisible(true); //在画板上获取画布权限 Graphics g=drawjp.getGraphics(); DrawListener listener=new DrawListener(g,bg,this,la1,la2); drawjp.addMouseListener(listener); drawjp.addMouseMotionListener(listener); } //添加按钮监听,获取按钮的背景颜色 ActionListener listener=new ActionListener(){ public void actionPerformed(ActionEvent e) { //获取事件源,发生事件的对象 Object obj=e.getSource(); if(obj instanceof JButton){ color=((JButton) obj).getBackground(); } } }; ActionListener colorlistener=new ActionListener(){ //按钮数组累加器 int i = 0; public void actionPerformed(ActionEvent e){ colorselector=JColorChooser.showDialog(null, "颜色选择器", Color.BLACK); colorselectorbu[i].setBackground(colorselector); i++; if(i>=6){ //超出六个按钮,返回0 i=0; } } }; }
下面是监听类:
Java代码
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索new
, awt
, color
, import
, jpanel
, javax
, 区域
, FlowLayout
, 用java画图
JColorChooser
,以便于您获取更多的相关知识。
时间: 2024-11-03 06:20:09