进行重绘图形

第一步建立一个界面

public class DrawUI extends JFrame {
public void initUI{
this.setTitle("重绘“);
this.setSize(800,800);
this.setDefaultCloseOpreation(EXIT_ON_CLOSE);
this.setVisible("true");
}
}

先在DrawUI类中声明数组,以存放数组对象

public class DrawUI extends JFrame{
        String strs[]={"直线","三角形","矩形","多边形","实心矩形","圆形","实心圆","立方体","签字笔","实时直线","实时矩形","橡皮擦","进入"};
        Color[] strc={Color.BLACK,Color.WHITE,Color.BLUE,Color.CYAN};
}

对该数组采用for循环,方便在按钮中实现调用

public class DrawUI extends JFrame{
 for (int i=0;i<strs.length;i++){
            JButton jButton=new JButton(strs[i]);
            jButton.setPreferredSize(dI);
            add(jButton);
            jButton.addActionListener(dL);
        }
        for (int j=0;j<strc.length;j++){
            JButton jButton=new JButton();
            jButton.setBackground(strc[j]);
            jButton.setPreferredSize(dI);
            add(jButton);
            jButton.addActionListener(dL);
        }
}

该dL为监听器对象,因为在这个程序里需要用到监听器

public class DrawUI extends JFrame{
DrawListener dL=new DrawListener();
  Dimension dI=new Dimension(90,30);//为按钮设置大小
}

这样的话,我们需要另建一个新类DrawListener去声明监听器,并重写接口中的方法

public class DrawListener implements ActionListener, MouseListener, MouseMotionListener {
public void actionPerformed(ActionEvent e){
}
 public void mouseClicked(MouseEvent e){}

    /**
     * Invoked when a mouse button has been pressed on a component.
     */
    public void mousePressed(MouseEvent e){}

    /**
     * Invoked when a mouse button has been released on a component.
     */
    public void mouseReleased(MouseEvent e){}

    /**
     * Invoked when the mouse enters a component.
     */
    public void mouseEntered(MouseEvent e){}

    /**
     * Invoked when the mouse exits a component.
     */
    public void mouseExited(MouseEvent e);{}
  public void mouseDragged(MouseEvent e);

    /**
     * Invoked when the mouse cursor has been moved onto a component
     * but no buttons have been pushed.
     */
    public void mouseMoved(MouseEvent e);

}

我们继续看DrawUI类的内容,将按钮,监听器都添加到initUI方法中

public void inintUI{ 
addMouseListener(dL);
     addMouseMotionListener(dL);
     Graphics g=getGraphics();//获取界面上的操作,赋值给DrawListener中的g,进行去绘图
     dL.g=g;
     String str="aaaa";
     dL.str=str;
}

另外设置一个新的方法paint,该方法用于重绘,保存图形。

 int count;
    public void paint(Graphics g){
        super.paint(g);
        count++;
        for(Shape shape : dL.shapelist) {
            if (shape != null)//因为界面在操作者没有进行绘制的时候,本身就进行了一次绘制,发生了越界,所以设置为空,当不进行重绘的时候,不去执行,防止出现越界错误。
 {
                shape.drawPaint(g);//绘制
            }
        }
        System.out.println("paint 调用次数"+count);
    }

重绘图形需要建立一个新类Shape,作用是保存,重绘图形

public class Shape {
//在类内方法外定义在接口方法内能够实现绘制图形的对象
 public void drawPaint(Graphics g){
//在方法内设置绘制图形的方法,只需要方法即可
}
}

重绘需要与接口中的绘制方法有所联系,需要将Shape类用到的名字,颜色,对象赋值给Shape的对象

public class DrawListener implements ActionListener, MouseListener, MouseMotionListener {
    Graphics g;
    String str;
    int x1,x2,y1,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8;
    String shapestr;
    Shape[] shapelist=new  Shape[100];
    int shapeindex;
public void mouseReleased(MouseEvent e){
        //释放
        x2=e.getX();
        y2=e.getY();
        if(shapestr.equals("直线")){
            g.drawLine(x1,y1,x2,y2);
            Shape shape=new Shape();
            shape.shapeName="直线";
            shape.color=Color.red;
            shape.x1=x1;
            shape.x2=x2;
            shape.y1=y1;
            shape.y2=y2;
            shapelist[shapeindex++]=shape;


        }
        int x = e.getX ();
        int y = e.getY ();
        if(shapestr.equals ("矩形")){
            g.drawRect (Math.min (x1, x2), Math.min (y1, y2), Math.abs (x2 - x1), Math.abs (y2 - y1));
            Shape shape=new Shape();
            shape.shapeName="矩形";
            shape.color=Color.red;
            shape.x1=x1;
            shape.y1=y1;
            shape.x2=x2;
            shape.y2=y2;
            shapelist[shapeindex++]=shape;
        }
        if(shapestr.equals ("等腰三角形")){
            g.drawLine (Math.min (x1, x2), Math.max (y1, y2), (x1 + x2) / 2, Math.min (y1, y2));
            g.drawLine ((x1 + x2) / 2, Math.min (y1, y2), Math.max (x1, x2), Math.max (y1, y2));
            g.drawLine (Math.min (x1, x2), Math.max (y1, y2), Math.max (x1, x2), Math.max (y1, y2));
            Shape shape=new Shape();
            shape.shapeName="等腰三角形";
            shape.color=Color.red;
            shape.x1=x1;
            shape.y1=y1;
            shape.x2=x2;
            shape.y2=y2;
            shapelist[shapeindex++]=shape;
        }

这三个图形作为例子,在接口中需要建立一个数组存储数据。

之后需要在Shape类中去定义这三个图形用到的对象和方法

public class Shape {
    String shapeName;
    int x,y,x1,y1,x2,y2;
    Color color;
    public void drawPaint(Graphics g){
        g.setColor(color);
        if(shapeName.equals("直线")){
            g.drawLine(x1,y1,x2,y2);}
       else if (shapeName.equals("矩形")){
            g.drawRect (Math.min (x1, x2), Math.min (y1, y2), Math.abs (x2 - x1), Math.abs (y2 - y1));}else if (shapeName.equals("等腰三角形")){
            g.drawLine (Math.min (x1, x2), Math.max (y1, y2), (x1 + x2) / 2, Math.min (y1, y2));
            g.drawLine ((x1 + x2) / 2, Math.min (y1, y2), Math.max (x1, x2), Math.max (y1, y2));
            g.drawLine (Math.min (x1, x2), Math.max (y1, y2), Math.max (x1, x2), Math.max (y1, y2));}
}

实现图形重绘,不消失就是这样了,最后在DrawUI中创建一个主方法就ok了。

  public static void main(String[] args){
        DrawUI drawUI=new DrawUI();
        drawUI.initUI();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值