当前位置: 技术问答>unix/linux知识
iis7站长之家
关于绘图。
来源: 互联网 发布时间:2015-05-16
本文导语: 本意:在JPanel中响应鼠标点击绘制矩形,每次绘制时前一次矩形应该消失。 实际情况:后一次鼠标点击绘制出了矩形,可是前一次的并不消失。如果先repaint(),再 drawRect(),就连本次的矩形也擦去了。 提问:怎么回...
本意:在JPanel中响应鼠标点击绘制矩形,每次绘制时前一次矩形应该消失。
实际情况:后一次鼠标点击绘制出了矩形,可是前一次的并不消失。如果先repaint(),再
drawRect(),就连本次的矩形也擦去了。
提问:怎么回事?怎么解决?
实际情况:后一次鼠标点击绘制出了矩形,可是前一次的并不消失。如果先repaint(),再
drawRect(),就连本次的矩形也擦去了。
提问:怎么回事?怎么解决?
|
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
public class TMouseMotion extends JApplet implements MouseListener
{
int x,y;
int o_x,o_y;
int flag;
int s_x;
int s_y;
int width;
int height;
public void init()
{
CustomListener ct=new CustomListener(this);
this.addMouseMotionListener(ct);
addMouseListener(this);//You missed this line
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics gdraw)
{
BufferedImage bufimg=new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_ARGB);
Graphics g=bufimg.getGraphics();
g.setColor(this.getBackground());
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.blue);
g.drawString("drag/move mouse......",5,20);
g.setColor(Color.red);
if(flag==1)
{
g.drawString("don't move!drag the mouse",5,85);
g.drawString("cursor coordinates:"+x+","+y,5,95);
}
else if(flag==2)
{
g.drawString("don't drag!move the mouse",5,85);
g.drawString("cursor coordinates:"+x+","+y,5,95);
}
g.setColor(Color.blue);
g.drawRect(s_x,s_y,width,height);
gdraw.drawImage(bufimg,0,0,null);
}
public void mousePressed(MouseEvent e)
{
o_x=e.getX();
o_y=e.getY();
}
public void mouseReleased(MouseEvent e)
{
o_x=0;
o_y=0;
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
class CustomListener implements MouseMotionListener
{
TMouseMotion tm;
public CustomListener(TMouseMotion tm)
{
this.tm=tm;
}
public void mouseMoved(MouseEvent e)
{
tm.flag=2;
tm.x=e.getX();
tm.y=e.getY();
tm.repaint();
}
public void mouseDragged(MouseEvent e)
{
tm.flag=1;
tm.x=e.getX();
tm.y=e.getY();
if(tm.x> =tm.o_x & & tm.y =tm.o_x & & tm.y> =tm.o_y)
{
tm.s_x=tm.o_x;
tm.s_y=tm.o_y;
tm.width=tm.x-tm.o_x;
tm.height=tm.y-tm.o_y;
}
if(tm.x =tm.o_y)
{
tm.s_x=tm.x;
tm.s_y=tm.o_y;
tm.width=tm.o_x-tm.x;
tm.height=tm.y-tm.o_y;
}
if(tm.x
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
public class TMouseMotion extends JApplet implements MouseListener
{
int x,y;
int o_x,o_y;
int flag;
int s_x;
int s_y;
int width;
int height;
public void init()
{
CustomListener ct=new CustomListener(this);
this.addMouseMotionListener(ct);
addMouseListener(this);//You missed this line
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics gdraw)
{
BufferedImage bufimg=new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_ARGB);
Graphics g=bufimg.getGraphics();
g.setColor(this.getBackground());
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.blue);
g.drawString("drag/move mouse......",5,20);
g.setColor(Color.red);
if(flag==1)
{
g.drawString("don't move!drag the mouse",5,85);
g.drawString("cursor coordinates:"+x+","+y,5,95);
}
else if(flag==2)
{
g.drawString("don't drag!move the mouse",5,85);
g.drawString("cursor coordinates:"+x+","+y,5,95);
}
g.setColor(Color.blue);
g.drawRect(s_x,s_y,width,height);
gdraw.drawImage(bufimg,0,0,null);
}
public void mousePressed(MouseEvent e)
{
o_x=e.getX();
o_y=e.getY();
}
public void mouseReleased(MouseEvent e)
{
o_x=0;
o_y=0;
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
class CustomListener implements MouseMotionListener
{
TMouseMotion tm;
public CustomListener(TMouseMotion tm)
{
this.tm=tm;
}
public void mouseMoved(MouseEvent e)
{
tm.flag=2;
tm.x=e.getX();
tm.y=e.getY();
tm.repaint();
}
public void mouseDragged(MouseEvent e)
{
tm.flag=1;
tm.x=e.getX();
tm.y=e.getY();
if(tm.x> =tm.o_x & & tm.y =tm.o_x & & tm.y> =tm.o_y)
{
tm.s_x=tm.o_x;
tm.s_y=tm.o_y;
tm.width=tm.x-tm.o_x;
tm.height=tm.y-tm.o_y;
}
if(tm.x =tm.o_y)
{
tm.s_x=tm.x;
tm.s_y=tm.o_y;
tm.width=tm.o_x-tm.x;
tm.height=tm.y-tm.o_y;
}
if(tm.x
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!