Java-图形重绘

Java图形重绘分享───如何在拖动,放大,缩小容器组件后,保留已经绘制的内容。
通常我们所使用的画图板,都有对已经做出图形的保留功能。但我们使用java语言编写的画图板是不具备自动保存图形功能的,这就需要我们自己来通过代码的使用来保存已经存在的图形。
要实现这一功能,我们需要首先思考一个问题。为什么我们所绘制的图形会消失呢?容器组件不是依然存在吗?
其实,java是通过调用系统底层的画图函数来实现容器以及元素的绘制。在我们每一次进行拖动,放大,缩小动作后,java语言都会自动调用一次,为我们重新绘制了框体。这样,我们之前所作的图形没有经过任何保留操作,自然也就消失了。
为了在重绘窗体之后,保留我们所作的图形,我们必须对 JFrame类的paint 方法进行重写。这样不但进行了窗体的重绘,我们所作的图形也能进行重绘。
重写的方法`

public class Drawing extends JFrame{

         public BaseShape [] array=new BaseShape[1000000];     //建立一个对象数组来存储已经绘制的图形,数组大小可变。BaseShape类在后面写出。
         public static void main(String [] args)
	{
		Drawing3 dr=new Drawing3();
		dr.draw();
	}
         public void paint(Graphics g)               //方法重写
    {
        super.paint(g);      //   这里通过调用父类的paint()方法来绘制窗体
        for(int i=0;i<=array.length;i++)
		{
			if(array[i]!=null)  array[i].repaint(g);       //调用对象的repaint()方法,实现图形的重新绘制。
			else break;
		}

public void draw()
	{
		//框架构建
		//这里说明一下this的用法,因为我们没有实例化框体对象。只能使用this代替对象,这里的this就是框体
		this.setTitle("Drawing3");      //    设置标题
		this.setSize(980,1000);          //设置大小  
		this.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
		this.setDefaultCloseOperation(3);
		this.setLocationRelativeTo(null);

		DrawingListener3 dlu=new DrawingListener3();
		//图形数组
	    String [] shape= {"直线","曲线","虚线","Rectangles","Circles","Text","Pictures","Triangles","cubes"};

	    for(int i=0;i<shape.length;i++)
	    {
	    	JButton jb=new JButton(shape[i]);
	    	jb.setPreferredSize(new Dimension(100,50));
	    	this.add(jb);
	        jb.addActionListener(dlu);
	        jb.setActionCommand(shape[i]);
	    }
	    
        this.setVisible(true);     //可视化处理
                           
	    this.addMouseListener(dlu);
		this.addMouseMotionListener(dlu);
	    dlu.gd= this.getGraphics();
	    dlu.array=this.array;        //使主函数与监听器的数组为同一数组
	 		
	 	
}
}

实现了JFrame类的重写,接下来我们要自己定义一个BaseShape类,用以储存图形
`

    public int x1,y1,x2,y2;
	public int height,width;
	public Color color;
	public int size;
	public String type;
	public ImageIcon icon;
	public BaseShape(int x1,int y1,int x2,int y2,Color color,int size,String type)   //  构造方法
	{
		this.x1=x1;
		this.x2=x2;
		this.y1=y1;
		this.y2=y2;
		this.color=color;
		this.size=size;
		this.type=type;
		
	}

	public void repaint(Graphics g)                //重新绘制图形的方法,在重绘时调用
	{
		
		Graphics2D g2d = (Graphics2D) g;// 将画笔强制转换为Graphics2D类型
		g2d.setStroke(new BasicStroke(size));// 设置画笔粗细
		g.setColor(color);      
		g.drawLine(x1, y1, x2, y2);         //核心代码,通过这一操作重绘图形
}

我们要在监听器中添加相应的代码

public BaseShape [] array;//   在这里只声明,定义在上面的代码块。
public int index;     //计数器,防止数组越界
public void mouseReleased(MouseEvent e)
	{
			x2=e.getX();  //得到点击的横坐标
			y2=e.getY();  //得到点击的纵坐标
			if(type.equals("直线"))
			{
				gd.drawLine(x1, y1, x2, y2);
				//调用构造方法,将绘制的图形储存在数组中
				if(index<1000000)array[index++]=new  BaseShape(x1, y1, x2, y2,color,size,"直线");
			}
	}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值