畫筆工具
功能
首先我們確定我們的畫筆中需要的功能:畫線條、畫等腰三角形、畫矩形、畫橢圓、顏色的選擇已經更改、線條的加粗和變細、撤銷、清除和保存。然后我們建立一個繼承了JFrame類的子類PaintBrushFrame:
public class PaintBrushFrame extends JFrame {
public PaintBrushFrame() {
this.setTitle("我的繪圖工具");
this.setSize(800, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new PaintBrushFrame().setVisible(true);
}
}
先創建出一個固定大小居中不可變的窗口,然后我們將要實現的功能按鈕分為兩類:1.圖形類 2.應用類。
建立圖形抽象類:
public abstract class Shape {
protected int startX;
protected int startY;
protected int endX;
protected int endY;
protected Color color;
protected int lineWidth;
/**
* 畫圖
* @param g 畫筆
*/
public void draw(Graphics g){
g.setColor(color);
((Graphics2D) g).setStroke(new BasicStroke(lineWidth));;
}
/**
* 修改初始橫坐標
* @param startX 初始橫坐標
*/
public void setStartX(int startX) {
this.startX = startX;
}
/**
* 修改初始縱坐標
* @param startY 初始縱坐標
*/
public void setStartY(int startY) {
this.startY = startY;
}
/**
* 修改終點橫坐標
* @param endX 終點橫坐標
*/
public void setEndX(int endX) {
this.endX = endX;
}
/**
* 修改終點縱坐標
* @param endY 終點縱坐標
*/
public void setEndY(int endY) {
this.endY = endY;
}
/**
* 修改顏色
* @param color 顏色
*/
public void setColor(Color color) {
this.color = color;
}
/**
* 修改粗細
* @param lineWidth 粗細
*/
public void setLineWidth(int lineWidth) {
this.lineWidth = lineWidth;
}
}
建立圖形抽象類的子類線條類:
public class Line extends Shape {
@Override
public void draw(Graphics g) {
super.draw(g);
g.drawLine(startX, startY, endX, endY);
}
}
建立圖形抽象類的子類橢圓類
public class Oval extends Shape {
@Override
public void draw(Graphics g) {
super.draw(g);
int x = startX<endX?startX:endX;
int y = startY<endY?startY:endY;
int width = Math.abs(startX-endX);
int height = Math.abs(startY-endY);
g.drawOval(x, y, width, height);
}
}
建立圖形抽象類的子類矩形類
public class Rectangle extends Shape {
@Override
public void draw(Graphics g) {
super.draw(g);
int x = startX<endX?startX:endX;
int y = startY<endY?startY:endY;
int width = Math.abs(startX-endX);
int height = Math.abs(startY-endY);
g.drawRect(x, y, width, height);
}
}
建立圖形抽象類的子類等腰三角形類
public class Triangle extends Shape{
@Override
public void draw(Graphics g) {
super.draw(g);
int x1 = startX>endX?startX:endX;
int y1 = startY>endY?startY:endY;
int width =Math.abs(startX-endX);
int x2 = x1 - width;
int y2 = y1;
int x3 = (startX<endX?startX:endX)+width/2;
int y3 = startY <endY?startY:endY;
g.drawPolygon(new int[] {x1, x2,x3},new int[] {y1,y2,y3},3);//畫三角形
// g.drawLine(x1, y1, x2, y2);
// g.drawLine(x3, y3, x2, y2);
// g.drawLine(x1, y1, x3, y3);
}
}
這個時候我們如果要創建圖形類的對象需要分別對這四種不同的圖形操作,這時我們可以再創建一個圖形的工廠類ShapeFactory,運用多態的實現方法創建一個ShapeFactory的對象然后再對應實現:
public class ShapeFactory {
//靜態的東西屬于一個類,不屬于這個類的任何一個對象
/**
* 創建圖形的工廠方法
* @param shapeType 類型
* @return 圖形對象或null
*/
public static Shape createShape(String shapeType){
Shape currentShape = null;
switch (shapeType) {
case "矩形" :
currentShape = new Rectangle();
break;
case "橢圓" :
currentShape = new Oval();
break;
case "三角形":
currentShape = new Triangle();
break;
case "線條" :
currentShape = new Line();
break;
}
return currentShape;
}
}
最后通過按鈕操作,鼠標點擊和松開事件的監聽,計時器和畫筆完成:
@SuppressWarnings("serial")
public class PaintBrushFrame extends JFrame {
private BufferedImage image = new BufferedImage(800, 600, 1);
private List<Shape> shapesArray = new ArrayList<>();
private String currentType = "線條";
private Color defeultColor = Color.BLACK;
private int defeultLineWidth = 1;
public PaintBrushFrame() {
this.setTitle("我的繪圖工具");
this.setSize(800, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel buttonPanel = new JPanel();
this.add(buttonPanel, BorderLayout.SOUTH);
String[] buttonNames = {"線條", "矩形", "橢圓", "等腰三角形"};
for (String name : buttonNames) {
JButton button = new JButton(name);
button.addActionListener(evt -> {
currentType = evt.getActionCommand();
});
buttonPanel.add(button);
}
String[] buttonNames2 = {"選擇顏色", "-", "+", "撤銷", "清空", "保存"};
for (String name : buttonNames2) {
JButton button = new JButton(name);
button.addActionListener(evt -> {
String command = evt.getActionCommand();
if (command.equals("選擇顏色")) {
Color currentColor = JColorChooser.showDialog(
PaintBrushFrame.this, "請選擇顏色", defeultColor);
defeultColor = currentColor != null
? currentColor
: defeultColor;
} else if (command.equals("-")) {
if (defeultLineWidth > 0) {
defeultLineWidth--;
}
} else if (command.equals("+")) {
defeultLineWidth++;
}
else if (command.equals("撤銷")) {
if (!shapesArray.isEmpty()) {
// Java雖然擁有垃圾回收(Garbage Collection)機制
// 但如果程序編寫不當仍然有可能造成內存泄漏
// 垃圾回收是針對內存堆空間的無用對象清理工作
shapesArray.remove(shapesArray.size() - 1);// 防止內存泄漏,為了讓垃圾回收器可以回收
repaint();
}
} else if (command.equals("清空")) {
if (!shapesArray.isEmpty()) {
shapesArray.clear();
}
repaint();
} else if (command.equals("保存")) {
JFileChooser chooser = new JFileChooser();
int choice = chooser.showSaveDialog(PaintBrushFrame.this);
if (choice == JFileChooser.APPROVE_OPTION) {
BufferedImage newImage = new BufferedImage(800, 600, 1);
Graphics graphics = newImage.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, 800, 600);
for (Shape shape : shapesArray) {
shape.draw(graphics);;
}
// for (int i = 0; i < totalShapes; i++) {
// shapesArray[i].draw(graphics);// 使用多態實現畫圖功能
// }
try {
ImageIO.write(image, "PNG",
chooser.getSelectedFile());
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
buttonPanel.add(button);
}
/*
* 缺省適配模式 給窗口或者窗口上的控件注冊事件監聽器有三種做法: 1.創建匿名內部類的對象(就地實例化)
* 2.創建一個內部類對象充當監聽器(因為有名字隨時都可以創建對象) 3.讓窗口實現接口用窗口對象充當監聽器 從Java
* 8開始,對于單方法接口(函數式接口)可以使用Lambda表達式 使用Lambda表達式其實就是寫一個匿名方法來編寫事件回調代碼
*/
MouseAdapter adapter = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
// 用工廠創建對象(根具體的圖形類型實現解耦和)
// // 畫線 Shape currentShape = new Line();//畫線
// // Shape currentShape = new Rectangle();//畫矩形
// // Shape currentShape = new Oval();//畫圓
// Shape currentShape = null;
// switch (currentType) {
// case "矩形" :
// currentShape = new Rectangle();
// break;
// case "橢圓" :
// currentShape = new Oval();
// break;
// case "三角形" :
// currentShape = new Triangle();
// break;
//
// default :
// currentShape = new Line();
// break;
// }
Shape currentShape = ShapeFactory.createShape(currentType);
currentShape.setColor(defeultColor);
currentShape.setLineWidth(defeultLineWidth);
currentShape.setStartX(x);
currentShape.setStartY(y);
currentShape.setEndX(x);
currentShape.setEndY(y);
shapesArray.add(currentShape);
}
@Override
public void mouseDragged(MouseEvent e) {
Shape currentShape = shapesArray.get(shapesArray.size() - 1);
int x = e.getX();
int y = e.getY();
currentShape.setEndX(x);
currentShape.setEndY(y);
repaint();
}
};
this.addMouseListener(adapter);
this.addMouseMotionListener(adapter);
// currentShape.setStartX(50);
// currentShape.setStartY(80);
// currentShape.setEndX(500);
// currentShape.setEndY(380);
}
// 該方法為回調方法
@Override
public void paint(Graphics g) {
Graphics otherGraphics = image.getGraphics();
super.paint(otherGraphics);
for (Shape shape : shapesArray) {
shape.draw(otherGraphics);
}
g.drawImage(image, 0, 0, null);
}
public static void main(String[] args) {
new PaintBrushFrame().setVisible(true);
}
}