栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

进行重绘图形

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

进行重绘图形

第一步建立一个界面

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 

该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){}

    
    public void mousePressed(MouseEvent e){}

    
    public void mouseReleased(MouseEvent e){}

    
    public void mouseEntered(MouseEvent e){}

    
    public void mouseExited(MouseEvent e);{}
  public void mouseDragged(MouseEvent e);

    
    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();
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/984729.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号