JPaint.java
上传用户:sandywf
上传日期:2022-08-10
资源大小:300k
文件大小:50k
- /*
- * 简易画图程序
- *
- * Filename: JavaPainting.java
- * Author: Keven
- * Date: 2008-12
- */
- import java.io.*;
- import java.awt.*;
- import java.awt.font.*;
- import java.awt.geom.*;
- import java.util.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.filechooser.FileFilter;
- /*
- * 类名:Shape
- * 功能描述:所画形状的基类,所有所画形状都由此类继承.
- * 包括形状类型枚举常量,起始终点坐标,画线宽度,颜色,是否填满,是否被选中等
- * 以及一些方法,如给定起点终点坐标,得到起点终点坐标,得到起始终点坐标中的最大值,读写操作等
- */
- class Shape {
- public static final int LINE = 1;
- public static final int SQUARE = 2;
- public static final int ELLIPSE = 3;
- public static final int ROUND = 4;
- public static final int PEN = 5;
- public static final int ERASER = 6;
- public static final int ACUTESHAPE = 7;
- public static final int WORD = 8;
- public static final int ACUTELINE = 9;
- public static final int ACUTEELLIPSE = 10;
- public static final int ACUTEROUND = 11;
- public static final int ACUTESQUARE = 12;
- public float PENSTROKE = 3.0f;
- public float ERASERSTROKE = 10.0f;
- protected int startX;
- protected int startY;
- protected int endX;
- protected int endY; // 起始终点坐标
- protected float stroke = 1.0f; // 定义线条粗细属性
- protected int type; // 定义字体属性
- protected String s1;
- protected String s2; // 定义字体风格属性
- protected boolean isFill;
- protected boolean isSelected;
- protected Color color;
- protected int shape;
- public Shape() {
- isFill = false;
- // 图形是否是填充
- isSelected = false;
- // 图形是否被选中
- color = Color.BLACK;
- // 默认颜色黑
- }
- public Shape(int sx, int sy, int ex, int ey) {
- this();
- startX = sx;
- startY = sy;
- endX = ex;
- endY = ey;
- }
- public void setStart(int x, int y) {
- startX = x;
- startY = y;
- }
- public void setEnd(int x, int y) {
- endX = x;
- endY = y;
- }
- public Point2D getStart() {
- return new Point2D.Double(startX, startY);
- }
- public Point2D getEnd() {
- return new Point2D.Double(endX, endY);
- }
- protected int maxX() {
- return (startX >= endX) ? startX : endY;
- }
- protected int minX() {
- return (startX <= endX) ? startX : endX;
- }
- protected int maxY() {
- return (startY >= endY) ? startY : endY;
- }
- protected int minY() {
- return (startY <= endY) ? startY : endY;
- }
- public void setStroke(float x) {
- stroke = x;
- }
- public void getStroke(float x) {
- stroke = x;
- }
- public boolean isContain(int x, int y) {
- if (x >= minX() && x <= maxX() && y >= minY() && y <= maxY())
- return true;
- else
- return false;
- }
- public void writeData(PrintWriter out) throws IOException {
- int fill, rgb;
- rgb = color.getRGB();
- fill = isFill ? 1 : 0;
- out.println(startX + "|" + startY + "|" + endX + "|" + endY + "|"
- + fill + "|" + rgb + "|" + shape);
- }
- public void readData(BufferedReader in) throws IOException {
- String s = in.readLine();
- StringTokenizer t = new StringTokenizer(s, "|");
- startX = Integer.parseInt(t.nextToken());
- startY = Integer.parseInt(t.nextToken());
- endX = Integer.parseInt(t.nextToken());
- endY = Integer.parseInt(t.nextToken());
- isFill = (Integer.parseInt(t.nextToken()) == 1) ? true : false;
- color = new Color(Integer.parseInt(t.nextToken()));
- shape = Integer.parseInt(t.nextToken());
- isSelected = false;
- }
- }
- /*
- * 类名:直线 描述:用来构造直线,继承基类坐标构建方法 是否包含的属性中对直线做矩形对角线处理
- */
- class Line extends Shape {
- public Line() {
- super();
- shape = LINE;
- }
- public Line(int sx, int sy, int ex, int ey) {
- super(sx, sy, ex, ey);
- shape = LINE;
- }
- public boolean isContain(int x, int y) {
- double c = Math.sqrt((endX - startX) * (endX - startX)
- + (endY - startY) * (endY - startY));
- double c1 = Math.sqrt((x - startX) * (x - startX) + (y - startY)
- * (y - startY));
- double c2 = Math
- .sqrt((x - endX) * (x - endX) + (y - endY) * (y - endY));
- if (c1 + c2 - c <= 0.25 * c)
- return true;
- else
- return false;
- }
- }
- /*
- * 类名:矩形 描述:此类用于构建矩形,继承基类坐标构建方法 是否包含的属性中看点是否在矩形内部
- */
- class Square extends Shape {
- public Square() {
- super();
- shape = SQUARE;
- }
- public Square(int sx, int sy, int ex, int ey) {
- super(sx, sy, ex, ey);
- shape = SQUARE;
- }
- public boolean isContain(int x, int y) {
- if (x >= minX() && x <= maxX() && y >= minY() && y <= maxY())
- return true;
- else
- return false;
- }
- }
- /*
- * 类名:椭圆 描述:此类用于构建椭圆,继承基类坐标构建方法 是否包含的属性中看椭圆是否在外切矩形中
- */
- class Ellipse extends Shape {
- public Ellipse() {
- super();
- shape = ELLIPSE;
- }
- public Ellipse(int sx, int sy, int ex, int ey) {
- super(sx, sy, ex, ey);
- shape = ELLIPSE;
- }
- public boolean isContain(int x, int y) {
- double dx = (double) (startX + endX) / 2;
- double dy = (double) (startY + endY) / 2;
- double a = (maxX() - minX()) / 2;
- double b = (maxY() - minY()) / 2;
- double x1 = (x - dx) * (x - dx) / (a * a);
- double y1 = (y - dy) * (y - dy) / (b * b);
- if (x1 + y1 <= 1)
- return true;
- else
- return false;
- }
- }
- /*
- * 类名:圆 描述:此类用于构建圆,继承基类坐标构建方法,是否包含的属性中与椭圆类似 采用椭圆画法,将椭圆长短轴统一
- */
- class Round extends Shape {
- public Round() {
- super();
- shape = ROUND;
- }
- public Round(int sx, int sy, int ex, int ey) {
- super(sx, sy, ex, ey);
- shape = ROUND;
- }
- public boolean isContain(int x, int y) {
- double dx = (double) (startX + endX) / 2;
- double dy = (double) (startY + endY) / 2;
- double a = (maxX() - minX()) / 2;
- double b = (maxY() - minY()) / 2;
- double x1 = (x - dx) * (x - dx) / (a * a);
- double y1 = (y - dy) * (y - dy) / (b * b);
- if (x1 + y1 <= 1)
- return true;
- else
- return false;
- }
- public void change() {
- int temp;
- if (startX > endX) {
- temp = startX;
- startX = endX;
- endX = temp;
- }
- if (startY > endY) {
- temp = startY;
- startY = endY;
- endY = temp;
- }
- if ((endX - startX) > (endY - startY)) {
- endY = startY + endX - startX;
- } else {
- endX = startX + endY - startY;
- }
- }
- public Point2D getStart() {
- return new Point2D.Double(startX, startY);
- }
- public Point2D getEnd() {
- if (startX > endX && startY > endY) {
- if ((endX - startX) < (endY - startY)) {
- endY = startY + endX - startX;
- } else {
- endX = startX + endY - startY;
- }
- } else if (startX < endX && startY < endY) {
- if ((endX - startX) < (endY - startY)) {
- endX = startX + endY - startY;
- } else {
- endY = startY + endX - startX;
- }
- } else if (startX > endX && startY < endY) {
- if ((startX - endX) < (endY - startY)) {
- endX = startX - endY + startY;
- } else {
- endY = startY + startX - endX;
- }
- } else if (startX < endX && startY > endY) {
- if ((endX - startX) < (startY - endY)) {
- endX = startX + startY - endY;
- } else {
- endY = startY - startX + endX;
- }
- }
- return new Point2D.Double(endX, endY);
- }
- }
- /*
- * 类名:画笔 描述:此类用于构建画笔,继承基类坐标构建方法 重定义基类中的线宽属性
- */
- class Pen extends Shape {
- public Pen() {
- super();
- shape = PEN;
- }
- public Pen(int sx, int sy, int ex, int ey) {
- super(sx, sy, ex, ey);
- shape = PEN;
- }
- public float stroke = PENSTROKE;
- }
- /*
- * 类名:橡皮擦 描述:此类用于构建橡皮擦,继承基类坐标构建方法 重定义基类中的线宽属性
- */
- class Eraser extends Shape {
- public Eraser() {
- super();
- shape = ERASER;
- }
- public Eraser(int sx, int sy, int ex, int ey) {
- super(sx, sy, ex, ey);
- shape = ERASER;
- }
- public float stroke = ERASERSTROKE;
- }
- /*
- * 类名:确定坐标图形 描述:此类用于构建确定坐标图形,继承基类坐标构建方法 给出点的坐标,按类型精确画图
- */
- class Acuteshape extends Shape {
- public Acuteshape() {
- super();
- shape = ACUTESHAPE;
- }
- public Acuteshape(int sx, int sy, int ex, int ey) {
- super(sx, sy, ex, ey);
- shape = ACUTESHAPE;
- }
- public void setStart(int x, int y) {
- startX = x;
- startY = y;
- }
- public void setEnd(int x, int y) {
- endX = x;
- endY = y;
- }
- }
- class Word extends Shape {
- public Word() {
- super();
- shape = WORD;
- }
- public Word(int sx, int sy, int ex, int ey) {
- super(sx, sy, ex, ey);
- shape = WORD;
- }
- protected String s1;
- protected String s2 = "Arial";
- }
- /*
- * 类名:按钮面板 描述:用于构建画图板上所有按钮,每个按钮均有响应注释
- */
- class ButtonPanel extends JPanel {
- JButton penButton = new JButton("Pen");
- JButton eraserButton = new JButton("Eraser");
- JButton cusorButton = new JButton("Move");
- JButton lineButton = new JButton("Line");
- JButton squareButton = new JButton("Square");
- JButton ellipseButton = new JButton("Ellipse");
- JButton roundButton = new JButton("Round");
- JButton colorButton = new JButton("Color");
- JButton fillButton = new JButton("Fill");
- JButton drawButton = new JButton("Draw");
- JButton setStrokeButton = new JButton("Stroke");
- JButton acuteButton = new JButton("AccutePaint");
- JButton wordButton = new JButton("Word");
- JButton wordSetting = new JButton("FontSetting");
- public ButtonPanel() {
- setLayout(new GridLayout(2, 6));
- penButton.setToolTipText("Drawing with a pen");
- eraserButton.setToolTipText("Use eraser to wipe out");
- cusorButton.setToolTipText("Move the shape");
- lineButton.setToolTipText("Draw a line");
- squareButton.setToolTipText("Draw a rectangle");
- ellipseButton.setToolTipText("Draw a ecllipse");
- roundButton.setToolTipText("Draw a round");
- colorButton.setToolTipText("Choose the draw color");
- fillButton.setToolTipText("Fill shape");
- drawButton.setToolTipText("Draw shape");
- setStrokeButton.setToolTipText("Set line stroke");
- acuteButton.setToolTipText("draw an accurte paint");
- wordButton.setToolTipText("Input a word");
- wordSetting.setToolTipText("Setting word style");
- add(penButton);
- add(eraserButton);
- add(cusorButton);
- add(lineButton);
- add(squareButton);
- add(ellipseButton);
- add(roundButton);
- add(colorButton);
- add(fillButton);
- add(drawButton);
- add(setStrokeButton);
- add(acuteButton);
- add(wordButton);
- add(wordSetting);
- }
- }
- /*
- * 类名:画图面板 描述:包含当前所画图形的所有信息和动作监听器,用于画图
- */
- class PaintPanel extends JPanel {
- private int currentShape; // 当前形状
- private boolean currentFill; // 当前图形填充状态
- static float currentstroke; // 当前线宽
- static float penstroke;
- static float eraserstroke;
- private Color currentColor; // 当前前景色
- private Color currentBackColor;// 当前背景色
- private boolean paintEnabled; // 画图使能
- private ArrayList polygon; // 图形容器
- private int moveX, moveY; // 移动后的坐标
- private int currentX, currentY; // 当前坐标
- private int f1, f2; // 用来存放当前字体风格
- public static String style1;// 用来存放当前字体
- public static int num;// 当前字体大小
- public static int acutesx,acutesy;
- public static int acuteex,acuteey;
-
- public PaintPanel() {
- paintEnabled = false;
- currentFill = false;
- currentstroke = 2.0f;
- penstroke = 3.0f;
- eraserstroke = 10.0f;
- currentColor = Color.BLACK;
- currentBackColor = Color.WHITE;
- style1 = "Arial";
- num = 30;// 初始化各量
- polygon = new ArrayList(25);
- setLayout(new BorderLayout());
- ButtonPanel buttonPanel = new ButtonPanel();// 创建按钮对象
- add(buttonPanel, BorderLayout.NORTH);
- // 按钮面板置于上部
- add(new LabelPanel(), BorderLayout.SOUTH);
- // 标签栏置于底部
- addMouseListener(new MouseHandler());
- addMouseMotionListener(new MouseMotionHandler());
- // 鼠标监听器
- /*
- * 以下语句用于监听各个按钮动作,改变响应相应的值,如当前形状
- */
- buttonPanel.cusorButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- paintEnabled = false;
- setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
- }
- });
- buttonPanel.penButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- paintEnabled = true;
- setCursor(Cursor.getDefaultCursor());
- currentShape = Shape.PEN;
- }
- });
- buttonPanel.eraserButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- paintEnabled = true;
- setCursor(Cursor.getDefaultCursor());
- currentShape = Shape.ERASER;
- }
- });
- buttonPanel.lineButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- paintEnabled = true;
- setCursor(Cursor.getDefaultCursor());
- currentShape = Shape.LINE;
- }
- });
- buttonPanel.squareButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- paintEnabled = true;
- setCursor(Cursor.getDefaultCursor());
- currentShape = Shape.SQUARE;
- }
- });
- buttonPanel.ellipseButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- paintEnabled = true;
- setCursor(Cursor.getDefaultCursor());
- currentShape = Shape.ELLIPSE;
- }
- });
- buttonPanel.roundButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- paintEnabled = true;
- setCursor(Cursor.getDefaultCursor());
- currentShape = Shape.ROUND;
- }
- });
- buttonPanel.fillButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- paintEnabled = true;
- setCursor(Cursor.getDefaultCursor());
- currentFill = true;
- }
- });
- buttonPanel.drawButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- paintEnabled = true;
- setCursor(Cursor.getDefaultCursor());
- currentFill = false;
- }
- });
- buttonPanel.colorButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- Color defaultColor = Color.BLACK;
- Color selected = JColorChooser.showDialog(PaintPanel.this,
- "Change Color", defaultColor);
- currentColor = selected;
- }
- });
- buttonPanel.wordButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- paintEnabled = true;
- setCursor(Cursor.getDefaultCursor());
- currentShape = Shape.WORD;
- }
- });
- buttonPanel.setStrokeButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- StrokeFrame stroke = new StrokeFrame();
- stroke.setVisible(true);
- currentstroke = stroke.a;
- penstroke = stroke.b;
- eraserstroke = stroke.c;
- stroke.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- }
- });
- buttonPanel.acuteButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- AcuteFrame acute = new AcuteFrame();
- acute.setVisible(true);
- //Shape temp = new Shape();
- //temp.setStart(acute.sx, acute.sy);
- //temp.setEnd(acute.ex, acute.ey);
- currentShape = acute.currentshape;
- //polygon.add(temp);
- acute.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- }
- });
- buttonPanel.wordSetting.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- FontSetting Font = new FontSetting();
- Font.setVisible(true);
- // num = Font.number;
- // style1 = Font.s2;
- Font.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- }
- });
- }
- /*
- * 画图组件,构建画笔进行画图
- */
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2 = (Graphics2D) g; // 定义画笔
- int w = getWidth();
- int h = getHeight();
- Rectangle2D scale = new Rectangle2D.Double(0, 0, w, h);
- g2.setColor(currentBackColor);
- g2.fill(scale); // 画出主框架
- // 采用2D图形方法分别进行画图
- for (int i = 0; i < polygon.size(); i++) {
- Shape temp = (Shape) polygon.get(i);
- g2.setColor(temp.color);
- switch (temp.shape) {
- case Shape.PEN:
- Line2D showLine = new Line2D.Double(temp.getStart(), temp
- .getEnd());
- g2.setStroke(new BasicStroke(penstroke, BasicStroke.CAP_ROUND,
- BasicStroke.JOIN_BEVEL));
- g2.draw(showLine);
- break;
- case Shape.ERASER:
- Line2D showLine1 = new Line2D.Double(temp.getStart(), temp
- .getEnd());
- g2.setColor(currentBackColor);
- g2.setStroke(new BasicStroke(eraserstroke,
- BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
- g2.draw(showLine1);
- break;
- case Shape.LINE:
- Line2D showLine2 = new Line2D.Double(temp.getStart(), temp
- .getEnd());
- g2.setStroke(new BasicStroke(currentstroke,
- BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
- g2.draw(showLine2);
- break;
- case Shape.SQUARE:
- Rectangle2D showSquare = new Rectangle2D.Double();
- showSquare.setFrameFromDiagonal(temp.getStart(), temp.getEnd());
- g2.setStroke(new BasicStroke(currentstroke));
- if (temp.isFill)
- g2.fill(showSquare);
- else
- g2.draw(showSquare);
- break;
- case Shape.ELLIPSE:
- Ellipse2D showEllipse = new Ellipse2D.Double();
- showEllipse
- .setFrameFromDiagonal(temp.getStart(), temp.getEnd());
- g2.setStroke(new BasicStroke(currentstroke));
- if (temp.isFill)
- g2.fill(showEllipse);
- else
- g2.draw(showEllipse);
- break;
- case Shape.ROUND:
- Ellipse2D showRound = new Ellipse2D.Double();
- showRound.setFrameFromDiagonal(temp.getStart(), temp.getEnd());
- g2.setStroke(new BasicStroke(currentstroke));
- if (temp.isFill)
- g2.fill(showRound);
- else
- g2.draw(showRound);
- break;
- case Shape.WORD:
- Font word = new Font(style1, Font.PLAIN, num);
- g2.setFont(word);
- if (temp.s1 != null)
- g2.drawString(temp.s1, temp.startX, temp.startY);
- break;
- case Shape.ACUTELINE:
- temp.setStart(acutesx, acutesy);
- temp.setEnd(acuteex, acuteey);
- Line2D showLine3 = new Line2D.Double(temp.getStart(), temp
- .getEnd());
- g2.setStroke(new BasicStroke(currentstroke,
- BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
- g2.draw(showLine3);
- break;
- case Shape.ACUTESQUARE:
- temp.setStart(acutesx, acutesy);
- temp.setEnd(acuteex, acuteey);
- Rectangle2D showSquare2 = new Rectangle2D.Double();
- showSquare2.setFrameFromDiagonal(temp.getStart(), temp.getEnd());
- g2.setStroke(new BasicStroke(currentstroke));
- if (temp.isFill)
- g2.fill(showSquare2);
- else
- g2.draw(showSquare2);
- break;
- case Shape.ACUTEELLIPSE:
- temp.setStart(acutesx, acutesy);
- temp.setEnd(acuteex, acuteey);
- Ellipse2D showEllipse2 = new Ellipse2D.Double();
- showEllipse2
- .setFrameFromDiagonal(temp.getStart(), temp.getEnd());
- g2.setStroke(new BasicStroke(currentstroke));
- if (temp.isFill)
- g2.fill(showEllipse2);
- else
- g2.draw(showEllipse2);
- break;
- case Shape.ACUTEROUND:
- temp.setStart(acutesx, acutesy);
- temp.setEnd(acuteex, acuteey);
- Ellipse2D showRound2 = new Ellipse2D.Double();
- showRound2.setFrameFromDiagonal(temp.getStart(), temp.getEnd());
- g2.setStroke(new BasicStroke(currentstroke));
- if (temp.isFill)
- g2.fill(showRound2);
- else
- g2.draw(showRound2);
- break;
- }
- }
- }
- public void setBackColor(Color color) {
- currentBackColor = color;
- }
- public void setForeColor(Color color) {
- currentColor = color;
- }
- public void setStroke(float y) {
- currentstroke = y;
- }
- public void setPenStroke(float y) {
- penstroke = y;
- }
- public void setEraserStroke(float y) {
- eraserstroke = y;
- }
- public void setDraw() {
- currentFill = false;
- }
- public void setFill() {
- currentFill = true;
- }
- public void writeShapes(String file) {
- try {
- PrintWriter out = new PrintWriter(new FileWriter(file));
- out.println(polygon.size());
- out.println(getWidth() + "|" + getHeight() + "|"
- + currentBackColor.getRGB());
- for (int i = 0; i < polygon.size(); i++)
- ((Shape) (polygon.get(i))).writeData(out);
- out.close();
- } catch (IOException exception) {
- exception.printStackTrace();
- }
- }
- /*
- * 从文件中读入图形,并把当前容器清空
- */
- public void readShapes(String file) {
- try {
- BufferedReader in = new BufferedReader(new FileReader(file));
- int size = Integer.parseInt(in.readLine());
- String back = in.readLine();
- StringTokenizer t = new StringTokenizer(back, "|");
- int w = Integer.parseInt(t.nextToken());
- int h = Integer.parseInt(t.nextToken());
- currentBackColor = new Color(Integer.parseInt(t.nextToken()));
- polygon.clear();
- Shape temp;
- for (int i = 0; i < size; i++) {
- temp = new Shape();
- temp.readData(in);
- polygon.add(temp);
- }
- setSize(w, h);
- in.close();
- } catch (IOException exception) {
- exception.printStackTrace();
- }
- }
- /*
- * 类名:标签面板 描述:用于显示当前坐标
- */
- private class LabelPanel extends JPanel {
- JLabel label;
- public LabelPanel() {
- label = new JLabel("Current X: " + currentX + " Current Y: "
- + currentY);
- add(label);
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- label.setText("Current X: " + currentX + " Current Y: "
- + currentY);
- }
- }
- /*
- * 类名:鼠标操作
- * 描述:继承了MouseAdapter,用来完成鼠标相应事件操作
- */
- private class MouseHandler extends MouseAdapter {
- private int tempX;
- private int tempY;
- public int index = 0;
- private Shape temp;// 临时形状存储
- public Shape[] itemList = new Shape[5000];// 画笔和橡皮擦使用的形状记录容器
- public void mousePressed(MouseEvent event) {
- tempX = event.getX();
- tempY = event.getY();
- currentX = tempX;
- currentY = tempY;// 获得鼠标点击时的坐标
- int isLeft = event.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK;
- int isRight = event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK;
- if (paintEnabled && currentShape == Shape.PEN) {
- itemList[index] = new Pen();
- itemList[index].color = currentColor;
- itemList[index].setStart(tempX, tempY);
- itemList[index].setEnd(tempX, tempY);
- polygon.add(itemList[index]);
- index++;
- setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- }// 若是画笔,则记录点击时的点坐标
- else if (paintEnabled && currentShape == Shape.ERASER) {
- itemList[index] = new Eraser();
- itemList[index].color = currentBackColor;
- itemList[index].setStart(tempX, tempY);
- itemList[index].setEnd(tempX, tempY);
- polygon.add(itemList[index]);
- index++;
- setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- } // 若是橡皮擦,则记录点击时的点坐标
- else if (isLeft != 0 && paintEnabled && currentShape == Shape.WORD) {
- itemList[index] = new Word();
- itemList[index].color = currentColor;
- itemList[index].setStart(event.getX(), event.getY());
- itemList[index].setEnd(event.getX(), event.getY());
- itemList[index].s1 = (String) JOptionPane.showInputDialog(null,
- "Please input the text you want!");
- polygon.add(itemList[index]);
- index++;
- setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- }// 对文字类形状进行处理
- else if (isLeft != 0 && paintEnabled) {
- switch (currentShape) {
- case Shape.LINE:
- temp = new Line();
- break;
- case Shape.SQUARE:
- temp = new Square();
- break;
- case Shape.ELLIPSE:
- temp = new Ellipse();
- break;
- case Shape.ROUND:
- temp = new Round();
- break;
- case Shape.ACUTELINE:
- temp = new Line();
- break;
- case Shape.ACUTESQUARE:
- temp = new Square();
- break;
- case Shape.ACUTEELLIPSE:
- temp = new Ellipse();
- break;
- case Shape.ACUTEROUND:
- temp = new Round();
- break;
- default:
- temp = new Line();
- }
- temp.setStart(tempX, tempY);
- temp.setEnd(tempX, tempY);
- temp.color = currentColor;
- temp.isFill = currentFill;
- polygon.add(temp);
- setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- }// 对其他类的形状,保存坐标,加入形状容器
- else if (isLeft != 0 && !paintEnabled) {
- int size = polygon.size();
- setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
- for (int i = 0; i < size; i++) {
- temp = (Shape) polygon.get(i);
- if (temp.isContain(tempX, tempY))
- temp.isSelected = true;
- }
- moveX = tempX;
- moveY = tempY;
- } else if (isRight != 0) {
- for (int i = 0; i < polygon.size(); i++) {
- temp = (Shape) polygon.get(i);
- if (temp.isContain(tempX, tempY)) {
- polygon.remove(i);
- i--;
- }
- //将位置所占的图形除去
- }
- repaint();//重画
- }
- }
- /*
- * 鼠标松开的动作,直线、矩形、椭圆在此时得到终点坐标,画笔橡皮停止记录笔的轨迹
- */
- public void mouseReleased(MouseEvent event) {
- tempX = event.getX();
- tempY = event.getY();
- currentX = tempX;
- currentY = tempY;
- int isLeft = event.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK;
- if (isLeft != 0 && paintEnabled && currentShape == Shape.PEN) {
- itemList[index] = new Pen();
- itemList[index].color = currentColor;
- itemList[index].setStart(event.getX(), event.getY());
- itemList[index].setEnd(event.getX(), event.getY());
- polygon.add(itemList[index]);
- index++;
- setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- } else if (isLeft != 0 && paintEnabled
- && currentShape == Shape.ERASER) {
- itemList[index] = new Eraser();
- itemList[index].color = currentBackColor;
- itemList[index].setStart(event.getX(), event.getY());
- itemList[index].setEnd(event.getX(), event.getY());
- polygon.add(itemList[index]);
- index++;
- setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- } else if (isLeft != 0 && paintEnabled
- && currentShape == Shape.WORD) {
- itemList[index] = new Word();
- itemList[index].color = currentColor;
- itemList[index].setStart(event.getX(), event.getY());
- itemList[index].setEnd(event.getX(), event.getY());
- itemList[index].s1 = JOptionPane
- .showInputDialog("Please input the text you want!");
- polygon.add(itemList[index]);
- index++;
- setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- } else if (isLeft != 0 && paintEnabled) {
- temp = (Shape) polygon.get(polygon.size() - 1);
- temp.setEnd(tempX, tempY);
- }
- else if (!paintEnabled) {
- int size = polygon.size();
- setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
- for (int i = 0; i < size; i++) {
- temp = (Shape) polygon.get(i);
- temp.isSelected = false;
- }
- } else
- setCursor(Cursor.getDefaultCursor());
- }
- }
- /*
- * 类名:鼠标动作执行
- * 描述:捕捉鼠标移动和拖拽的位置,直线、矩形、椭圆实时重画,画笔和橡皮记录鼠标拖拽的位置
- */
- private class MouseMotionHandler implements MouseMotionListener {
- private int tempX;
- private int tempY;
- public int index = 0;
- private Shape temp;
- public Shape[] itemList = new Shape[5000];
- public void mouseMoved(MouseEvent event) {
- currentX = event.getX();
- currentY = event.getY();
- repaint();
- }
- public void mouseDragged(MouseEvent event) {
- tempX = event.getX();
- tempY = event.getY();
- currentX = tempX;
- currentY = tempY;
- int isLeft = event.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK;
- int isRight = event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK;
- if (paintEnabled && currentShape == Shape.PEN) {
- itemList[index] = new Pen();
- itemList[index].color = currentColor;
- itemList[index].setStart(event.getX(), event.getY());
- itemList[index].setEnd(event.getX(), event.getY());
- polygon.add(itemList[index]);
- index++;
- setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- } else if (paintEnabled && currentShape == Shape.ERASER) {
- itemList[index] = new Eraser();
- itemList[index].color = currentBackColor;
- itemList[index].setStart(event.getX(), event.getY());
- itemList[index].setEnd(event.getX(), event.getY());
- polygon.add(itemList[index]);
- index++;
- setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- } else if (isLeft != 0 && paintEnabled
- && currentShape == Shape.WORD) {
- itemList[index] = new Word();
- itemList[index].color = currentColor;
- itemList[index].setStart(event.getX(), event.getY());
- itemList[index].setEnd(event.getX(), event.getY());
- String input;
- input = JOptionPane
- .showInputDialog("Please input the text you want!");
- polygon.add(itemList[index]);
- index++;
- setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- } else if (isLeft != 0 && paintEnabled) {
- int size = polygon.size();
- temp = (Shape) polygon.get(size - 1);
- temp.setEnd(tempX, tempY);
- } else if (isLeft != 0 && !paintEnabled) {
- int size = polygon.size();
- int x1, y1, x2, y2;
- for (int i = 0; i < size; i++) {
- temp = (Shape) polygon.get(i);
- if (temp.isSelected) {
- x1 = (int) temp.getStart().getX() + (tempX - moveX);
- y1 = (int) temp.getStart().getY() + (tempY - moveY);
- x2 = (int) temp.getEnd().getX() + (tempX - moveX);
- y2 = (int) temp.getEnd().getY() + (tempY - moveY);
- temp.setStart(x1, y1);
- temp.setEnd(x2, y2);
- }
- moveX = tempX;
- moveY = tempY;//对选定的图形进行移动
- }
- }
- repaint();
- }
- }
- }
- /*
- * 类名:主框架
- * 描述:主框架的大小布局,画图面板的位置,菜单栏的构建
- */
- class PaintFrame extends JFrame {
- PaintPanel paintPanel;
- JMenuItem backItem;
- JMenuItem foreItem;
- JRadioButtonMenuItem draw;
- JRadioButtonMenuItem fill;
- public PaintFrame() {
- setTitle("java painting");
- setBounds(150, 150, 800, 500);
- paintPanel = new PaintPanel();
- getContentPane().add(paintPanel);
- JMenuBar menuBar = makeMenuBar();
- setJMenuBar(menuBar);
- backItem.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- Color defaultColor = Color.WHITE;
- Color selected = JColorChooser.showDialog(PaintFrame.this,
- "Background Color", defaultColor);
- paintPanel.setBackColor(selected);
- }
- });//前景色设置
- foreItem.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- Color defaultColor = Color.BLACK;
- Color selected = JColorChooser.showDialog(PaintFrame.this,
- "Change ForeColor", defaultColor);
- paintPanel.setForeColor(selected);
- }
- });//背景色设置
- draw.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- paintPanel.setDraw();
- }
- });
- fill.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- paintPanel.setFill();
- }
- });//填充图设置
- }
- /*
- * 类名:菜单栏
- * 描述:构建主框架的菜单栏,有文件的新建、打开、保存,颜色设置,填充画,帮助、声明
- */
- private JMenuBar makeMenuBar() {
- JMenuBar menuBar = new JMenuBar();
- JMenu fileMenu = new JMenu("File");
- fileMenu.setMnemonic('F');
- menuBar.add(fileMenu);
- JMenuItem newItem = new JMenuItem("New", 'N');
- newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
- InputEvent.CTRL_MASK));
- newItem.addActionListener(new ActionListener() {
- @SuppressWarnings("deprecation")
- public void actionPerformed(ActionEvent e) {
- PaintFrame frame = new PaintFrame();
- frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- frame.show();
- }
- });
- fileMenu.add(newItem);//新建文件
- JMenuItem openItem = new JMenuItem("Open", 'O');
- openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
- InputEvent.CTRL_MASK));
- openItem.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JFileChooser chooser = new JFileChooser();
- chooser.setCurrentDirectory(new File("."));
- chooser.addChoosableFileFilter(new MyFilter());
- int result = chooser.showOpenDialog(PaintFrame.this);
- if (result == JFileChooser.APPROVE_OPTION) {
- String file = chooser.getSelectedFile().getName();
- paintPanel.readShapes(file);
- }
- }
- });
- fileMenu.add(openItem);//打开文件
- JMenuItem saveItem = new JMenuItem("Save", 'O');
- saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
- InputEvent.CTRL_MASK));
- saveItem.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JFileChooser chooser = new JFileChooser();
- chooser.setCurrentDirectory(new File("."));
- chooser.addChoosableFileFilter(new MyFilter());
- int result = chooser.showSaveDialog(PaintFrame.this);
- if (result == JFileChooser.APPROVE_OPTION) {
- String file = chooser.getSelectedFile().getName();
- paintPanel.writeShapes(file);
- }
- }
- });
- fileMenu.add(saveItem);//保存文件
- fileMenu.addSeparator();
- JMenuItem exitItem = new JMenuItem("Exit", 'x');
- exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
- InputEvent.CTRL_MASK));
- exitItem.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent event) {
- System.exit(0);
- }
- });
- fileMenu.add(exitItem);//退出程序
- JMenu setMenu = new JMenu("Settings");
- setMenu.setMnemonic('S');
- menuBar.add(setMenu);
- backItem = new JMenuItem("Background", 'B');
- setMenu.add(backItem);
- foreItem = new JMenuItem("ForColor", 'C');
- setMenu.add(foreItem);//颜色设置
- ButtonGroup group = new ButtonGroup();
- draw = new JRadioButtonMenuItem("Draw", true);
- fill = new JRadioButtonMenuItem("Fill", false);
- group.add(draw);
- group.add(fill);
- JMenu style = new JMenu("Style");
- style.add(draw);
- style.add(fill);
- setMenu.add(style);//填充画设置
- JMenu helpMenu = new JMenu("Help");
- helpMenu.setMnemonic('H');
- menuBar.add(helpMenu);
- JMenuItem helpItem = new JMenuItem("Help", 'H');
- helpItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,
- InputEvent.CTRL_MASK));
- helpItem.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent event) {
- HelpFrame help = new HelpFrame();
- help.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- help.show();
- }
- });
- helpMenu.add(helpItem);
- JMenuItem aboutItem = new JMenuItem("About", 'A');
- aboutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
- InputEvent.CTRL_MASK));
- aboutItem.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent event) {
- JOptionPane
- .showMessageDialog(
- PaintFrame.this,
- " JavaPaint nn"
- + "Author : Jianwen Sun(Keven) nE-mail : jwfy22465@yahoo.com.cn");
- }
- });
- helpMenu.add(aboutItem);
-
- return menuBar;
- }
- private class MyFilter extends FileFilter {
- public boolean accept(File f) {
- return f.getName().toLowerCase().endsWith(".jpt")
- || f.isDirectory();
- }
- public String getDescription() {
- return "JPaint file type";
- }
- }//生成.jpt的扩展名文件
- /*
- * 类名:帮助窗口
- * 描述:生成文本区域用于显示帮助文档
- */
- private class HelpFrame extends JFrame {
- public HelpFrame() {
- setTitle("JPaint Help");
- setSize(450, 500);
- setResizable(false);
- JTextArea text = new JTextArea(20, 42);
- Font f = new Font("宋体", Font.PLAIN, 20);
- text.setFont(f);
- text.setForeground(Color.BLUE);
- text.setLineWrap(true);
- text.setWrapStyleWord(true);
- text.setEditable(false);
- text.append(" JavaPaint Helpnn");
- text.append(" JavaPaint是一个简易JAVA画图程序, "
- + "借助于JAVA2D graphics的强大功能,"
- + "可以实现简单的画图操作。 "
- + "直线、矩形、椭圆、圆形都可以借助JAVA 2D库中相应的类实现,"
- + "通过构建相应的类,调用Graphics2D的draw方法绘制图形。"
- + "画笔和橡皮是把鼠标拖拽轨迹分成很多条可以当做是点的小直线 "
- + "相连而实现的,");
- text.append("这个小程序还可以画字体,包含了字体库中20个常用字体,"
- + "还可以对设置字体大小颜色等 。"
- + "程序生成.jpt文件,可以进行保存或者打开,"
- + "实现类似于windows画图的功能 。"
- + "该软件还有许多不足,如改变线宽或者字体时,全部线或者字体都会改变。"
- + "由于作者对程序内部事件进行顺序不熟练,只好用static的类似于参量的值来表示。n");
- getContentPane().add(text);//帮助文档
- }
- }
- }
- /*
- * 类名:设置线宽窗口
- * 描述:对线宽进行设置,包括画笔线宽,橡皮擦线宽,其他图形线宽
- */
- class StrokeFrame extends JFrame {
- public float a = 2.0f;
- public float b = 3.0f;
- public float c = 10.0f;
- public StrokeFrame() {
- setTitle("Stroke Frame");
- setSize(450, 160);
- setResizable(false);
- JPanel subPanel = new JPanel();
- GridLayout GridLayout = new GridLayout(3, 2);
- subPanel.setLayout(GridLayout);
- JLabel oneLabel = new JLabel("CurrentStroke:");
- JLabel twoLabel = new JLabel("PenStroke:");
- JLabel threeLabel = new JLabel("EraserStroke:");
- one = new JTextField();
- two = new JTextField();
- three = new JTextField();
- subPanel.add(oneLabel);
- subPanel.add(one);
- subPanel.add(twoLabel);
- subPanel.add(two);
- subPanel.add(threeLabel);
- subPanel.add(three);
- add(subPanel, BorderLayout.CENTER);
- JButton ok = new JButton("ok");
- add(ok, BorderLayout.SOUTH);
- NextAction nextAction = new NextAction();
- ok.addActionListener(nextAction);
- }
- JTextField one;
- JTextField two;
- JTextField three;
- public class NextAction implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- a = Float.valueOf(one.getText());
- b = Float.valueOf(two.getText());
- c = Float.valueOf(three.getText());
- PaintPanel.currentstroke = a;
- PaintPanel.penstroke = b;
- PaintPanel.eraserstroke = c;
- }
- }
- }
- /*
- * 类名:准确画图窗口
- * 描述:准确设置某图形的坐标形状等参数,传递给画笔进行画图
- */
- class AcuteFrame extends JFrame {
- public static int sx, sy;
- public static int ex, ey;
- public static int currentshape = 0;
- public AcuteFrame() {
- setTitle("Acute Fram");
- setSize(450, 180);
- setResizable(false);
- JPanel subPanel = new JPanel();
- GridLayout GridLayout = new GridLayout(2, 4);
- subPanel.setLayout(GridLayout);
- JLabel oneLabel = new JLabel("Start coordinate-x:");
- JLabel twoLabel = new JLabel("Start coordinate-y:");
- JLabel threeLabel = new JLabel("End coordinate-x:");
- JLabel fourLabel = new JLabel("End coordinate-y:");
- one = new JTextField();
- two = new JTextField();
- three = new JTextField();
- four = new JTextField();
- subPanel.add(oneLabel);
- subPanel.add(one);
- subPanel.add(twoLabel);
- subPanel.add(two);
- subPanel.add(threeLabel);
- subPanel.add(three);
- subPanel.add(fourLabel);
- subPanel.add(four);
- add(subPanel, BorderLayout.NORTH);
- JPanel upPanel = new JPanel();
- GridLayout GridLayout1 = new GridLayout(4, 1);
- subPanel.setLayout(GridLayout1);
- JButton lineButton1 = new JButton("Line");
- JButton squareButton1 = new JButton("Square");
- JButton ellipseButton1 = new JButton("Ellipse");
- JButton roundButton1 = new JButton("Round");
- lineButton1.setToolTipText("Draw a line");
- squareButton1.setToolTipText("Draw a rectangle");
- ellipseButton1.setToolTipText("Draw a ecllipse");
- roundButton1.setToolTipText("Draw a round");
- upPanel.add(lineButton1);
- upPanel.add(squareButton1);
- upPanel.add(ellipseButton1);
- upPanel.add(roundButton1);
- add(upPanel, BorderLayout.CENTER);
- JButton ok = new JButton("OK");
- add(ok, BorderLayout.SOUTH);
- NextAction1 nextAction1 = new NextAction1();
- NextAction2 nextAction2 = new NextAction2();
- NextAction3 nextAction3 = new NextAction3();
- NextAction4 nextAction4 = new NextAction4();
- NextAction5 nextAction5 = new NextAction5();
- ok.addActionListener(nextAction5);
- lineButton1.addActionListener(nextAction1);
- squareButton1.addActionListener(nextAction2);
- ellipseButton1.addActionListener(nextAction3);
- roundButton1.addActionListener(nextAction4);
- }
- JTextField one;
- JTextField two;
- JTextField three;
- JTextField four;
- public class NextAction5 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- sx = Integer.valueOf(one.getText());
- sy = Integer.valueOf(two.getText());
- ex = Integer.valueOf(three.getText());
- ey = Integer.valueOf(four.getText());
- }
- }
- public class NextAction4 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- currentshape = Shape.ACUTEROUND;
- PaintPanel.acutesx = sx;
- PaintPanel.acutesy = sy;
- PaintPanel.acuteex = ex;
- PaintPanel.acuteey = ey;
- }
- }
- public class NextAction3 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- currentshape = Shape.ACUTEELLIPSE;
- PaintPanel.acutesx = sx;
- PaintPanel.acutesy = sy;
- PaintPanel.acuteex = ex;
- PaintPanel.acuteey = ey;
- }
- }
- public class NextAction2 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- currentshape = Shape.ACUTESQUARE;
- PaintPanel.acutesx = sx;
- PaintPanel.acutesy = sy;
- PaintPanel.acuteex = ex;
- PaintPanel.acuteey = ey;
- }
- }
- public class NextAction1 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- currentshape = Shape.ACUTELINE;
- PaintPanel.acutesx = sx;
- PaintPanel.acutesy = sy;
- PaintPanel.acuteex = ex;
- PaintPanel.acuteey = ey;
- }
- }
- }
- /*
- * 类名:字体设置窗口
- * 描述: 对字号和字体进行设置
- */
- class FontSetting extends JFrame {
- public static int number;// 字号
- public static String s2;
- public static int style;
- public FontSetting() {
- setTitle("Font setting");
- setSize(400, 300);
- setResizable(false);
- JPanel subPanel = new JPanel();
- GridLayout GridLayout = new GridLayout(2, 1);
- subPanel.setLayout(GridLayout);
- JLabel oneLabel = new JLabel("字体大小(磅值):");
- one = new JTextField();
- subPanel.add(oneLabel);
- subPanel.add(one);
- add(subPanel, BorderLayout.NORTH);
- JPanel upPanel = new JPanel();
- GridLayout GridLayout1 = new GridLayout(4, 5);
- subPanel.setLayout(GridLayout1);
- JButton brush1 = new JButton("Arial");
- JButton brush2 = new JButton("Consolas Italic");
- JButton brush3 = new JButton("Constantia");
- JButton brush4 = new JButton("Corbel");
- JButton brush5 = new JButton("Forte");
- JButton brush6 = new JButton("Gisha");
- JButton brush7 = new JButton("Courier");
- JButton brush8 = new JButton("Times New Roman");
- JButton brush9 = new JButton("Onyx");
- JButton brush10 = new JButton("Perpetua");
- JButton brush11 = new JButton("宋体");
- JButton brush12 = new JButton("黑体");
- JButton brush13 = new JButton("仿宋");
- JButton brush14 = new JButton("隶书");
- JButton brush15 = new JButton("楷体");
- JButton brush16 = new JButton("Waker");
- JButton brush17 = new JButton("Vrinda");
- JButton brush18 = new JButton("Teen");
- JButton brush19 = new JButton("Vivaldi");
- JButton brush20 = new JButton("Jokerman");
- upPanel.add(brush1);
- upPanel.add(brush2);
- upPanel.add(brush3);
- upPanel.add(brush4);
- upPanel.add(brush5);
- upPanel.add(brush6);
- upPanel.add(brush7);
- upPanel.add(brush8);
- upPanel.add(brush9);
- upPanel.add(brush10);
- upPanel.add(brush11);
- upPanel.add(brush12);
- upPanel.add(brush13);
- upPanel.add(brush14);
- upPanel.add(brush15);
- upPanel.add(brush16);
- upPanel.add(brush17);
- upPanel.add(brush18);
- upPanel.add(brush19);
- upPanel.add(brush20);
- add(upPanel, BorderLayout.CENTER);
- NextAction1 nextAction1 = new NextAction1();
- NextAction2 nextAction2 = new NextAction2();
- NextAction3 nextAction3 = new NextAction3();
- NextAction4 nextAction4 = new NextAction4();
- NextAction5 nextAction5 = new NextAction5();
- NextAction6 nextAction6 = new NextAction6();
- NextAction7 nextAction7 = new NextAction7();
- NextAction8 nextAction8 = new NextAction8();
- NextAction9 nextAction9 = new NextAction9();
- NextAction10 nextAction10 = new NextAction10();
- NextAction11 nextAction11 = new NextAction11();
- NextAction12 nextAction12 = new NextAction12();
- NextAction13 nextAction13 = new NextAction13();
- NextAction14 nextAction14 = new NextAction14();
- NextAction15 nextAction15 = new NextAction15();
- NextAction16 nextAction16 = new NextAction16();
- NextAction17 nextAction17 = new NextAction17();
- NextAction18 nextAction18 = new NextAction18();
- NextAction19 nextAction19 = new NextAction19();
- NextAction20 nextAction20 = new NextAction20();
- brush1.addActionListener(nextAction1);
- brush2.addActionListener(nextAction2);
- brush3.addActionListener(nextAction3);
- brush4.addActionListener(nextAction4);
- brush5.addActionListener(nextAction5);
- brush6.addActionListener(nextAction6);
- brush7.addActionListener(nextAction7);
- brush8.addActionListener(nextAction8);
- brush9.addActionListener(nextAction9);
- brush10.addActionListener(nextAction10);
- brush11.addActionListener(nextAction11);
- brush12.addActionListener(nextAction12);
- brush13.addActionListener(nextAction13);
- brush14.addActionListener(nextAction14);
- brush15.addActionListener(nextAction15);
- brush16.addActionListener(nextAction16);
- brush17.addActionListener(nextAction17);
- brush18.addActionListener(nextAction18);
- brush19.addActionListener(nextAction19);
- brush20.addActionListener(nextAction20);
- }
- JTextField one;
- public class NextAction1 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "Arial";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction2 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "Consolas Italic";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction3 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "Constantia";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction4 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "Corbel";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction5 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "Forte";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction6 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "Gisha";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction7 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "Courier";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction8 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "Times New Roman";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction9 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "Onyx";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction10 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "Perpetua";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction11 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "宋体";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction12 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "黑体";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction13 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "仿宋";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction14 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "隶书";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction15 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "楷体";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction16 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "Waker";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction17 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "Vrinda";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction18 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "Teen";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction19 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "Vivaldi";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- public class NextAction20 implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- PaintPanel.style1 = "Jokerman";
- number = Integer.valueOf(one.getText());
- PaintPanel.num = number;
- }
- }
- }
- /*
- * 类名:主程序
- * 描述:画图程序运行的主程序
- */
- public class JPaint {
- public static void main(String[] args) {
- PaintFrame frame = new PaintFrame();
- JFrame.setDefaultLookAndFeelDecorated(true);
- JDialog.setDefaultLookAndFeelDecorated(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.show();
- }
- }