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

📄 painter.java

📁 绘图程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.math.*;
import java.io.File;
import javax.imageio.*;
import java.io.*;
import java.awt.image.RenderedImage;
import javax.swing.JFileChooser.*;
public class Painter extends JFrame {

	int shapeFlag;
	int pressX,pressY;    //记录鼠标按下时的坐标
	int oldX,oldY;        //记录上次画的点的位置
	JPanel paintArea;     //画图板
	JTextField content;   //填写的文字内容
	JPanel showColor;     //显示当前选择的颜色
	JPanel showBgColor;   //显示当前选择的背景颜色
	JCheckBox fill;       //显示是否填充
	boolean first;
	boolean firstPaint;
	int size[] = {14,15,16,17,18,19,20,21,22};
	int realSize;         //文字大小
	Color color;          //颜色
	Color Bgcolor;        //背景颜色
	boolean isFill;
	Image imBuffer;       //保存的图片
    JLabel statusBar;            //显示鼠标状态的提示条


	public Painter() {
	    super("Java Painter");
	    setSize(660,500);
	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     //不能最大化
	    setResizable(false);                 //不能拖动窗口大小
	    setVisible(true);

	    shapeFlag = 0;         //记录选的是哪个图形,1为圆,2为椭圆,
	    									//3为直线,4为矩形,5为文字,6为随意画。
	    first = false;
	    firstPaint = true;
	    color = Color.black;   //颜色默认为黑色
	    Bgcolor = Color.white; //背景色默认为白色
	    isFill = false;
    }

