static关键字
静态的东西只属于类,不属于类的任何一个对象
静态的属性跟类的状态无关事件监听器
给窗口或者窗口上的控件注册事件监听器有三种做法:
* 1.创建匿名内部类的对象(就地实例化)
* 2.创建一个内部类对象(因为有名字,随时都可以创建对象)
* 3.让窗口实现接口,用窗口对象(this)充当监听器从Java 8 开始,对于单方法接口(函数式接口),可以使用lambda表达式,写一个匿名方法作为事件回调代码
GC机制(garbage collection)
Java虽然拥有GC机制(垃圾回收),但如果程序编写不当,仍然可能造成内存泄漏
垃圾回收是针对内存堆空间的无用对象清理工作缺省适配模式
MouseAdapter adapter = new MouseAdapter(){}创建内部类
内部类可以直接使用外部类的私有成员(属性和方法)用工厂(factory)创建对象
画图工具
- 图形类
package org.mobiletrain;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
/**
* 图形(抽象类)
* @author apple
*
*/
public abstract class Shape {
protected int startX;//起点横坐标
protected int startY;//起点纵坐标
protected int endX;//终点横坐标
protected int endY;//终点纵坐标
protected Color color;//颜色
protected float lineWidth;//粗细
public void draw(Graphics g){
g.setColor(color);
((Graphics2D) g).setStroke(new BasicStroke(lineWidth));
}
public void setStartX(int startX) {
this.startX = startX;
}
public void setStartY(int startY) {
this.startY = startY;
}
public void setEndX(int endX) {
this.endX = endX;
}
public void setEndY(int endY) {
this.endY = endY;
}
public void setColor(Color color) {
this.color = color;
}
public void setLineWidth(float lineWidth) {
this.lineWidth = lineWidth;
}
}
- 线条类
package org.mobiletrain;
import java.awt.Graphics;
public class Line extends Shape {
@Override
public void draw(Graphics g) {
super.draw(g);
g.drawLine(startX, startY, endX, endY);
}
}
- 矩形类
package org.mobiletrain;
import java.awt.Graphics;
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);
}
}
- 圆形类
package org.mobiletrain;
import java.awt.Graphics;
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);
}
}
- 三角形类
package org.mobiletrain;
import java.awt.Graphics;
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.drawLine(x1, y1, x2, y2);
// g.drawLine(x2, y2, x3, y3);
// g.drawLine(x3, y3, x1, y1);
g.drawPolygon(new int[] {x1, x2, x3},new int[] {y1,y2,y3},3);//画多边形
}
}
- 图形工厂类
package org.mobiletrain;
/**
* 图形工厂(简单工厂模式/静态工厂模式)
* @author apple
*
*/
public class ShapeFactory {
/**
* 创建图形对象的工厂方法
* @param shapeType 类型
* @return 图形对象或null
*/
//静态的东西只属于类,不属于类的任何一个对象
//静态的属性跟类的状态无关
public static Shape creatShape(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;
}
}
- 窗口类
package org.mobiletrain.ui;
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.Collection;
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 org.mobiletrain.Shape;
import org.mobiletrain.ShapeFactory;
@SuppressWarnings("serial")
public class PaintBrushFrame extends JFrame {
private BufferedImage image = new BufferedImage(800, 600, 1);
private Shape[] shapesArray = new Shape[100];
//Collection<Integer> shape = new ArrayList<Integer>();
private int totalShapes = 0;
private String currentType = "线条";
private Color defaultColor = Color.BLACK;
private float defaultWidth = 3;
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, "请选择颜色", defaultColor);
defaultColor = currentColor != null ? currentColor : defaultColor;
}
else if (command.equals("-")) {
if (defaultWidth > 1) {
defaultWidth--;
}
}
else if (command.equals("+")) {
if (defaultWidth < 20) {
defaultWidth++;
}
}
else if (command.equals("撤销")) {
if (totalShapes > 0) {
shapesArray[totalShapes - 1] = null;//防止内存泄露
totalShapes -= 1;
repaint();
}
}
else if (command.equals("清空")) {
if (totalShapes > 0) {
for (int i = 0; i < totalShapes; i++) {
shapesArray[i] = null;
}
totalShapes = 0;
repaint();
}
}
else if (command.equals("保存")) {
JFileChooser chooser = new JFileChooser();
int choice = chooser.showSaveDialog(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 (int i = 0; i < totalShapes; i++) {
shapesArray[i].draw(graphics);
}
try {
ImageIO.write(newImage, "PNG", chooser.getSelectedFile());
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
buttonPanel.add(button);
}
MouseAdapter adapter = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {//鼠标按下
if (totalShapes < shapesArray.length) {
int x = e.getX();
int y = e.getY();
//用工厂创建对象(可以跟具体的图形类型实现解耦合)
Shape currentShape = ShapeFactory.creatShape(currentType); //方法的重构
currentShape.setColor(defaultColor);
currentShape.setLineWidth(defaultWidth);
currentShape.setStartX(x);
currentShape.setStartY(y);
currentShape.setEndX(x);
currentShape.setEndY(y);
shapesArray[totalShapes] = currentShape;
totalShapes++;
}
}
@Override
public void mouseDragged(MouseEvent e) {//鼠标拖拽
if (totalShapes > 0) {
Shape currentShape = shapesArray[totalShapes - 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 otherGrapgics = image.getGraphics();
super.paint(otherGrapgics);
for (int i = 0; i < totalShapes; i++) {
shapesArray[i].draw(otherGrapgics);
}
g.drawImage(image,0,0,null);
}
public static void main(String[] args) {
new PaintBrushFrame().setVisible(true);
}
}