第一天寫了一個繪圖小窗口
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel
import com.huawei.Shape;
import com.huawei.ShapeFactory;
@SuppressWarnings("serial")
public class PaintBrushFrame extends JFrame {
private BufferedImage image = new BufferedImage(666, 666, 1);
private List<Shape> shapesArray = new ArrayList<>();
private String currentType = "線條";
private Color defaultcolor = Color.BLACK;
private int defaultwidth = 9;
public PaintBrushFrame() {
setTitle("我的繪圖工具");
setSize(666, 666);
setResizable(false);
setLocationRelativeTo(null);
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(e -> {
currentType = button.getActionCommand();
});
buttonPanel.add(button);
}
String[] buttonNames2 = { "顏色", "-", "+", "撤銷", "清空", "保存" };
for (String name : buttonNames2) {
JButton button = new JButton(name);
button.addActionListener(e -> {
String command = e.getActionCommand();
if (command.equals("顏色")) {
Color currentColor = JColorChooser.showDialog(PaintBrushFrame.this, "請選擇顏色", defaultcolor);
defaultcolor = currentColor != null ? currentColor : defaultcolor;
} else if (command.equals("+")) {
if (defaultwidth < 23) {
defaultwidth++;
}
} else if (command.equals("-")) {
if (defaultwidth > 1) {
defaultwidth--;
}
} else if (command.equals("撤銷")) {
if (shapesArray.size() > 0) {
shapesArray.remove(shapesArray.size() - 1);
repaint();
}
} else if (command.equals("清空")) {
// Java雖然有垃圾回收機(jī)制(Garbage Collection
// 但如果程序編寫不當(dāng)仍有可能造成內(nèi)存泄漏
// 垃圾回收是針對內(nèi)存堆空間的無用對象清理工作
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(666, 666, 1);
Graphics graphics = newimage.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, 666, 666);
for (Shape shape : shapesArray) {
shape.draw(graphics);
}
try {
ImageIO.write(newimage, "PNG", chooser.getSelectedFile());
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
buttonPanel.add(button);
}
// 缺省適配模式
// 給窗口或者窗口上的有三種做法
// 1从诲,創(chuàng)建匿名內(nèi)部類的對象(就地實例化
// 2.創(chuàng)建一個內(nèi)部類對象來充當(dāng)監(jiān)聽器(它有 名字隨時可以創(chuàng)建對象
// 3.讓窗口實現(xiàn)接口俺夕,用窗口對象(this充當(dāng)監(jiān)聽器
// 從Java8開始丹喻,對于但方法接口可以使用Lambda表達(dá)式(λ
// Lambda表達(dá)式其實就是寫一個匿名方法來編寫事件回調(diào)代碼
// 創(chuàng)建匿名內(nèi)部類的對象
// 內(nèi)部類可以直接使用外部類的私有成員(屬性和方法
MouseAdapter adapter = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
// 用工廠創(chuàng)建對象(跟具體的圖形類型解耦合
Shape currentShape = ShapeFactory.createShape(currentType);
currentShape.setColor(defaultcolor);
currentShape.setLineWidth(defaultwidth);
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);
}
@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);
}
}
怎么樣去實現(xiàn)圖形的畫法就省略了襟铭,做一個繪圖工具看似簡單碌奉,但是對于現(xiàn)階段的我來說還是相當(dāng)困難的,還是得回去抄抄這個代碼蝌矛,多去理解老師寫代碼的思路道批,練好英語。
第二入撒,三天的集合聽的也是挺懵比的隆豹,集合的用處還是非常大的,List,Set茅逮。還得重寫方法璃赡,真的是爆炸啊。
這個正則表達(dá)式還是非常神奇的献雅,反正不當(dāng)搞得懂碉考,老師都說了,去網(wǎng)上找挺身,不需要自己寫侯谁,只要懂原理就行了