1、问题的产生与明确
上节课学过简易画图板的制作以后,在做出的画图板上,细心观察不难发现一个问题:当改变界面大小(最大化、最小化)时,画板上绘制的图形会全部消失。
原因是这样的:
在java编译器中,图形界面是由容器组件和元素组件构成的。
而所有的组件都是采用的C和C++的代码,AWT组件就是通过调用操作系统底层的绘图函数来实现的;SWING组件则是在AWT组件的基础上,采用纯Java语言实现的。
总而言之一句话:所有的组件都是画出来的。
我们所绘制图形的数据都存储在内存中,在创建窗体时我们已经定义了窗体的大小,如果我们再次改变窗体大小的时候,原来的窗体就不满足显示的需求。这时候就会自动调用组件的绘制方法,将窗体上所有的组件再重新绘制一次,但是不会执行我们所绘制的图形的代码,所以我们看到的就是绘图板界面还在,但是界面上之前绘制的图形消失了。操作系统重绘窗体上的组件时,调用了paint方法,这个方法是定义在JFrame和JPanel中都有的,叫做
Public void paint (Graphics g) { } ,一句简单的“Graphics g ; ”在实际编写中是这样的:
public abstract class Graphics {
protected Graphics() {
public abstract Graphics create();
{
/**
* Creates a new {@code Graphics} object based on this
* {@code Graphics} object, but with a new translation and clip area.
* The new {@code Graphics} object has its origin
* translated to the specified point (<i>x</i>, <i>y</i>).
* Its clip area is determined by the intersection of the original
* clip area with the specified rectangle. The arguments are all
* interpreted in the coordinate system of the original
* {@code Graphics} object. The new graphics context is
* identical to the original, except in two respects:
*
* <ul>
* <li>
* The new graphics context is translated by (<i>x</i>, <i>y</i>).
* That is to say, the point ({@code 0}, {@code 0}) in the
* new graphics context is the same as (<i>x</i>, <i>y</i>) in
* the original graphics context.
* <li>
* The new graphics context has an additional clipping rectangle, in
* addition to whatever (translated) clipping rectangle it inherited
* from the original graphics context. The origin of the new clipping
* rectangle is at ({@code 0}, {@code 0}), and its size
* is specified by the {@code width} and {@code height}
* arguments.
* </ul>
*
* @param x the <i>x</i> coordinate.
* @param y the <i>y</i> coordinate.
* @param width the width of the clipping rectangle.
* @param height the height of the clipping rectangle.
* @return a new graphics context.
* @see java.awt.Graphics#translate
* @see java.awt.Graphics#clipRect
*/
public Graphics create(int x, int y, int width, int height) {
Graphics g = create();
if (g == null) return null;
g.translate(x, y);
g.clipRect(0, 0, width, height);
return g;
}
public abstract void translate(int x, int y);
public abstract Color getColor();
想要改变这个方法,就必须定义一个类继承组件,然后才能重写paint方法。最重要的一点就是,该类必须继承JFrame类或是JPanel类,不然不可以重写paint方法。
2、如何让图形不消失?
想让图形保留,就要将图形的相关数据记录下来
在之前的学习中,我们已经了解到一些基础的图形绘画方法:
对于直线:x1,y1,x2,y2,color,type;
对于矩形:x1,y1,width,height,color,type;
对于图片:x1,y1,width,height,Image,type;
对于文字:x1,y1,str,color,font,type;
但从以上方法不难看出,如果直接记录方法,需要定义很多很多个图形类,这在现实中不太现实,所以需要寻找另外的方法。
实际上,仅仅需要定义数组来存储图形的数据作为内存,但是图形有很多种类型,20,30,100,每一种类型定义一个数组,数组量就太庞大了。
那么进一步思考:怎么减少数组的数量?
有两种比较可行的方法:
1.定义一个图形父类,不同的图形就定义这个类的子类,数组类型就可以使用图形父类的;
// 图形父类
class Shape {
// 共有的方法
public void draw() {
System.out.println("Drawing a shape");
}
}
// 圆形是 Shape 的子类
class Circle extends Shape {
// 重写父类的方法
@Override
public void draw() {
System.out.println("Drawing a circle");
}
}
public class Main {
public static void main(String[] args) {
// 创建一个 Shape 类型的数组,可以存储不同图形的实例
Shape[] shapes = new Shape[2];
shapes[0] = new Shape(); // 父类实例
shapes[1] = new Circle(); // 子类实例
// 遍历数组并调用各个图形的方法
for (Shape shape : shapes) {
shape.draw();
}
}
}
2.定义一个类,使用方法重载来解决。
而数组类型就是该类。
窗体改变大小后会自动调用paint方法,此时需要将paint方法进行重写,在重写的重绘方法中,把存储在数组(内存)中的图形再画一次。
3、具体实现:
重新定义一个Data类用来储存数据,在该类得方法中将绘图的相关数据赋给类的属性,如果有某组特殊的数据,需要对方法进行重载,通过改变参数的类型和个数进行重载;例如,对图片的绘制时,就需要多存放一组ImageIcon数据,此时就要对Data类的方法进行重写。
public int x1,y1,x2,y2,width,height;
public String type;
public Color color;
public Graphics g;
public ImageIcon icon;
public Data(int x1,int y1,int x2,int y2,Color color,String type) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
this.type = type;
}
//对Data方法进行重写,针对有特殊属性的数据
public Data(int x1,int y1,int width,int height, Color color, String type, ImageIcon icon) {
this.x1 = x1;
this.y1 = y1;
this.color = color;
this.width = width;
this.height = height;
this.type = type;
this.icon = icon;
}
把数据存放到内存以后,在Data类中定义重绘方法,重写调用重绘方法时,把存储在数组中的图形再画一遍。
public void paint (Graphics g) {
super.paint(g);
System.out.println("重绘");
//添加循环语句,将之前所的绘图全部进行重绘,等到输出为空时结束循环
for (int i = 0; i<array.length; i++) {
Data d = array[i];
if (d != null)
d.draw(g);
else
{break;}
}
将原本的Drawing类继承父类JFrame,实例化Drawing对象drawing时,如果在drawing对象中找不到的方法就会自动到父类JFrame中去调用,这时就不用多余的实例化JFrame对象,只需要用this关键字表示drawing对象就能调用JFrame中的方法对窗体界面进行设计了。(重点、难点)
在图形重绘(或任何对象的方法调用)中,关键字 this
有着特定的作用。在Java中,this
是一个关键字,表示当前对象的引用。它可以用于区分实例变量和方法参数,以及在某些情况下明确指示对当前对象的引用。
以下是 this
在图形重绘中的常见用途:
-
区分实例变量和方法参数: 当方法参数与实例变量同名时,使用
this
可以明确指示使用的是实例变量而不是方法参数。例如:class Shape { private int x; private int y; // 构造函数 public Shape(int x, int y) { this.x = x; this.y = y; } // 重绘方法 public void redraw(int x, int y) { // 使用 this.x 和 this.y 来引用实例变量 this.x = x; this.y = y; // 其他重绘逻辑... } }
-
在上述例子中,构造函数和
redraw
方法中使用了this.x
和this.y
,以明确指示它们是实例变量。 -
在构造函数中调用其他构造函数: 在一个类的构造函数中,可以使用
this
调用同一个类中的其他构造函数。这样可以避免代码的重复。例如:class Shape { private int x; private int y; // 默认构造函数 public Shape() { // 调用带参数的构造函数,并传递默认值 this(0, 0); } // 带参数的构造函数 public Shape(int x, int y) { this.x = x; this.y = y; } }
-
在这个例子中,
Shape
类有两个构造函数,其中一个通过使用this(0, 0)
调用另一个构造函数,传递了默认值 -
在图形重绘的上下文中,
this
主要用于访问当前对象的实例变量,确保正确地引用对象自身的属性。 -
最终完成重绘