组成
UI界面
图形绘制功能
窗体继承和图像重绘
UI界面
见上篇界面开发
图形绘制功能
图形类Shape
import java.awt.*;
import javax.swing.*;
public class Shape {
int x1, x2, y1, y2;
String type;
Color color;
public void drawShape(Graphics g) {
g.setColor(color);
switch (type) {
case "直线":
g.drawLine(x1, y1, x2, y2);
break;
case "矩形":
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs
(y2 - y1));
break;
case "圆形":
g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs
(y2 - y1));
break;
}
}
}
基本图形绘制语句
g.drawLine (x1, y1, x2, y2);//直线
g.drawRect (x1, y1, x2 - x1, y2 - y1);//矩形
g.drawOval (x1, y1, x2 - x1, y2 - y1);//圆形
MouseListener 用来监听鼠标在窗体上的操作
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class PrintListener1 implements MouseListener, ActionListener {
@Override
public void actionPerformed(ActionEvent e) {}
@Override
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e){}
@Override
public void mouseReleased(MouseEvent e){}
@Override
public void mouseEntered(MouseEvent e){}
@Override
public void mouseExited(MouseEvent e){}
}
对actionPerformed重写(点击按钮)
public void actionPerformed(ActionEvent e) {
String ac = e.getActionCommand();
System.out.println("点击了按钮 " + ac);
type = ac;
}
对mouseClicked重写(点击鼠标)
public void mouseClicked(MouseEvent e) {
System.out.println("鼠标点击事件");
int x = e.getX();
int y = e.getY();
System.out.println("鼠标点击的坐标 x:" + x + " y:" + y);
if (type.equals("三角形") || type.equals("多边形")) {
Shape shape1 = new Shape ();
g.fillOval(x - 2, y - 2, 5, 5);
if (count == 0) {
x3 = x;
y3 = y;
count++;
} else if (count == 1) {
x4 = x;
y4 = y;
count++;
g.drawLine(x3, y3, x4, y4);
shape1.x1 = x3;
shape1.x2 = x4;
shape1.y1 = y3;
shape1.y2 = y4;
shape1.type = "直线";
shape1.color=Color.RED;
shapeList.addValue(shape1);
} else if (count == 2) {
x5 = x;
y5 = y;
if (type.equals("三角形")) {
count = 0;
g.drawLine(x4, y4, x5, y5);
shape1.x1 = x4;
shape1.x2 = x5;
shape1.y1 = y4;
shape1.y2 = y5;
shape1.type = "直线";
shape1.color=Color.RED;
shapeList.addValue(shape1);
g.drawLine(x5, y5, x3, y3);
shape1.x1 = x5;
shape1.x2 = x3;
shape1.y1 = y5;
shape1.y2 = y3;
shape1.type = "直线";
shape1.color=Color.RED;
shapeList.addValue(shape1);
} else {
g.drawLine(x4, y4, x5, y5);
shape1.x1 = x4;
shape1.x2 = x5;
shape1.y1 = y4;
shape1.y2 = y5;
shape1.type = "直线";
shape1.color=Color.RED;
shapeList.addValue(shape1);
System.out.println(count);
x4 = x5;
y4 = y5;
if (e.getButton() == MouseEvent.BUTTON3) {
g.drawLine(x5, y5, x3, y3);
shape1.x1 = x5;
shape1.x2 = x3;
shape1.y1 = y5;
shape1.y2 = y3;
shape1.type = "直线";
shape1.color=Color.RED;
shapeList.addValue(shape1);
count = 0;
}
}
}
}
}
对mousePressed和mouseReleased重写(鼠标拖动)
public void mousePressed(MouseEvent e){
System.out.println ("鼠标按下事件");
x1 = e.getX ();
y1 = e.getY ();
}
@Override
public void mouseReleased(MouseEvent e){
System.out.println ("鼠标释放事件");
x2 = e.getX ();
y2 = e.getY ();
if(type.equals ("直线") || type.equals ("矩形") || type.equals ("圆形")){
Shape shape = new Shape ();
shape.x1 = x1;
shape.x2 = x2;
shape.y1 = y1;
shape.y2 = y2;
shape.type = type;
shape.color = Color.RED;
shape.drawShape (g);
shapeList.addValue(shape);
}
}
窗体继承和图像重绘
import java.awt.*;
import javax.swing.*;
public class ShapeUI extends JFrame {
PrintListener1 sl = new PrintListener1();
public void initUI() {
this.setTitle("创意图画");
this.setSize(800, 800);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
String[] btnStrs = {"直线", "矩形", "圆形", "三角形", "多边形"};
for (int i = 0; i < btnStrs.length; i++) {
JButton btn = new JButton(btnStrs[i]);
this.add(btn);
btn.addActionListener(sl);
}
this.setVisible(true);
this.addMouseListener(sl);
Graphics g = this.getGraphics();
sl.g = g;
}
public void paint(Graphics g) {
super.paint(g);
Arr<Shape> shapeList = sl.shapeList;
for (int i = 0; i <= shapeList.length; i++) {
Shape shape = shapeList.getValue(i);
if (shape != null) {
shape.drawShape(g);
}
}
}
public static void main(String[] args) {
new ShapeUI().initUI();
}
}