    public void setLayout() {
    	Container con = getContentPane();
 		con.setLayout(null);       //主窗口不使用格式
 		Insets insets = con.getInsets();

 		JPanel shape = new JPanel();
 		shape.setBounds(5+insets.left, 105+insets.top,50,300);
 		shape.setLayout(null);
 		con.add(shape);

 		ButtonGroup bg = new ButtonGroup();     //按钮分组

 		Icon imageC = new ImageIcon("Circle.gif");
 		JToggleButton b1 = new JToggleButton(imageC);   //给按钮加入图片
 		b1.setToolTipText("画圆");
 		b1.addActionListener(new Circle());
 		b1.setBounds(insets.left,insets.top,50,50);
 		bg.add(b1);
 		shape.add(b1);
 		Icon imageE = new ImageIcon("Oval.gif");
 		JToggleButton b2 = new JToggleButton(imageE);
 		b2.setToolTipText("画椭圆");
 		b2.addActionListener(new Ecllipse());
 		b2.setBounds(insets.left,50+insets.top,50,50);
 		bg.add(b2);
 		shape.add(b2);
 		Icon imageL = new ImageIcon("Line.gif");
 		JToggleButton b3 = new JToggleButton(imageL);
 		b3.setToolTipText("画直线");
 		b3.addActionListener(new Line());
 		b3.setBounds(insets.left,100+insets.top,50,50);
 		bg.add(b3);
 		shape.add(b3);
 		Icon imageR = new ImageIcon("Rectangle.gif");
 		JToggleButton b4 = new JToggleButton(imageR);
 		b4.setToolTipText("画矩形");
 		b4.addActionListener(new Rectangle());
 		b4.setBounds(insets.left,150+insets.top,50,50);
 		bg.add(b4);
 		shape.add(b4);
 		Icon imageT = new ImageIcon("Word.gif");
 		JToggleButton b5 = new JToggleButton(imageT);
 		b5.setToolTipText("写字(选择字号,然后在绘图板上单击左键)");
 		b5.addActionListener(new Text());
 		b5.setBounds(insets.left,200+insets.top,50,50);
 		bg.add(b5);
 		shape.add(b5);
 		Icon imageA = new ImageIcon("Pen.gif");
  		JToggleButton b6 = new JToggleButton(imageA);   //给按钮加入图片
  		b6.setToolTipText("任意图形");
  		b6.addActionListener(new Pencil());
  		b6.setBounds(insets.left,250+insets.top,50,50);
  		bg.add(b6);
 		shape.add(b6);

 		JMenu[] menus = { new JMenu("File"),                //添加菜单
    					new JMenu("Edit"), new JMenu("Help") };
 		JMenuBar mb = new JMenuBar();
 		for(int i = 0; i < menus.length; i++)
      		mb.add(menus[i]);
     	JMenuItem open = new JMenuItem("open");
      	menus[0].add(open);
        open.addActionListener(                            //点击打开
           new ActionListener(){
                   public void actionPerformed(ActionEvent e)
                   {
                    	Open();		//如果被触发,则调用打开文件函数段//////////////////////////
                   }
           }
    	);
 		setJMenuBar(mb);
      	JMenuItem save = new JMenuItem("save");
      	menus[0].add(save);
      	save.addActionListener(                            //点击保存
           new ActionListener(){
                   public void actionPerformed(ActionEvent e)
                   {
                    	Save();		//如果被触发,则调用打开文件函数段//////////////////////////
                   }
           }
    	);
 		setJMenuBar(mb);
 		menus[0].addSeparator();//将新分隔符追加到菜单的末尾。

 		//退出菜单项
  		JMenuItem exit=new JMenuItem("exit");
    	exit.addActionListener(
           new ActionListener(){
                   public void actionPerformed(ActionEvent e)
                   {
                    System.exit(0);	//如果被触发,则退出画图板程序
                   }
           }
    	);
    	menus[0].add(exit);

	   JMenuItem aboutItem=new JMenuItem("About PaintPad");
   	   aboutItem.addActionListener(
            new ActionListener(){
                    public void actionPerformed(ActionEvent e)
                     {
                      JOptionPane.showMessageDialog(null,
                         "计算机科学与技术  任海奇 0410743 ",
                         " java简单画图板程序说明 ",
                          JOptionPane.INFORMATION_MESSAGE );
                      }
                    }
               );
     menus[2].add(aboutItem);
//////////////////////////////////////////////////////////
 	/*	JLabel label1 = new JLabel("ShapeColor");         //画那些已有文字
 		label1.setBounds(5+insets.left,20+insets.top,80,20);
 		label1.setLayout(null);
 		con.add(label1);
 		JLabel label2 = new JLabel("Background");
 		label2.setBounds(5+insets.left,60+insets.top,80,20);
 		label2.setLayout(null);
 		con.add(label2);
 		*/
 		JLabel label3 = new JLabel("Text");
 		label3.setBounds(310+insets.left,20+insets.top,30,20);
 		label3.setLayout(null);
 		con.add(label3);
 		JLabel label4 = new JLabel("Size");
 		label4.setBounds(310+insets.left,60+insets.top,30,20);
 		label4.setLayout(null);
 		con.add(label4);
 	    statusBar=new JLabel("Welcome To The Java Painter!");
 	    statusBar.setBounds(insets.left,415+insets.top,200,20);
 	    statusBar.setLayout(null);
     	con.add(statusBar);


 		content = new JTextField("Type Text Here");    //画文字内容框
 		content.setBounds(350+insets.left,20+insets.top,300,20);
 		content.setLayout(null);
 		con.add(content);

 		JComboBox textSize = new JComboBox();   //画设定文字大小的ComboBox
 		for(int i = 0; i < size.length; i++)
      	{
      		textSize.addItem(size[i]);
      	}
      	textSize.setBounds(350+insets.left,60+insets.top,100,20);
      	textSize.setLayout(null);
      	textSize.setMaximumRowCount(3);
 		con.add(textSize);
    	textSize.addActionListener(new TS());
    	textSize.addMouseListener(new ClickTS());

    	JButton setColor = new JButton("Shape Color");   //画颜色选择器的按钮
    	setColor.setToolTipText("选择前景色");
    	setColor.setMargin(new Insets(2,2,2,2));
    	setColor.setBounds(5+insets.left,20+insets.top,95,20);
    	setColor.setLayout(null);
    	con.add(setColor);
    	setColor.addActionListener(new SC());

    	showColor = new JPanel();            //画显示颜色的Panel
    	showColor.setBounds(120+insets.left,20+insets.top,50,20);
    	showColor.setLayout(null);
    	showColor.setBackground(Color.black);
    	con.add(showColor);

    	JButton setBackground = new JButton("Background");   //画背景颜色选择器的按钮
    	setBackground.setToolTipText("选择背景色");
    	setBackground.setMargin(new Insets(2,2,2,2));
    	setBackground.setBounds(5+insets.left,60+insets.top,95,20);
    	setBackground.setLayout(null);
    	con.add(setBackground);
    	setBackground.addActionListener(new SBg());

    	showBgColor = new JPanel();            //画显示背景颜色的Panel
    	showBgColor.setBounds(120+insets.left,60+insets.top,50,20);
    	showBgColor.setLayout(null);
    	showBgColor.setBackground(Color.white);
    	con.add(showBgColor);

    	fill = new JCheckBox("Fill the region"); //画是否填充的选项
    	fill.setToolTipText("选择是否填充");
    	fill.setBounds(500+insets.left,60+insets.top,100,20);
    	fill.setLayout(null);
    	con.add(fill);
    	fill.addActionListener(new F());

 		paintArea = new JPanel();      //画图板
 		paintArea.setBounds(70+insets.left,105+insets.top,570,300);
 		paintArea.setLayout(null);
 		paintArea.setBackground(Color.white);
 		con.add(paintArea);
 		paintArea.addMouseListener(new Mouse1());     //鼠标响应
 		paintArea.addMouseMotionListener(new Mouse2());

 		imBuffer = this.createImage(paintArea.getWidth(),paintArea.getHeight());

 		try{
 			Graphics gBuffer = imBuffer.getGraphics();
 			gBuffer.setColor(Color.white);
 			gBuffer.fillRect(0,0,paintArea.getWidth(),paintArea.getHeight());
 		}
 		catch(Exception e1){
 			    System.out.println("ERROR!");
	 	}
	}

