有没有人知道怎么样在JFrame里面实现用鼠标画一个矩形啊??
----------------解决方案--------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawRectangle extends JFrame implements MouseListener{
private int mouseX,mouseY,mouseWidth,mouseHeight;
public DrawRectangle(){
super("鼠标画矩形");
this.setSize(640,480);
this.setVisible(true);
this.addMouseListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new DrawRectangle();
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.blue);
//g.clearRect(0,0,this.getSize().width,this.getSize().height);
g.drawRect(mouseX,mouseY,mouseWidth,mouseHeight);
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){
mouseX = e.getX();
mouseY = e.getY();
}
public void mouseReleased(MouseEvent e){
mouseWidth = e.getX()-mouseX;
mouseHeight = e.getY()-mouseY;
repaint();
}
}
----------------解决方案--------------------------------------------------------
可是这样很难确定我要的矩形大小耶,只能乱画,没有个渐变的过程
----------------解决方案--------------------------------------------------------
http://bbs.bc-cn.net/viewthread.php?tid=89152&extra=&page=100#
我以前写了一个截屏程序,里面可以画矩形来截
你可以参考一下
----------------解决方案--------------------------------------------------------
能不能给我个简单的程序啊
----------------解决方案--------------------------------------------------------
简单的就是实现keyDragged()方法
这样就可以边拖动边看到效果
你得加MouseMotionListener监听器
----------------解决方案--------------------------------------------------------
简单的就是实现keyDragged()方法
这样就可以边拖动边看到效果
你得加MouseMotionListener监听器
----------------解决方案--------------------------------------------------------