java画图板之二——重绘直线(paint方法)

在我们的第一个简易画图板中,使用了两个类DrawFrame和DrawListener,但是有一个很大的问题,不能保存!当我们扩大或缩小画图板的大小时,之前的一起都没了。
原因在于,当我们改变画图板的时候,其实是将他重新画了一遍,而不是改变原来的大小,所以原来的自然就被重画的窗体给覆盖了,所以我们也需要重画一遍之前画的

而要重画之前画的,比如我们画了两条直线,要想把两条直线重画出来,需要先保存他们的先后顺序,画了两条,所以需要一个数组sharr[]
同时要保存关于他们的信息,直线的话为
起点坐标,末点坐标,粗细,颜色,名字,粗细和颜色暂不考虑

而直线也好,矩形也好,都是图形,所以我们需要用一个shape方法来保存
里面的参数包括(x1,y1,x2,y2,name)
所以我们需要构造一个shape类,包含shape方法,直线的一切信息作为他的私有属性

做了这些之后我们就可以使用paint方法了,这个方法是在窗体类中写的,现在我们是有三个类,一个窗体类,一个监听器,一个shape类
paint方法是JFrame中的方法,所以我们的窗体类还要继承JFrame方法
一开始就要调用super.paint(),表示继承父类中的方法
然后使用for循环,将sharr里面的保存的图像一个一个的画出来,这里可以直接复制监听器中的方法,但是太麻烦了,所以我们可以把画图的方法都移到shape类中,在监听器和窗体中分别调用,

综上:
**shape作用:创建图形对象,实现画图的具体方法
监听器作用:调用画图方法,实时绘制要画的图像,每画一次就保存一次图像信息在数组中,包括坐标,颜色等
窗体类作用:显示窗体,将数组中的图像信息重新画出来,调用画图方法**

在监听器和窗体类中都有数组,但是我们知道画布是从窗体传过去的,所以默认一切变量都从窗体传到监听器


shape类
package com.panel;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

public class MyShape {
    private int x1;
    private int y1;
    private int x2;
    private int y2;
    private String name;


    public void shape(int x1,int y1,int x2,int y2,String name) {
        this.x1=x1;
        this.y1=y1;
        this.x2=x2;
        this.y2=y2;
        this.name=name;
    }
    public void draw(Graphics g) {//画图方法
        g.drawLine(x1, y1, x2, y2);

    }
}

监听器类

package com.panel;

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

public class eListener implements MouseListener,ActionListener{
    private int x1,x2,y1,y2;
    String shapeType="";
    Graphics g;
    MyShape [] sharr;
    int count=0;

    public void setG(Graphics g) {//传画布
        this.g=g;
    }
    public void setarr(MyShape [] arr) {//传数组
        sharr=arr;
    }
    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
        x1=e.getX();
        y1=e.getY();
    }
    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        x2=e.getX();
        y2=e.getY();
        if("直线".equals(shapeType)) {
//          g.drawLine(x1, y1, x2, y2);

            //创建图形对象
            MyShape sh=new MyShape();
            sh.shape(x1, y1, x2, y2, shapeType);
            sh.draw(g);
            if(count<99)
                sharr[count++]=sh;

        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //获取动作的事件动作命令   
            String action=e.getActionCommand();                                                        
    shapeType=action;

    }
}

窗体类

package com.panel;

import java.awt.FlowLayout;
import java.awt.Graphics;

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

public class eFrame extends JFrame {//继承JFrame
    MyShape[] sharr = new MyShape[100];

    public void showUI() {
        // JFrame frame=new JFrame();
        this.setTitle("画布");
        this.setSize(1000, 600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        // 设置布局,流式布局
        this.setLayout(new FlowLayout());
        // 添加监听器
        eListener listener = new eListener();

        String[] shapeArr = { "直线" };
        for (int i = 0; i < shapeArr.length; i++) {
            JButton btn = new JButton(shapeArr[i]);
            this.add(btn);
            btn.addActionListener(listener);
        }
        this.addMouseListener(listener);
        this.setVisible(true);
        Graphics g = this.getGraphics();
        // 监听器收到画布
        listener.setG(g);
// 监听器收到数组
        listener.setarr(sharr);
    }

    public void paint(Graphics g) {
        super.paint(g);//继承父类

        System.out.println("ok");
        for (int i = 0; i < sharr.length; i++) {
            if (sharr[i] != null) {//如果有图形对象
                sharr[i].draw(g);//每个图形对象都调用一次绘图方法
            }
        }

    }
    public static void main(String[] args) {
        eFrame f = new eFrame();
        f.showUI();
    }
}

运行一下这里写图片描述
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_37465638/article/details/81145766