`
小篮子java的家
  • 浏览: 32087 次
  • 性别: Icon_minigender_2
社区版块
存档分类
最新评论

重绘-刷新的程序语言

阅读更多
画板实现重绘和橡皮擦


重绘的实现主要在首先要自定义一个通用队列  然后再处理鼠标监听的
类中 实例化静态一个队列对象 以方便直接调用 然后画完各种图形后
将画图的对象加载到队列中保存  然后再重写paint函数 首先调用父类的paint函数
再利用循环调用加载在队列中的画图对象

橡皮擦首先是创建按钮 然后按处理曲线的方法一样处理监听器 唯一的不同之处是
首先在传递颜色的参数时  将颜色定义成画布颜色一致 RGB的(238 238 238)

package net.java.t071301;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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


public class DrawFrame extends JFrame{

	public static  void main (String args []){
		
		   DrawFrame df = new DrawFrame();
		   df.init();
	}

	public void init() {
	   this.setTitle("简易画板");
	   this.setLocationRelativeTo(null);
	   this.setDefaultCloseOperation(2);
	   this.setSize(600, 400);
	   this.setResizable(false);
	   FlowLayout fl= new  FlowLayout();
	   this.setLayout(fl);
	   JButton jbt1= new JButton("直线");
	   jbt1.setActionCommand("line");
	   this.add(jbt1);
	   JButton jbt2= new JButton("曲线");
	   jbt2.setActionCommand("curve");
	   this.add(jbt2);
	   JButton jbt3= new JButton("圆形");
	   jbt3.setActionCommand("oval");
	   this.add(jbt3);
	   JButton jbt4= new JButton("矩形");
	   jbt4.setActionCommand("rect");
	   this.add(jbt4);
	   JButton jbt5= new JButton("填充圆");
	   jbt5.setActionCommand("filloval");
	   this.add(jbt5);
	   JButton jbt6= new JButton("填充矩形");
	   jbt6.setActionCommand("fillrect");
	   this.add(jbt6);
	   JButton jbt7= new JButton("颜色");
	   jbt7.setActionCommand("color");
	   this.add(jbt7);
	   JButton jbt8= new JButton("橡皮擦");
	   jbt8.setActionCommand("erase");
	   this.add(jbt8);
	   
	   ActionListener al = new ActionListener(){
		 
		public void actionPerformed(ActionEvent e) {
	
			 if(e.getActionCommand()=="color" ){
				  color = JColorChooser.showDialog(null,"颜色选择器",Color.BLACK);	
			   }
			 else{
				 items = e.getActionCommand();
				 }
		}
	   };
	   
	   jbt1.addActionListener(al);
	   jbt2.addActionListener(al);
	   jbt3.addActionListener(al);
	   jbt4.addActionListener(al);
	   jbt5.addActionListener(al);
	   jbt6.addActionListener(al);
	   jbt7.addActionListener(al);
	   jbt8.addActionListener(al);
	  
	   this.setVisible(true);
	   
	   Graphics g = this.getGraphics();
	  
	   DrawListener dl = new DrawListener(g,this);
	   
	   this.addMouseListener(dl);
	   this.addMouseMotionListener(dl);
	 
	    }
	  //重写一个绘制方法
	 public void paint(Graphics g){
		   super.paint(g);
		   for(int i=0;i<DrawListener.qi.size();i++){
			   Draw draw=DrawListener.qi.get(i);
			   draw.draw(g);
		   }
	   
	}
	public Color getColor(){
		return color;
	}
	public String getItems(){
		return items;
	}
	private Color color =Color.BLACK;
	private String items="line";
}


package net.java.t071301;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class DrawListener implements MouseListener, MouseMotionListener{
          
	private Graphics g;
	private DrawFrame df;
	int x1,y1,x2,y2;
	public static QueueIplm<Draw> qi=new QueueIplm<Draw> ();
	public DrawListener(Graphics g , DrawFrame df){
		this.g=g;
		this.df=df;
	}

	public void mousePressed(MouseEvent e) {
		x1=e.getX();
		y1=e.getY();	
	}

	public void mouseReleased(MouseEvent e) {
	      x2=e.getX();
	      y2=e.getY();
	      Draw draw;
	    if(df.getItems().equals("line")){
	    	draw = new DrawLine(x1,y1,x2,y2,df.getColor());
	    	draw.draw(g); 
	    	 qi.add(draw);
	    } 
	    else  if(df.getItems().equals("oval")){
	    	draw = new DrawOval(x1,y1,Math.abs(x2-x1), Math.abs(y2-y1),df.getColor());
	    	draw.draw(g);
	    	 qi.add(draw);
	    }
	    else  if(df.getItems().equals("rect")){
	    	draw = new DrawRect(x1,y1,Math.abs(x2-x1), Math.abs(y2-y1),df.getColor());
	    	draw.draw(g);
	    	 qi.add(draw);
	    }
	    else  if(df.getItems().equals("fillrect")){
	    	draw = new DrawFillRect(x1,y1,Math.abs(x2-x1), Math.abs(y2-y1),df.getColor());
	    	draw.draw(g);
	    	 qi.add(draw);
	    }
	    else  if(df.getItems().equals("filloval")){
	    	draw = new DrawFillOval(x1,y1,Math.abs(x2-x1), Math.abs(y2-y1),df.getColor());
	    	draw.draw(g);
	    	 qi.add(draw);
	    }
	   
	}
      public void mouseDragged(MouseEvent e) {
    	  x2=e.getX();
	      y2=e.getY();
	      Draw draw;
	    if(df.getItems().equals("curve")){
	    	draw = new DrawLine(x1,y1,x2,y2,df.getColor());
	    	draw.draw(g);
	    	x1=x2;
	    	y1=y2;
	    	qi.add(draw);}
	    else if(df.getItems().equals("erase")){
	    	Color cr = new Color(238,238,238);
	    	float[] hsbvals = null;
	    	float[] objx=cr.RGBtoHSB(238, 238, 238, hsbvals);
	    	float a=objx[0];
	    	float b=objx[1];
	    	float c=objx[2];
	    draw = new DrawLine(x1,y1,x2,y2,Color.getHSBColor(a, b, c));
     	draw.draw(g);
    	x1=x2;
    	y1=y2;
    	qi.add(draw);
	    } 
	}  
	    
	public void mouseEntered(MouseEvent e) {
	}
	
	public void mouseExited(MouseEvent e) {	
	}
	
	public void mouseMoved(MouseEvent e) {
	}
	
	public void mouseClicked(MouseEvent e) {
	}
	}


ackage net.java.t071301;

import java.awt.Graphics;

public abstract class Draw {
	
       public abstract void draw(Graphics g);
}


package net.java.t071301;

import java.awt.Color;
import java.awt.Graphics;

public class Drawerase extends Draw{
        private Color color;
        private int x1,y1,x2,y2;
	
	public	Drawerase(int x1,int y1,int x2,int y2,Color color){
		this.x1=x1;
		 this.y1=y1;
		 this.x2=x2;
		 this.y2=y2;
		 this.color=color;
		}

	public void draw(Graphics g) {
		 g.setColor(color);
	     g.drawLine(x1, y1, x2, y2);
	}
  
}


ackage net.java.t071301;

import java.awt.Color;
import java.awt.Graphics;

public class DrawFillOval extends Draw{
	 private int x1,y1,x2,y2;
	 private  Color color;
	 
	public DrawFillOval(int x1,int y1,int x2,int y2, Color color){
		 this.x1=x1;
		 this.y1=y1;
		 this.x2=x2;
		 this.y2=y2;
		 this.color=color;
	}

	public void draw(Graphics g) {
		g.setColor(color);
		g.fillOval(x1, y1, x2, y2);
	}

}


package net.java.t071301;

import java.awt.Color;
import java.awt.Graphics;

public class DrawFillRect extends Draw{
	 private int x1,y1,x2,y2;
	 private  Color color;
	 
	public DrawFillRect(int x1,int y1,int x2,int y2, Color color){
		 this.x1=x1;
		 this.y1=y1;
		 this.x2=x2;
		 this.y2=y2;
		 this.color=color;
	}

	public void draw(Graphics g) {
		g.setColor(color);
		g.fillRect(x1, y1, x2, y2);
	}

}



ackage net.java.t071301;

import java.awt.Color;
import java.awt.Graphics;

public class DrawLine extends Draw{
	 private int x1,y1,x2,y2;
	 private  Color color;
	public DrawLine(int x1,int y1,int x2,int y2, Color color){
		 this.x1=x1;
		 this.y1=y1;
		 this.x2=x2;
		 this.y2=y2;
		 this.color=color;
	}

	public void draw(Graphics g) {
		g.setColor(color);
		g.drawLine(x1, y1, x2, y2);
	}

}


package net.java.t071301;

import java.awt.Color;
import java.awt.Graphics;

public class DrawOval extends Draw{
	 private int x1,y1,x2,y2;
	 private  Color color;
	 
	public DrawOval(int x1,int y1,int x2,int y2, Color color){
		 this.x1=x1;
		 this.y1=y1;
		 this.x2=x2;
		 this.y2=y2;
		 this.color=color;
	}

	public void draw(Graphics g) {
		g.setColor(color);
		g.drawOval(x1, y1, x2, y2);
	}

}



package net.java.t071301;

import java.awt.Color;
import java.awt.Graphics;

public class DrawRect extends Draw{
	 private int x1,y1,x2,y2;
	 private  Color color;
	 
	public DrawRect(int x1,int y1,int x2,int y2, Color color){
		 this.x1=x1;
		 this.y1=y1;
		 this.x2=x2;
		 this.y2=y2;
		 this.color=color;
	}

	public void draw(Graphics g) {
		g.setColor(color);
		g.drawRect(x1, y1, x2, y2);
	}

}



package net.java.t071301;

public interface Queue<E> {
	
	//得到指定位置的对象
    public E get(int index);
    
    //加入一个对象
    public void  add(E e);
    
    //在指定的位置插入一个对象
    public void insert (E e, int index);
    
    public void addall(QueueIplm<E>  list);
    
    //删除指定位置的对象
    public E delete(int index);
   
    //得到队列中的元素
    public int size();    
}

package net.java.t071301;

public class QueueIplm<E> implements Queue<E>{
 
	private  Object[] obj=null;
	private int initCount =0;
	private int incresCount =1;
	
	public QueueIplm(){ 
		 obj = new Object[0];
	}
	public QueueIplm(int initCount){ 
		this.initCount=initCount;
		 obj = new Object[initCount];
	}
	public QueueIplm(int initCount, int incresCount){ 
		this.initCount=initCount;
		this.incresCount=incresCount;
		 obj = new Object[initCount];
	}
	
	
	public void add(E e) {
		Object[] temp = new Object[obj.length + incresCount];
		for (int i = 0; i < obj.length; i++) {
			temp[i] = obj[i];
		}
		temp[obj.length] = e;
		this.initCount++;
		obj = temp;
	}

	public void addall(QueueIplm<E> list) {
		Object[] temp = new Object[obj.length + list.size()];
		for (int i = 0; i < obj.length; i++) {
			temp[i] = obj[i];
		}
		for (int i = 0; i < list.size(); i++) {
			temp[i + obj.length] = list.get(i);
			this.initCount++;
		}
		obj = temp;
		
	}


	public E delete(int index) {
		E item = (E) obj[index];
		for (int i = index; i < obj.length; i++) {
			obj[i] = obj[i + 1];
		}
		this.initCount--;
		return item;
	}
	public E get(int index) {
		if (index < 0 || index > initCount)
			return null;
		E item = (E) obj[index];
		return item;
		
	}


	public void insert(E e,int index) {
		obj[index] = e;
	}


	public int size(){
		return initCount;
		
	}

}

分享到:
评论
1 楼 Fhappy 2011-10-22  
应该要有点注释的

相关推荐

    解决重绘时候闪屏问题的实例程序

    当一个应用程序需要更新其显示内容时,比如窗口大小变化、滚动操作或数据刷新,操作系统会执行一个称为“重绘”的过程。这个过程中,旧的内容被清除,新的内容被绘制。如果这个过程处理不当,尤其是在短时间内频繁...

    JAVA中的控件重绘

    在Java编程语言中,控件重绘是一个关键概念,它涉及到图形用户界面(GUI)的动态更新和定制。当我们谈论控件重绘时,我们通常指的是如何改变或更新已存在的组件外观,使其根据用户交互或其他事件进行刷新。下面将...

    如何实现重绘

    在不同的编程语言和框架中,实现重绘的方法各异,但核心思想都是通知系统某部分需要刷新。 在Windows API中,重绘操作通常通过`InvalidateRect`函数触发,该函数将指定区域标记为无效,随后系统会在下一次消息循环...

    易语言源码易语言刷新窗口源码.rar

    易语言是一种专为中国人设计的编程语言,它以简体中文作为编程语句,降低了编程的门槛,使得更多非计算机专业的人也能参与到程序开发中来。"易语言刷新窗口源码"是关于易语言中一个特定功能的源代码示例,主要涉及到...

    史上最高级最全面的刷新控件

    7. **兼容性**:MJRefresh库兼容Objective-C和Swift两种编程语言,覆盖了大部分iOS开发者的需求。 8. **示例项目**:在MJRefresh-master中,通常会包含一个示例项目,演示了如何在实际应用中使用这个库,这对于初学...

    MFC下好用的高速绘图控件-(Hight-Speed Charting)

    为了实现这一点,开发者可能需要掌握动态数据更新的策略,比如只重绘改变的部分,而不是整个图表。此外,有效的内存管理也是关键,特别是在处理大量数据时,防止内存泄漏和不必要的资源消耗。 标签中的"vc c++"表明...

    易语言通用刷新显示

    易语言是一种专为中国人设计的编程语言,它以简体中文作为编程语法,降低了编程的门槛,使得更多非计算机专业的人也能进行程序开发。在易语言中,“通用刷新显示”是一个重要的概念,它涉及到图形用户界面(GUI)的...

    e语言-易语言窗口自绘效果

    易语言是一种专为中国人设计的编程语言,它以简化的语法和中文编程为特色,旨在降低编程门槛,让更多的人能够参与到编程中来。在“易语言窗口自绘效果”这个主题中,我们主要探讨的是如何利用易语言及其相关支持库来...

    易语言源码易语言刷新桌面源码.rar

    易语言是一种专为初学者设计的编程语言,其目标是降低编程的门槛,使得更多的人能够参与到编程活动中来。易语言具有直观的汉字编程语法,让编程变得更加简单易懂。"刷新桌面源码"则是易语言中实现的一种功能,它主要...

    易语言刷新桌面模块

    易语言是一种专为中国人设计的编程语言,它以简化的汉字作为编程语句,使得编程更加直观易懂。"易语言刷新桌面模块"是基于易语言编写的代码模块,主要用于实现计算机桌面的刷新功能。在Windows操作系统中,桌面刷新...

    基于ChartCtrl源码的动态曲线实时刷新显示Demo

    2. **事件驱动编程**:为了实现动态刷新,开发者可能使用了定时器(如Windows的消息定时器)来定期触发数据更新和图表重绘,这是一种常见的事件驱动编程模式。 3. **数据结构与算法**:为了存储和处理动态曲线的...

    易语言双缓冲绘图-易语言

    易语言是一种专为中国人设计的编程语言,它以简化的语法和直观的界面著称,降低了编程的入门难度。在易语言中,双缓冲绘图是一个重要的技术,它主要用于提高图形绘制的效率和质量,避免在窗口重绘时出现闪烁现象,...

    易语言刷新系统托盘

    在刷新系统托盘的代码中,我们可能会使用SendMessage来向系统托盘区域发送特定的消息,例如WM_NULL(0x0000)用于强制窗口重绘,或者WM_USER定义自定义消息,以达到刷新托盘区域的目的。 在实际开发中,"易语言刷新...

    易语言刷新桌面

    易语言是一种专为中国人设计的编程语言,它的目标是让编程变得简单易学。"刷新桌面"是一个常见的操作,尤其在编程中,我们有时需要更新显示以反映程序运行中的变化。在易语言中,实现这一功能涉及到一些核心概念和...

    Qt动态语言切换

    为了确保界面的文本即时更新,可能需要重绘某些部件。对于`QWidget`子类,可以调用`update()`或`repaint()`;对于`QGraphicsItem`,调用`updateGeometry()`和`update()`。 7. **处理资源文件**: 如果应用包含...

    QT实现动态切换语言

    我们需要手动触发界面元素的重绘,例如通过触发一个信号。 ```cpp emit languageChanged(); ``` 然后在对应的槽函数中,更新界面元素的文本。 6. 优化和注意事项: - 对于动态加载的资源,如菜单项,可能需要在...

    窗体闪烁程序(C#源代码编写)

    窗体闪烁程序是一种常见的计算机应用程序现象,特别是在使用C#编程语言进行Windows桌面应用开发时可能会遇到。本程序的目的是通过源代码控制窗体在显示过程中的动态效果,以实现特定的视觉反馈或交互体验。在C#中,...

    易语言-重绘超级列表框自定义文字颜色易语言

    在IT行业中,易语言是一种基于汉语编程的编程语言,它以直观、易学的特性深受初学者和业余爱好者的喜爱。本主题聚焦于易语言的一个特定应用:重绘超级列表框,并实现自定义文字颜色的功能。超级列表框是常见的用户...

    基于c#.net的小型画图软件

    "实现重绘"这一特性则意味着程序具有图形刷新和更新的能力,当用户执行撤销、复制、粘贴或移动图形等操作时,画布可以即时反映出这些变化,保持画面的整洁和准确。 【标签】中的"c#"表明了这个项目的核心编程语言是...

Global site tag (gtag.js) - Google Analytics