实现画图程序的重绘功能

 画图

-创建一个窗体 : JFrame

  • 需要设置属性: 标题 尺寸 位置 关闭方式 可视化 布局
-实现鼠标监听器: MouseListener
  • 创建一个类 实现 监听器接口 ,重写接口中的所有方法
-鼠标事件: MouseEvent 获取鼠标事件发生时的数据
  • MouseEvent e参数 可以获取鼠标操作时的坐标
  • e.getX();
  • e.getY();
  • 还可以获取鼠标的哪个按键操作的 左 中 右 = 1 2 3
-渲染图形:
  • Graphics 图形 是由 GPU显卡计算屏幕中每个像素应该显示什么颜色渲染的
  • 每个可视化组件都包含了一个Graphics 用于渲染
  • 每个可视化组件的Graphics 只能在自己的组件范围内渲染图形
  • 获取窗体的Graphics 就可以在窗体上绘制图形 渲染像素点   Graphics g = jf.getGraphics();
  • 获取Graphics时必须在可视化之后

 重绘
  • 窗体在尺寸状态发生改变,窗体本身以及窗体内的组件都会自动刷新重新渲染绘制

           -窗体刷新时就会将原本的内容图形覆盖了

  • 找到窗体的刷新方法: paint

           -继承 :

               -重写方法: 子类中重写父类的方法可以在父类方法基础上改造 扩展

                    -子类对象会优先调用重写的方法

               -继承 JFrame 类,重写 paint 方法,在方法中添加自己绘制过的图片 再绘制一遍
                     -还需要在绘制图形 时保存图形 ,在paint方法中再绘制
  • 保存图形: 面向对象
           -图形类:
              -属性: 名字 坐标 颜色

              -方法: 绘制的方法

代码展示
 
