读书人

7.重绘

发布时间: 2012-09-25 09:55:58 作者: rapoo

七.重绘
为什么要重绘?
缓存
int array[] = new int[0];

//实现数组动态变化,也就是每次执行该方法时数组的长度加1
public void add(int a){
//创建一个新的数组,长度为array数组长度加1
int[] array2 = new int[array.length+1];
//循环拷贝
for(int i=0;i<array.length;i++){
array2[i] = array[i];
}
//把a的值赋给array2数组的最后一个元素
array2[array.length] = a;
//数组交换
array = array2;
}

例:
MyFrame.java

package cn.netjava.repaint;

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;

/**
* 窗体类
* @author ssss
*
*/
public class MyFrame extends JFrame{
//定义属性
Graphics g;
String command;

/** 主函数 */
public static void main(String[] args){
//实例化MyFrame对象
MyFrame mf = new MyFrame();
//调用显示窗体的方法
mf.showUI();
}

/** 显示窗体的方法 */
public void showUI(){
//设置窗体的标题
this.setTitle("简单窗体");
//设置窗体的大小
this.setSize(400,400);
//设置窗体显示位置居中
this.setLocationRelativeTo(null);
//设置窗体关闭时程序退出
this.setDefaultCloseOperation(3);
//设置窗体布局
this.setLayout(new FlowLayout());

//添加按钮
JButton btn_line = new JButton("line");
JButton btn_rect = new JButton("rect");
JButton btn_oval = new JButton("oval");

this.add(btn_line);
this.add(btn_rect);
this.add(btn_oval);

//以匿名内部类的形式创建事件监听器对象
ActionListener listener1 = new ActionListener(){

public void actionPerformed(ActionEvent e){
//获取动作命令,让计算机知道我们是点击了哪个按钮
command = e.getActionCommand();
}
};

btn_line.addActionListener(listener1);
btn_rect.addActionListener(listener1);
btn_oval.addActionListener(listener1);




//设置窗体可见
this.setVisible(true);
//获取画布对象
g = this.getGraphics();

//以匿名内部类的形式创建一个鼠标监听器对象
MouseListener listener = new MouseAdapter(){
//定义坐标
int x1,y1,x2,y2;

//重写我们需要的方法
//鼠标按下时的操作
public void mousePressed(MouseEvent e){

x1 = e.getX();
y1 = e.getY();
}

//鼠标释放时的操作
public void mouseReleased(MouseEvent e){
x2 = e.getX();
y2 = e.getY();

Shape shape = new ImpLine(x1,y1,x2,y2);

//判断
if("line".equals(command)){
shape = new ImpLine(x1,y1,x2,y2);
}else if("rect".equals(command)){
shape = new ImpRect(x1,y1,x2,y2);
}else if("oval".equals(command)){
shape = new ImpOval(x1,y1,x2,y2);
}

//调用画图的方法
shape.draw(g);

MyList.add(shape);
}
};

//给窗体添加鼠标监听
this.addMouseListener(listener);

}


/** 重绘的方法 */
public void paint(Graphics g){
super.paint(g);

Shape[] shapes = MyList.array;
for(int i=0;i<shapes.length;i++){
shapes[i].draw(g);
}
}
}

MyList.java

package cn.netjava.repaint;
/**
* 队列类
* @author ssss
*
*/
public class MyList {
//定义整型数组
static Shape[] array = new Shape[0];

//实现数组动态变化,也就是每次执行该方法时数组的长度加1
public static void add(Shape shape){
//创建一个新的数组,长度为array数组长度加1
Shape[] array2 = new Shape[array.length+1];
//循环拷贝
for(int i=0;i<array.length;i++){
array2[i] = array[i];
}
//把a的值赋给array2数组的最后一个元素
array2[array.length] = shape;
//数组交换
array = array2;
}

Shape.java

package cn.netjava.repaint;

import java.awt.Graphics;

/**
* 抽象类
* @author ssss
*
*/
public abstract class Shape {
//定义共有属性
public int x1,y1,x2,y2;

//定义抽象方法
abstract void draw(Graphics g);

}

ImpLine.java

package cn.netjava.repaint;

import java.awt.Graphics;

/**
* 画直线的类,继承Shape类
* @author ssss
*
*/
public class ImpLine extends Shape{


//构造方法
public ImpLine(int x1,int y1,int x2,int y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}


//实现抽象方法draw()
public void draw(Graphics g){
g.drawLine(x1, y1, x2, y2);
}
}

ImpOval.java

package cn.netjava.repaint;

import java.awt.Graphics;
/**
* 画椭圆的类,继承Shape类
* @author ssss
*
*/
public class ImpOval extends Shape{
//构造方法
public ImpOval(int x1,int y1,int x2,int y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}


//实现抽象方法draw()
public void draw(Graphics g){
g.drawOval(x1, y1, x2, y2);
}
}

ImpRect.java

package cn.netjava.repaint;

import java.awt.Graphics;
/**
* 画矩形的类,继承Shape类
* @author ssss
*
*/
public class ImpRect extends Shape{


//构造方法
public ImpRect(int x1,int y1,int x2,int y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}


//实现抽象方法draw()
public void draw(Graphics g){
g.drawRect(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
}
}


读书人网 >编程

热点推荐