是一下这个合不合适: 在画板是点击鼠标,不放拖动后再释放就会画出一个矩形。(不过拖动效果感觉不大好) 如果不显示拖动的效果只是在释放鼠标时画图可以把public void mouseDragged(MouseEvent e)函数设计为空函数;import java.awt.*; import java.awt.event.*; import javax.swing.*;public class MoveRectPanel extends javax.swing.JPanel implements MouseMotionListener,MouseListener{ private Point stp = new Point(0, 0);; private Graphics g; public MoveRectPanel() { addMouseMotionListener(this); addMouseListener(this); } public static void main(String[] args){ JFrame jf = new JFrame(); MoveRectPanel sai = new MoveRectPanel(); jf.add(sai); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setSize(400, 500); jf.setVisible(true);
} public void mouseDragged(MouseEvent e) {//鼠标拖动事件 repaint();//刷新画板 g = getGraphics();//得到一个Graphics的实例进行画图 int x=Math.min(stp.x, e.getX());//判断画矩形的坐标点 int y=Math.min(stp.y, e.getY()); g.drawRect(x, y, Math.abs(e.getX()-stp.x), Math.abs(e.getY()-stp.y)); } public void mouseMoved(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) {//鼠标按下事件 stp.move(e.getX(), e.getY());//记录第一次点击的点 repaint();//刷新画板 } public void mouseReleased(MouseEvent e) {//鼠标释放事件 g = getGraphics();//得到一个Graphics的实例进行画图 int x=Math.min(stp.x, e.getX());//判断画矩形的坐标点 int y=Math.min(stp.y, e.getY()); g.drawRect(x, y, Math.abs(e.getX()-stp.x), Math.abs(e.getY()-stp.y)); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } }
在画板是点击鼠标,不放拖动后再释放就会画出一个矩形。(不过拖动效果感觉不大好)
如果不显示拖动的效果只是在释放鼠标时画图可以把public void mouseDragged(MouseEvent e)函数设计为空函数;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class MoveRectPanel extends javax.swing.JPanel
implements MouseMotionListener,MouseListener{
private Point stp = new Point(0, 0);;
private Graphics g; public MoveRectPanel() {
addMouseMotionListener(this);
addMouseListener(this);
} public static void main(String[] args){
JFrame jf = new JFrame();
MoveRectPanel sai = new MoveRectPanel();
jf.add(sai);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(400, 500);
jf.setVisible(true);
} public void mouseDragged(MouseEvent e) {//鼠标拖动事件
repaint();//刷新画板
g = getGraphics();//得到一个Graphics的实例进行画图
int x=Math.min(stp.x, e.getX());//判断画矩形的坐标点
int y=Math.min(stp.y, e.getY());
g.drawRect(x, y, Math.abs(e.getX()-stp.x), Math.abs(e.getY()-stp.y));
} public void mouseMoved(MouseEvent e) {
} public void mouseClicked(MouseEvent e) {
} public void mousePressed(MouseEvent e) {//鼠标按下事件
stp.move(e.getX(), e.getY());//记录第一次点击的点
repaint();//刷新画板
} public void mouseReleased(MouseEvent e) {//鼠标释放事件
g = getGraphics();//得到一个Graphics的实例进行画图
int x=Math.min(stp.x, e.getX());//判断画矩形的坐标点
int y=Math.min(stp.y, e.getY());
g.drawRect(x, y, Math.abs(e.getX()-stp.x), Math.abs(e.getY()-stp.y));
} public void mouseEntered(MouseEvent e) {
} public void mouseExited(MouseEvent e) {
}
}
你的方法我试试,谢谢了啊
public class Rectangle extends Shape{
public Rectangle() {
}
public void draw(Graphics g){
g.setColor(getColor());
if (this.getFill()==false) g.drawRect(getbeginX(),getbeginY(),getwidth(),getheight());
else g.fillRect(getbeginX(),getbeginY(),getwidth(),getheight());
}
/* public void draw(Graphics g,boolean f){
g.setColor(getColor());
if (f==false) g.drawRect(beginX,beginY,width,height);
else g.fillRect(beginX,beginY,width,height);
}*/
}其中,父类shape是抽象出的一个图形类,包含一些基本内容重写
public void paintComponent(Graphics g){
super.paintComponent(g);
//画出已有的图像,代码略
//下面的r为:private Rectangle r=new Rectangle();
//画矩形
if (type=="Rectangle"&&flag==true) {
r.setShapeType("Rectangle");
if (dragged==true&&finish==false) {
g.setColor(pacolor);
if (fill==false) g.drawRect(beginX,beginY,endWidth,endHeight);
else g.fillRect(beginX,beginY,endWidth,endHeight);
}
if (dragged==false&&finish==true) {
r.setColor(pacolor);
r.setbeginX(beginX);
r.setbeginY(beginY);
r.setWidth(endWidth);
r.setHeight(endHeight);
r.setFill(fill);
r.draw(g);
save(r);flag=false;
}
//相关鼠标事件
public void mousePressed(MouseEvent e) {
beginX=e.getX();
beginY=e.getY();
finish=false;
flag=true;
repaint();
} public void mouseReleased(MouseEvent e) {
endWidth=e.getX()-beginX;
endHeight=e.getY()-beginY;
dragged=false;
finish=true;
repaint();
}
public void mouseDragged(MouseEvent e) {
endWidth=e.getX()-beginX;
endHeight=e.getY()-beginY;
dragged=true;
repaint();
}
有点乱,兄弟将就吧
我想知道如果这个JPanel已经定义好了,你只能拿到这个对象
在专门的鼠标事件中是否可以在这个jpanel对象上面画图呢?