import
java.awt.*;
public class BasicShape {
String type;
Color color;
int x1, y1, x2, y2;
// 封装 构造方法
public BasicShape(String type, Color color, int x1, int y1, int x2, int y2) {
this.type = type;
this.color = color;
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
// 绘制方法
public void drawShape(Graphics g){
g.setColor(color);
if (type.equals("直线")) {
g.drawLine(x1, y1, x2, y2);
} else if (type.equals("矩形")) {
g.drawRect(Math.min(x1, x2), Math.min(y1, y2),
Math.abs(x2 - x1), Math.abs(y2 - y1));
} else if (type.equals("圆形")) {
g.drawOval(Math.min(x1, x2), Math.min(y1, y2),
Math.abs(x2 - x1), Math.abs(y2 - y1));
} else if (type.equals("实心矩形")) {
g.fillRect(Math.min(x1, x2), Math.min(y1, y2),
Math.abs(x2 - x1), Math.abs(y2 - y1));
} else if (type.equals("实心圆形")) {
g.fillOval(Math.min(x1, x2), Math.min(y1, y2),
Math.abs(x2 - x1), Math.abs(y2 - y1));
} else if (type.equals("等腰三角形")) {
int mx1 = (x1 + x2) / 2;
int my1 = y1;
g.drawLine(mx1, my1, x1, y2);
g.drawLine(mx1, my1, x2, y2);
g.drawLine(x2, y2, x1, y2);
} else if (type.equals("实心等腰三角形")) {
Polygon polygon = new Polygon();
int mx1 = (x1 + x2) / 2;
int my1 = y1;
polygon.addPoint(mx1, my1);
polygon.addPoint(x1, y2);
polygon.addPoint(x2, y2);
g.fillPolygon(polygon);
}
}
}
import
javax.swing.*;
import
java.awt.*;
public class DrawPad extends JFrame {
DrawListener dl = new DrawListener();
public void initUI() {
setTitle("画图板 v1.0");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);// 居中显示
setLayout(new FlowLayout());
String[] btnStrs = {"直线", "矩形", "圆形", "实心矩形", "实心圆形", "等腰三角形",
"实心等腰三角形", "多边形", "三角形", "撤回"};
Color[] colors = {
Color.BLACK, Color.WHITE, Color.GRAY, Color.LIGHT_GRAY,
Color.DARK_GRAY, Color.RED,
Color.GREEN, Color.BLUE, Color.ORANGE, Color.YELLOW, Color.PINK,
Color.CYAN,
new Color(53, 152, 96, 137)
};
for (int i = 0; i < btnStrs.length; i++) {
JButton btn = new JButton(btnStrs[i]);
add(btn);
btn.addActionListener(dl);
}
for (int i = 0; i < colors.length; i++) {
JButton btn = new JButton();
btn.setBackground(colors[i]);
btn.setPreferredSize(new Dimension(25, 25));
add(btn);
btn.addActionListener(dl);
}
setVisible(true);
// 获取Graphics
Graphics g = getGraphics();
// 添加鼠标监听器
addMouseListener(dl);
// 传递的是一个对象的所在地址,dl中的g 与 g 都是指向的同一个Graphics对象
dl.g = g;
}
@Override
public void paint(Graphics g) {
super.paint(g);// 原本刷新窗体的方法 super 在子类中表示父类
// 调用自己存储的图形重新绘制
System.out.println("paint");
for (int i = 0; i < dl.shapeList.length; i++) {
BasicShape shape = dl.shapeList[i];
if(shape==null){
return;
}
shape.drawShape(g);
// null.xxx
}
}
public static void main(String[] args) {
DrawPad drawPad = new DrawPad();
drawPad.initUI();
}
}
import
javax.swing.*;
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 DrawListener implements MouseListener, ActionListener {
Graphics g;
int x1, y1, x2, y2;// 类的属性 可以在整个类中调用使用
int x3, y3, x4, y4, x5, y5, count;
String type = "直线";
Color background = Color.BLACK;
// 存储图形对象的数组
int maxShapeSize = 30;
BasicShape[] shapeList = new BasicShape[maxShapeSize];
int shapeSize = 0;
@Override
public void actionPerformed(ActionEvent e) {
String ac = e.getActionCommand();
if (ac.equals("")) {
JButton btn = (JButton) e.getSource();
background = btn.getBackground();
g.setColor(background);
} else {
type = ac;
}
System.out.println();
}
public void mouseClicked(MouseEvent e) {
System.out.println("点击");
int x = e.getX();
int y = e.getY();
if (type.equals("三角形") || type.equals("多边形")) {
g.fillOval(x - 3, y - 3, 6, 6);
if (count == 0) {
x3 = x;
y3 = y;
count++;
} else if (count == 1) {
x4 = x;
y4 = y;
g.drawLine(x3, y3, x4, y4);
count++;
} else if (count == 2) {
x5 = x;
y5 = y;
if (type.equals("三角形")) {
g.drawLine(x5, y5, x4, y4);
g.drawLine(x3, y3, x5, y5);
count = 0;
} else {
g.drawLine(x5, y5, x4, y4);
if (e.getButton() == MouseEvent.BUTTON3) {
g.drawLine(x3, y3, x5, y5);
count = 0;
return;
}
x4 = x5;
y4 = y5;
count = 2;
}
}
}
}
public void mousePressed(MouseEvent e) {
System.out.println("按下");
int x = e.getX();// 方法中的局部变量 只能在方法中使用
int y = e.getY();
System.out.println("x: " + x + " y:" + y);
x1 = x;
y1 = y;
}
// 局部:
public void mouseReleased(MouseEvent e) {
System.out.println("释放");
int x = e.getX();
int y = e.getY();
System.out.println("x: " + x + " y:" + y);
x2 = x;
y2 = y;
if (type.equals("直线") || type.equals("矩形") || type.equals("圆形")
|| type.equals("实心矩形") || type.equals("实心圆形") ||
type.equals("等腰三角形") || type.equals("实心等腰三角形")) {
BasicShape shape = new BasicShape(type, background, x1, y1, x2, y2);
shape.drawShape(g);
if (shapeSize >= maxShapeSize) {
System.out.println("存储容量已满");
// 扩容数组
return;
}
shapeList[shapeSize++] = shape;
}
}
public void mouseEntered(MouseEvent e) {
System.out.println("进入");
}
public void mouseExited(MouseEvent e) {
System.out.println("离开");
}
}

多边形的重绘功能暂未实现,在下一篇博客里更新哦~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值