- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import javax.swing.*;
- public class Painter extends JFrame implements ActionListener {
- private static final long serialVersionUID = 1L;
- private static final int X = 100;
- private static final int Y = 100;
- private static final int WIDTH = 600;
- private static final int HEIGHT = 450;
- private int judge = 0;
- private Drawing itemList[] = new Drawing[5000];
- private int index = 0;
- private Paint drawingArea = new Paint();
- private JToolBar buttonPanel;
- private JButton choices[];
- private JLabel mouseAction ;
- private int R,G,B;
- private Color color = Color.black;
- private float stroke = 1.0f;
- private static String names[] = {
- "铅笔","橡皮擦","清除","直线","空心矩形","实心矩形",
- "空心圆","实心圆","空心椭圆","实心椭圆","调色盒","线条粗细"
- };
- public Painter() {
- createNewItem();
- this.setTitle("画图");
- this.setBounds(X, Y, WIDTH, HEIGHT);
- choices = new JButton[names.length];
- buttonPanel = new JToolBar(JToolBar.HORIZONTAL);
- buttonPanel.setFloatable(false);
- for(int i = 0;i<names.length;i++) {
- choices[i] = new JButton(names[i]);
- buttonPanel.add(choices[i]);
- choices[i].addActionListener(this);
- }
- this.setLayout(new BorderLayout());
- mouseAction = new JLabel();
- add(buttonPanel,BorderLayout.NORTH);
- add(drawingArea,BorderLayout.CENTER);
- add(mouseAction,BorderLayout.SOUTH);
- setVisible(true);
- this.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- }
- public static void main(String[] args) {
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- } catch (Exception e) {
- }// 将界面设置为当前windows风格
- new Painter();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if ((e.getActionCommand()).equals("铅笔")) {
- judge = 0;
- createNewItem();
- repaint();
- }
- if ((e.getActionCommand()).equals("橡皮擦")) {
- judge = 1;
- createNewItem();
- repaint();
- }
- if ((e.getActionCommand()).equals("清除")) {
- index = 0;
- judge = 0;
- createNewItem();
- repaint();// 将有关值设置为初始状态,并且重画
- }
- if ((e.getActionCommand()).equals("直线")) {
- judge = 2;
- createNewItem();
- repaint();
- }
- if ((e.getActionCommand()).equals("空心矩形")) {
- judge = 3;
- createNewItem();
- repaint();
- }
- if ((e.getActionCommand()).equals("实心矩形")) {
- judge = 4;
- createNewItem();
- repaint();
- }
- if ((e.getActionCommand()).equals("空心圆")) {
- judge = 5;
- createNewItem();
- repaint();
- }
- if ((e.getActionCommand()).equals("实心圆")) {
- judge = 6;
- createNewItem();
- repaint();
- }
- if ((e.getActionCommand()).equals("空心椭圆")) {
- judge = 7;
- createNewItem();
- repaint();
- }
- if ((e.getActionCommand()).equals("实心椭圆")) {
- judge = 8;
- createNewItem();
- repaint();
- }
- if ((e.getActionCommand()).equals("调色盒")) {
- this.colorChooser();
- }
- if ((e.getActionCommand()).equals("线条粗细")) {
- this.setStroke();
- }
- }
- public void colorChooser () {
- try {
- color = JColorChooser.showDialog(Painter.this, "Choose a color",
- color);
- R = color.getRed();
- G = color.getGreen();
- B = color.getBlue();
- itemList[index].R = R;
- itemList[index].G = G;
- itemList[index].B = B;
- }catch (NullPointerException e) {
- }
- }
- public void setStroke() {
- try {
- String input = null;
- input = JOptionPane
- .showInputDialog("Please input a float stroke value! ( >0 )");
- stroke = Float.parseFloat(input);
- itemList[index].stroke = stroke;
- }catch (NullPointerException e) {
- }catch(NumberFormatException e) {
- JOptionPane.showMessageDialog(null, "输入值为空", "提示", JOptionPane.ERROR_MESSAGE);
- }
- }
- private void createNewItem() {
- if (judge == 0) {
- itemList[index] = new Pen();
- }
- else if (judge == 1) {
- itemList[index] = new Rubber();
- }
- else if (judge == 2) {
- itemList[index] = new Line();
- }
- else if (judge == 3) {
- itemList[index] = new Rect();
- }
- else if (judge == 4) {
- itemList[index] = new fillRect();
- }
- else if (judge == 5) {
- itemList[index] = new Circle();
- }
- else if (judge == 6) {
- itemList[index] = new fillCircle();
- }
- else if (judge == 7) {
- itemList[index] = new Oval();
- }
- else if (judge == 8) {
- itemList[index] = new fillOval();
- }
- else {
- }
- itemList[index].R = R;
- itemList[index].G = G;
- itemList[index].B = B;
- itemList[index].stroke = stroke;
- }
- private class Paint extends JPanel {
- private static final long serialVersionUID = 1L;
- public Paint() {
- this.setBackground(Color.WHITE);
- setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- addMouseMotionListener(new MouseAdapter() {
- @Override
- public void mouseMoved(MouseEvent e) {
- mouseAction.setText("MouseMoved@ [X:"+e.getX()+"Y:"+e.getY()+"]");
- }
- public void mouseDragged(MouseEvent e) {
- mouseAction.setText("MouseDragged@ [X:"+e.getX()+"Y:"+e.getY()+"]");
- if (judge == 0||judge == 1) {
- itemList[index - 1].sx = itemList[index].ex = itemList[index].sx = e
- .getX();
- itemList[index - 1].sy = itemList[index].ey = itemList[index].sy = e
- .getY();
- index++;
- createNewItem();
- } else {
- itemList[index].ex = e.getX();
- itemList[index].ey = e.getY();
- }
- repaint();
- }
- });
- addMouseListener(new MouseAdapter() {
- @Override
- public void mouseExited(MouseEvent e) {
- mouseAction.setText("MouseExited@ [X:"+e.getX()+"Y:"+e.getY()+"]");
- }
- public void mouseClicked(MouseEvent e) {
- mouseAction.setText("MouseClicked@ [X:"+e.getX()+"Y:"+e.getY()+"]");
- }
- public void mousePressed(MouseEvent e) {
- mouseAction.setText("MousePressed@ [X:"+e.getX()+"Y:"+e.getY()+"]");
- //itemList[index].sx = itemList[index].ex = e.getX();
- //itemList[index].sy = itemList[index].ey = e.getY();
- if (judge == 0||judge == 1) {
- itemList[index].sx = itemList[index].ex = e.getX();
- itemList[index].sy = itemList[index].ey = e.getY();
- index++;
- createNewItem();
- }else {
- itemList[index].sx = itemList[index].ex = e.getX();
- itemList[index].sy = itemList[index].ey = e.getY();
- }
- }
- public void mouseReleased(MouseEvent e) {
- mouseAction.setText("MouseReleased@ [X:"+e.getX()+"Y:"+e.getY()+"]");
- if (judge == 0||judge == 1) {
- itemList[index].sx = e.getX();
- itemList[index].sy = e.getY();
- }
- itemList[index].ex = e.getX();
- itemList[index].ey = e.getY();
- repaint();
- index++;
- createNewItem();
- }
- });
- setVisible(true);
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2d = (Graphics2D) g;
- for(int i=0;i<=index;i++) {
- draw(g2d, itemList[i]);
- }
- /*int j = 0;
- while (j <= index) {
- draw(g2d, itemList[j]);
- j++;
- }*/
- }
- void draw(Graphics2D g2d, Drawing i) {
- i.draw(g2d);// 将画笔传入到各个子类中,用来完成各自的绘图
- }
- }
- }
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Graphics2D;
- public class Drawing {
- int sx, sy, ex, ey;
- int R,G,B;
- float stroke;
- public void draw(Graphics2D g) {
- }
- }
- class Rect extends Drawing {
- public void draw(Graphics2D g) {
- g.setPaint(new Color(R,G,B));
- g.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,
- BasicStroke.JOIN_BEVEL));
- g.drawRect(Math.min(sx, ex), Math.min(sy, ey), Math.abs(sx - ex),
- Math.abs(sy - ey));
- }
- }
- class fillRect extends Drawing {
- public void draw(Graphics2D g) {
- g.setPaint(new Color(R,G,B));
- g.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,
- BasicStroke.JOIN_BEVEL));
- g.fillRect(Math.min(sx, ex), Math.min(sy, ey), Math.abs(sx - ex),
- Math.abs(sy - ey));
- }
- }
- class Oval extends Drawing {
- public void draw(Graphics2D g) {
- g.setPaint(new Color(R,G,B));
- g.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,
- BasicStroke.JOIN_BEVEL));
- g.drawOval(Math.min(sx, ex), Math.min(sy, ey), Math.abs(ex - sx),
- Math.abs(ey - sy));
- }
- }
- class fillOval extends Drawing {
- public void draw(Graphics2D g) {
- g.setPaint(new Color(R,G,B));
- g.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,
- BasicStroke.JOIN_BEVEL));
- g.fillOval(Math.min(sx, ex), Math.min(sy, ey), Math.abs(ex - sx),
- Math.abs(ey - sy));
- }
- }
- class Circle extends Drawing {
- public void draw(Graphics2D g) {
- g.setPaint(new Color(R,G,B));
- g.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,
- BasicStroke.JOIN_BEVEL));
- g.drawOval(Math.min(sx, ex), Math.min(sy, ey), Math.abs(ey - sy),
- Math.abs(ey - sy));
- }
- }
- class fillCircle extends Drawing {
- public void draw(Graphics2D g) {
- g.setPaint(new Color(R,G,B));
- g.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,
- BasicStroke.JOIN_BEVEL));
- g.fillOval(Math.min(sx, ex), Math.min(sy, ey), Math.abs(ex - sx),
- Math.abs(ex - sx));
- }
- }
- class Pen extends Drawing {
- public void draw(Graphics2D g) {
- g.setPaint(new Color(R,G,B));
- g.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,
- BasicStroke.JOIN_BEVEL));
- g.drawLine(sx, sy, ex, ey);
- }
- }
- class Line extends Drawing {
- public void draw(Graphics2D g) {
- g.setPaint(new Color(R,G,B));
- g.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,
- BasicStroke.JOIN_BEVEL));
- g.drawLine(sx, sy, ex, ey);
- }
- }
- class Rubber extends Drawing {
- public void draw(Graphics2D g2d) {
- g2d.setPaint(new Color(255, 255, 255));
- g2d.setStroke(new BasicStroke(stroke + 5, BasicStroke.CAP_ROUND,
- BasicStroke.JOIN_BEVEL));
- g2d.drawLine(sx, sy, ex, ey);
- }
- }