畫(huà)筆工具
功能
首先我們確定我們的畫(huà)筆中需要的功能:畫(huà)線條、畫(huà)等腰三角形、畫(huà)矩形、畫(huà)橢圓、顏色的選擇已經(jīng)更改、線條的加粗和變細(xì)、撤銷、清除和保存。然后我們建立一個(gè)繼承了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);
}
}
先創(chuàng)建出一個(gè)固定大小居中不可變的窗口,然后我們將要實(shí)現(xiàn)的功能按鈕分為兩類:1.圖形類 2.應(yīng)用類。
建立圖形抽象類:
public abstract class Shape {
protected int startX;
protected int startY;
protected int endX;
protected int endY;
protected Color color;
protected int lineWidth;
/**
* 畫(huà)圖
* @param g 畫(huà)筆
*/
public void draw(Graphics g){
g.setColor(color);
((Graphics2D) g).setStroke(new BasicStroke(lineWidth));;
}
/**
* 修改初始橫坐標(biāo)
* @param startX 初始橫坐標(biāo)
*/
public void setStartX(int startX) {
this.startX = startX;
}
/**
* 修改初始縱坐標(biāo)
* @param startY 初始縱坐標(biāo)
*/
public void setStartY(int startY) {
this.startY = startY;
}
/**
* 修改終點(diǎn)橫坐標(biāo)
* @param endX 終點(diǎn)橫坐標(biāo)
*/
public void setEndX(int endX) {
this.endX = endX;
}
/**
* 修改終點(diǎn)縱坐標(biāo)
* @param endY 終點(diǎn)縱坐標(biāo)
*/
public void setEndY(int endY) {
this.endY = endY;
}
/**
* 修改顏色
* @param color 顏色
*/
public void setColor(Color color) {
this.color = color;
}
/**
* 修改粗細(xì)
* @param lineWidth 粗細(xì)
*/
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);//畫(huà)三角形
// g.drawLine(x1, y1, x2, y2);
// g.drawLine(x3, y3, x2, y2);
// g.drawLine(x1, y1, x3, y3);
}
}
這個(gè)時(shí)候我們?nèi)绻獎(jiǎng)?chuàng)建圖形類的對(duì)象需要分別對(duì)這四種不同的圖形操作,這時(shí)我們可以再創(chuàng)建一個(gè)圖形的工廠類ShapeFactory,運(yùn)用多態(tài)的實(shí)現(xiàn)方法創(chuàng)建一個(gè)ShapeFactory的對(duì)象然后再對(duì)應(yīng)實(shí)現(xiàn):
public class ShapeFactory {
//靜態(tài)的東西屬于一個(gè)類,不屬于這個(gè)類的任何一個(gè)對(duì)象
/**
* 創(chuàng)建圖形的工廠方法
* @param shapeType 類型
* @return 圖形對(duì)象或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;
}
}
最后通過(guò)按鈕操作,鼠標(biāo)點(diǎn)擊和松開(kāi)事件的監(jiān)聽(tīng),計(jì)時(shí)器和畫(huà)筆完成:
@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, "請(qǐng)選擇顏色", 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)機(jī)制
// 但如果程序編寫(xiě)不當(dāng)仍然有可能造成內(nèi)存泄漏
// 垃圾回收是針對(duì)內(nèi)存堆空間的無(wú)用對(duì)象清理工作
shapesArray.remove(shapesArray.size() - 1);// 防止內(nèi)存泄漏,為了讓垃圾回收器可以回收
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);// 使用多態(tài)實(shí)現(xiàn)畫(huà)圖功能
// }
try {
ImageIO.write(image, "PNG",
chooser.getSelectedFile());
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
buttonPanel.add(button);
}
/*
* 缺省適配模式 給窗口或者窗口上的控件注冊(cè)事件監(jiān)聽(tīng)器有三種做法: 1.創(chuàng)建匿名內(nèi)部類的對(duì)象(就地實(shí)例化)
* 2.創(chuàng)建一個(gè)內(nèi)部類對(duì)象充當(dāng)監(jiān)聽(tīng)器(因?yàn)橛忻蛛S時(shí)都可以創(chuàng)建對(duì)象) 3.讓窗口實(shí)現(xiàn)接口用窗口對(duì)象充當(dāng)監(jiān)聽(tīng)器 從Java
* 8開(kāi)始,對(duì)于單方法接口(函數(shù)式接口)可以使用Lambda表達(dá)式 使用Lambda表達(dá)式其實(shí)就是寫(xiě)一個(gè)匿名方法來(lái)編寫(xiě)事件回調(diào)代碼
*/
MouseAdapter adapter = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
// 用工廠創(chuàng)建對(duì)象(根具體的圖形類型實(shí)現(xiàn)解耦和)
// // 畫(huà)線 Shape currentShape = new Line();//畫(huà)線
// // Shape currentShape = new Rectangle();//畫(huà)矩形
// // Shape currentShape = new Oval();//畫(huà)圓
// 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);
}
// 該方法為回調(diào)方法
@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);
}
}