import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Canvas extends Applet implements MouseMotionListener
{int x=-1,y=-1;
int width=160,height=30;
public void init()
{
//setBackground(Color.green);
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
if (x!=-1 &&y!=-1)
{
g.setColor(Color.red);
g.drawRect(x,y,width,height);
g.fillRect(x,y,width,height);
}
}
public void mouseDragged(MouseEvent e)
{
x=(int)e.getX();
y=(int)e.getY();
repaint();
}
public void mouseMoved(MouseEvent e)
{}
public void update(Graphics g)
{
paint(g);
}
} // <applet code=Canvas.class width=200 height=200></applet>这是我实现的一个程序,只能在鼠标点击处拖动鼠标来画矩形,我需要的效果是在以有的矩形基础上向左拖动,已有的矩形消失,向右拖动,矩形接着已有的延伸.不知怎么做,还有怎么让矩形沿直线画,谢谢!!
import java.awt.*;
import java.awt.event.*;
public class Canvas extends Applet implements MouseMotionListener
{int x=-1,y=-1;
int width=160,height=30;
public void init()
{
//setBackground(Color.green);
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
if (x!=-1 &&y!=-1)
{
g.setColor(Color.red);
g.drawRect(x,y,width,height);
g.fillRect(x,y,width,height);
}
}
public void mouseDragged(MouseEvent e)
{
x=(int)e.getX();
y=(int)e.getY();
repaint();
}
public void mouseMoved(MouseEvent e)
{}
public void update(Graphics g)
{
paint(g);
}
} // <applet code=Canvas.class width=200 height=200></applet>这是我实现的一个程序,只能在鼠标点击处拖动鼠标来画矩形,我需要的效果是在以有的矩形基础上向左拖动,已有的矩形消失,向右拖动,矩形接着已有的延伸.不知怎么做,还有怎么让矩形沿直线画,谢谢!!
解决方案 »
- Thread.currentThread()的疑问
- 在servlert无法用 String str = config.getInitParameter("email");返回为null?
- 小弟最近做了一个银行的简单查询,存钱,取钱的系统,有点问题,请求帮助
- 再次求救!!!AES加密算法问题
- 基础问题:字符串的截取
- 帮我分析一下这几行带码吧
- 怎样设置JTree的某个节点为选中状态?
- 请问怎么改写装载applet的html使客户端没有jre时能自动到指定的url下载?
- 谁有Borland Appserver 4.5的注册码,急用???
- JAVA同步数据到ldap
- Java Puzzlers里面的一个谜题,大家都给个解释,进者有分
- 一个用Java读写Excel文件的例子
存WEB实现矢量绘图