⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paintdraw.java

📁 java小程序 画板的设计 经供参考
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * @(#)PaintDraw.java
 *
 *
 * @author 曾晓辉
 * @version 1.00 2007/7/17
 */
import java.awt.*;
import java.awt.Font;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.io.*;
import com.sun.image.codec.jpeg.*;


public class PaintDraw extends Canvas implements  MouseListener,MouseMotionListener,AdjustmentListener,ItemListener,ActionListener 
{   JFrame frame=new JFrame("");
      
    JPanel JRadioPanel=new JPanel();           //四块面板,单选按钮面板底部south
    JPanel JColorPanel=new JPanel();           //颜色大小面板,西部
    JPanel JDrawPanel=new JPanel();            //绘图面板,中间 
    JPanel JScrollPanel=new JPanel();          //颜色滑动条,东部 
    
 	JScrollBar r1=new JScrollBar(JScrollBar.VERTICAL,0,4,0,255);   //三滑动条值从0~255public JScrollBar(int orientation, int value, int extent,int min,int max)
    JScrollBar g1=new JScrollBar(JScrollBar.VERTICAL,0,4,0,255);
    JScrollBar b1=new JScrollBar(JScrollBar.VERTICAL,0,4,0,255);
	
    private JMenuBar jb=new JMenuBar();	                           //菜单栏
	private JMenu jm1=new JMenu("文件(F)");
	private JMenu jm2=new JMenu("编辑(E)");
	private JMenu jm3=new JMenu("帮助(H)");
	
	public JMenuItem jmi1=new JMenuItem("新建(N)");
	public JMenuItem jmi2=new JMenuItem("打开(O)");
	public JMenuItem jmi3=new JMenuItem("保存(S)");
	public JMenuItem jmi4=new JMenuItem("另存为...");
	public JMenuItem jmi5=new JMenuItem("退出(E)");
	public JMenuItem jmi6=new JMenuItem("剪切(T)");
	public JMenuItem jmi7=new JMenuItem("复制(C)");
	public JMenuItem jmi8=new JMenuItem("粘贴(P)");
    public JMenuItem jmi9=new JMenuItem("旋转(R)");
    public JMenuItem jmi10=new JMenuItem("缩放(B)");
	public JMenuItem jmi11=new JMenuItem("平移(D)");
	public JMenuItem jmi12=new JMenuItem("关于(A)"); 
	
	JButton 调色板,橡皮,clear,截屏;                            //按钮  
	ButtonGroup bg=new ButtonGroup();                         //单选按钮组
	JRadioButton op1=new JRadioButton("任意",true);
	JRadioButton op2=new JRadioButton("直线",false);
	JRadioButton op3=new JRadioButton("圆",false);
	JRadioButton op4=new JRadioButton("矩形",false);
	JRadioButton op5=new JRadioButton("三角形",false);
	JRadioButton op6=new JRadioButton("多边形",false);
	
	JCheckBox jcb=new JCheckBox("填充");         //定义一个单选框,填充的颜色
	BufferedImage image;
	Choice sizeChoice,eraseChoice;                            //两下拉列表,用于选取画笔,橡皮大小
	
	JTextArea tt=new JTextArea(2,3);                           //文本域,初步用于显示选取颜色
  
    Color c=Color.black;                                                   //全局变量定义
    boolean flag=false;
    boolean fill=false;                                    //是否填充 
    int f=1;                                               //f=1,任意,f=2直线,f=3圆,f=4矩形,f=5三角形,f=6多边形
    int sx,sy,x,y;
    public PaintDraw() 
    {   frame.setDefaultLookAndFeelDecorated(true);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setLocationRelativeTo(null);
    	frame.getContentPane().setLayout(new BorderLayout());
        
        jm1.add(jmi1); jm1.add(jmi2); jm1.add(jmi3); jm1.add(jmi4); jm1.add(jmi5); jm2.add(jmi6);
        jm2.add(jmi7); jm2.add(jmi8); jm2.add(jmi9); jm2.add(jmi10); jm2.add(jmi11); jm3.add(jmi12);
        jb.add(jm1);jb.add(jm2);jb.add(jm3);
        jm1.setMnemonic(70);jm2.setMnemonic(69);jm3.setMnemonic(72);
    	jmi1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.ALT_MASK));
    	jmi2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.ALT_MASK));
    	jmi3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.ALT_MASK));
    	jmi5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,ActionEvent.ALT_MASK));
    	jmi6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,ActionEvent.ALT_MASK));
    	jmi7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.ALT_MASK));
    	jmi8.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,ActionEvent.ALT_MASK));
    	jmi9.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,ActionEvent.ALT_MASK));
    	jmi10.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,ActionEvent.ALT_MASK));
    	jmi11.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,ActionEvent.ALT_MASK));
    	jmi12.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,ActionEvent.ALT_MASK));
    	jb.setBackground(new Color(142,192,237));
        jmi1.addActionListener(this);jmi2.addActionListener(this);jmi3.addActionListener(this);jmi4.addActionListener(this);
    	jmi5.addActionListener(this);jmi6.addActionListener(this);jmi7.addActionListener(this);jmi8.addActionListener(this);
    	jmi9.addActionListener(this);jmi10.addActionListener(this);jmi11.addActionListener(this);jmi12.addActionListener(this);
    	frame.setJMenuBar(jb);
    	
    	
    	JToolBar JTB=new JToolBar();
	    JButton jbt1=new JButton(new ImageIcon("new.gif"));
    	/*jbt1.setFont(new Font("楷体",Font.BOLD+Font.CENTER_BASELINE,16));*/
	    JButton jbt2=new JButton(new ImageIcon("register.gif"));
	    JButton jbt3=new JButton(new ImageIcon("save.gif"));
	    JButton jbt4=new JButton("C");
	    jbt4.setFont(new Font("楷体",Font.BOLD+Font.CENTER_BASELINE,16));
	    JButton jbt5=new JButton(new ImageIcon("save.gif"));
	    JButton jbt6=new JButton(new ImageIcon("right.gif"));
	    JButton jbt7=new JButton(new ImageIcon("middle.gif"));
	    JButton jbt8=new JButton(new ImageIcon("left.gif"));
	    JButton jbt9=new JButton(new ImageIcon("sound.gif"));
	    JButton jbt10=new JButton(new ImageIcon("underline.gif"));
	    JButton jbt11=new JButton("L");
	    jbt11.setFont(new Font("楷体",Font.BOLD+Font.CENTER_BASELINE,16));
	    /*jbt1.setToolTipText("adfasdf");*/
    	jbt1.setBorder(BorderFactory.createRaisedBevelBorder());
     	jbt2.setBorder(BorderFactory.createRaisedBevelBorder());
    	jbt3.setBorder(BorderFactory.createRaisedBevelBorder());
    	jbt4.setBorder(BorderFactory.createRaisedBevelBorder());
    	jbt5.setBorder(BorderFactory.createRaisedBevelBorder());
    	jbt6.setBorder(BorderFactory.createRaisedBevelBorder());
    	jbt7.setBorder(BorderFactory.createRaisedBevelBorder());
    	jbt8.setBorder(BorderFactory.createRaisedBevelBorder());
    	jbt9.setBorder(BorderFactory.createRaisedBevelBorder());
    	jbt10.setBorder(BorderFactory.createRaisedBevelBorder());
    	jbt11.setBorder(BorderFactory.createRaisedBevelBorder());
    	JTB.add(jbt1);	JTB.add(jbt2);	JTB.add(jbt3);	JTB.add(jbt4);	JTB.add(jbt5);
    	JTB.add(jbt6);	JTB.add(jbt7);	JTB.add(jbt8);	JTB.add(jbt9);	JTB.add(jbt10);
    	JTB.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
    	frame.getContentPane().add(JTB,BorderLayout.NORTH);
    	
        tt.setEditable(false);tt.setBackground(c);
    	Color c1=new Color(142,192,237);
    	op1.setBackground(c1);	op2.setBackground(c1);	op3.setBackground(c1);
    	op4.setBackground(c1);	op5.setBackground(c1);	op6.setBackground(c1);
    	bg.add(op1);bg.add(op2);bg.add(op3);bg.add(op4);bg.add(op5);bg.add(op6);
    	JRadioPanel.setLayout(new FlowLayout());
    	JRadioPanel.setBackground(c1);
    	JRadioPanel.add(op1);JRadioPanel.add(op2);JRadioPanel.add(op3);JRadioPanel.add(op4);
    	JRadioPanel.add(op5);JRadioPanel.add(op6);JRadioPanel.add(jcb);JRadioPanel.add(tt);
    	frame.getContentPane().add(JRadioPanel,BorderLayout.SOUTH);
    	op1.addItemListener(this);op2.addItemListener(this);op3.addItemListener(this);
        op4.addItemListener(this);op5.addItemListener(this);op6.addItemListener(this);
        
        jcb.addItemListener(this);  
    	
       	JScrollPanel.setLayout(new GridLayout(1,3));
    	r1.addAdjustmentListener(this);	
    	g1.addAdjustmentListener(this);	
    	b1.addAdjustmentListener(this);
    	r1.setBackground(new Color(255,172,179));g1.setBackground(new Color(185,255,199));b1.setBackground(new Color(181,185,255));
    	JScrollPanel.add(r1);JScrollPanel.add(g1);JScrollPanel.add(b1);
    	frame.getContentPane().add(JScrollPanel,BorderLayout.EAST);
    	
       	sizeChoice=new Choice();
    	sizeChoice.add("1");sizeChoice.add("3");sizeChoice.add("5");sizeChoice.add("7");sizeChoice.add("9");
    	eraseChoice=new Choice();
    	eraseChoice.add("5");eraseChoice.add("9");eraseChoice.add("13");eraseChoice.add("17");
    	JButton 橡皮=new JButton("橡皮");
    	JButton 调色板=new JButton("调色板");
       	JButton clear=new JButton("清除");
    	JButton 截屏=new JButton("截屏");
    	JLabel 大小B = new JLabel("画笔大小",JLabel.CENTER);
        JLabel 大小E = new JLabel("橡皮大小",JLabel.CENTER);
    	JColorPanel.setLayout(new GridLayout(8,1));
    	JColorPanel.add(大小B);JColorPanel.add(sizeChoice);JColorPanel.add(大小E);JColorPanel.add(eraseChoice);
    	JColorPanel.add(橡皮);JColorPanel.add(调色板);JColorPanel.add(clear);JColorPanel.add(截屏);
    	JColorPanel.setBackground(c1);
    	frame.getContentPane().add(JColorPanel,BorderLayout.WEST);
    	橡皮.addActionListener(this);
    	调色板.addActionListener(this);
    	clear.addActionListener(this);
    	截屏.addActionListener(this);
    	
    	
    	Canvas can=new Canvas();
    	JDrawPanel.add(can);JDrawPanel.setLayout(null);
    	JDrawPanel.setOpaque(true);                             //设为非透明   
    	JDrawPanel.setBackground(Color.white);
    	frame.getContentPane().add(JDrawPanel,BorderLayout.CENTER);
    	JDrawPanel.addMouseListener(this);
    	JDrawPanel.addMouseMotionListener(this);
    	
    
    	frame.setTitle("简易绘图");
    	frame.setSize(600,450);
    	frame.setVisible(true);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -