重绘是指将图形的绘制所需要的关键元素保存下来,当对页面进行更改时,图形能够重新绘制,不会丢失。
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
public class NWDrawUI extends JFrame {
DListener lis1 = new DListener();
public static void main(String[] args){
NWDrawUI nw = new NWDrawUI();
nw.showUI();
}
public void showUI(){
this.setSize(1000,700);
//创建窗体位置居中
this.setLocationRelativeTo(null);
//创建窗体关闭时的操作
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建窗体的标题
this.setTitle("画图板界面-3.0");
//创建布局
this.setLayout(new FlowLayout());
String[] textArr = {"画线","直线","矩形边","椭圆边","矩形填充","椭圆填充","橡皮擦","颜色选择器"};
for(int i=0;i
创建的画图板监听器的代码::
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JColorChooser;
import javax.swing.JTextField;
public class DListener implements MouseListener, ActionListener, MouseMotionListener {
// 记录坐标
int x1,x2,y1,y2;
Color color =Color.BLACK;
// 画布
public Graphics g;
//保存图形信息
String shapeText="画线";
//存储图形的数组
int count = 0;
//获取按钮的信息
public void actionPerformed(ActionEvent e) {
shapeText = e.getActionCommand();
System.out.println(shapeText);
if(shapeText.equals("颜色选择器")) {
JColorChooser Jcolor = new JColorChooser();
color = Jcolor.showDialog(Jcolor, null, color);
g.setColor(color);
}
}
//构造方法
public void mousePressed(MouseEvent e) {
// 获取按下的坐标
x1 = e.getX();
y1 = e.getY();
}
public void mouseDragged(MouseEvent e) {
if (shapeText.equals("画笔")){
System.out.println("已经点击了按钮画线");
x2 = e.getX();
y2 = e.getY();
g.drawLine(x1, y1, x2, y2);
System.out.println("x1: "+x1+"y1: "+y1);
x1 = x2;
y1 = y2;
}
}
public void mouseReleased(MouseEvent e) {
// 获取松开的坐标
x2 = e.getX();
y2 = e.getY();
if(shapeText.equals("直线"))
{
System.out.println("直线");
g.drawLine(x1, y1, x2, y2);
}
else if(shapeText.equals("矩形边"))
{
g.drawRect(Math.min(x1, x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
}
else if(shapeText.equals("矩形填充"))
{
g.fillRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
}
else if(shapeText.equals("椭圆边"))
{
g.drawOval(Math.min(x1, x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
}
else if(shapeText.equals("椭圆填充"))
{
g.fillOval(Math.min(x1, x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
}
else if(shapeText.equals("橡皮擦")){
Color color = new Color(238,238,238);
g.setColor(color);
g.fillOval(Math.min(x1, x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
}
}
public void mouseMoved(MouseEvent e) {};
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {};
public void mouseExited(MouseEvent e) {};
}
运行结果如下:
当进行放大操作后:
原先的图形发生了丢失的情况
图形消失的原因:因为Java画板都是画出来的,当你进行放大操作时,对界面进行了重新绘制,但是原来所画的图形 并没有重新显示出来,因此需要进行重新绘制
1 . 重新创建一个类,目的是存储画板上图形的数据,并建立方法,利用这些数据,把图形在新的界面绘制出来。
这是新创建的Shap类用于重绘
import java.awt.Color;
import java.awt.Graphics;
public class Shap {
String type;
int x1,y1,x2,y2;
public Shap(int x1, int y1, int x2, int y2, String type) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
this.type = type;
}
//绘制自己
public void show(Graphics g){
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.fillRect(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.fillOval(Math.min(x1, x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
}
else if(type.equals("橡皮擦")){
Color color = new Color(238,238,238);
g.setColor(color);
g.fillOval(Math.min(x1, x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
}
}
}
2 . 对主方法也要进行一定的修改
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
public class NWDrawUI extends JFrame {
Shap date[] = new Shap[100000];//所增添的内容
DListener lis1 = new DListener(date);//所增添的内容
public static void main(String[] args){
NWDrawUI nw = new NWDrawUI();
nw.showUI();
}
public void showUI(){
this.setSize(1000,700);
//创建窗体位置居中
this.setLocationRelativeTo(null);
//创建窗体关闭时的操作
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建窗体的标题
this.setTitle("画图板界面-3.0");
//创建布局
this.setLayout(new FlowLayout());
String[] textArr = {"画线","直线","矩形边","椭圆边","矩形填充","椭圆填充","橡皮擦","颜色选择器"};
for(int i=0;i
Shap date[] = new Shap[100000];//所增添的内容
DListener lis1 = new DListener(date);//所增添的内容
建立一个数组存储对象
public void paint(Graphics g) {
super.paint(g); //继承父类的方法
//根据数组所存储的数据进行重绘
for (int i = 0; i < date.length; i++) {
if (date[i] != null) {
date[i].show(g);
}
}
监听器的代码如下:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JColorChooser;
import javax.swing.JTextField;
public class DListener implements MouseListener, ActionListener, MouseMotionListener {
// 记录坐标
int x1,x2,y1,y2;
Color color =Color.BLACK;
// 画布
public Graphics g;
//保存图形信息
String shapeText="画线";
//存储图形的数组
int count = 0;
Shap[] h;
public DListener(Shap[] s){
this.h = s;
}
//获取按钮的信息
public void actionPerformed(ActionEvent e) {
shapeText = e.getActionCommand();
System.out.println(shapeText);
if(shapeText.equals("颜色选择器")) {
JColorChooser Jcolor = new JColorChooser();
color = Jcolor.showDialog(Jcolor, null, color);
g.setColor(color);
}
}
//构造方法
public void mousePressed(MouseEvent e) {
// 获取按下的坐标
x1 = e.getX();
y1 = e.getY();
}
public void mouseDragged(MouseEvent e) {
if (shapeText.equals("画笔")){
System.out.println("已经点击了按钮画线");
x2 = e.getX();
y2 = e.getY();
g.drawLine(x1, y1, x2, y2);
System.out.println("x1: "+x1+"y1: "+y1);
x1 = x2;
y1 = y2;
}
}
public void mouseReleased(MouseEvent e) {
// 获取松开的坐标
x2 = e.getX();
y2 = e.getY();
if(shapeText.equals("直线"))
{
System.out.println("直线");
g.drawLine(x1, y1, x2, y2);
}
else if(shapeText.equals("矩形边"))
{
g.drawRect(Math.min(x1, x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
}
else if(shapeText.equals("矩形填充"))
{
g.fillRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
}
else if(shapeText.equals("椭圆边"))
{
g.drawOval(Math.min(x1, x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
}
else if(shapeText.equals("椭圆填充"))
{
g.fillOval(Math.min(x1, x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
}
else if(shapeText.equals("橡皮擦")){
Color color = new Color(238,238,238);
g.setColor(color);
g.fillOval(Math.min(x1, x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
}
// 创建一个图形对象
Shap a =new Shap(x1,y1,x2,y2,shapeText);
h[count] = a;
count++;
}
public void mouseMoved(MouseEvent e) {};
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {};
public void mouseExited(MouseEvent e) {};
}
Shap[] h;
public DListener(Shap[] s){
this.h = s;
}
对监听器进行处理,使数组间进行传值,保持监听器的一致性。
接下来进行运行,便可以实现重绘的操作。