 	public void paint(Graphics g)
 	{
 		super.paint(g);

 		if(!firstPaint)                   //重画
 		{
 			Graphics gPaintArea = paintArea.getGraphics();
 			gPaintArea.drawImage(imBuffer,0,0,this);
 		}

 		firstPaint = false;
 	}

 	class Circle implements ActionListener {
 		public void actionPerformed(ActionEvent e) {
 			shapeFlag = 1;
 		}
 	}

 	class Ecllipse implements ActionListener {
 		public void actionPerformed(ActionEvent e) {
 			shapeFlag = 2;
 		}
 	}

 	class Line implements ActionListener {
 		public void actionPerformed(ActionEvent e) {
 			shapeFlag = 3;
 		}
 	}

 	class Rectangle implements ActionListener {
 		public void actionPerformed(ActionEvent e) {
 			shapeFlag = 4;
 		}
 	}

 	class Text implements ActionListener {
 		public void actionPerformed(ActionEvent e) {
 			shapeFlag = 5;
 		}
 	}
  	class Pencil implements ActionListener {
  		public void actionPerformed(ActionEvent e) {
  			shapeFlag = 6;
  		}
 	}

 	class TS implements ActionListener {            //设置文字大小的响应//////////////////
 		public void actionPerformed(ActionEvent e){
 			int index = ((JComboBox)e.getSource()).getSelectedIndex();
 			realSize = size[index];
 	//		repaint();

    	}
    }

 	class ClickTS extends MouseAdapter {            //设置文字大小的响应//////////////////
 		public void mousePressed(MouseEvent e) {
 	//		repaint();
    	}

⌨️ 快捷键说明

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