java鼠标拖动圆响应事件_如何使用鼠标拖动事件在Java Applet上绘制矩...

我正在使用Java.

我想根据mousedrag事件绘制矩形.如果用户拖动鼠标,则小程序上的矩形应根据当前鼠标坐标增加或减少.

我有以下代码.

在下面的代码中,我使用[b] SelectionArea [/ b]类扩展了在其上执行绘制操作的画布.我在此类中使用[b] image [/ b]变量进行双缓冲,以减少闪烁并保存小程序的先前状态(即,绘制小程序的内容)

但如果我画第一个矩形,代码工作正常.如果我开始绘制第二个矩形,则先前绘制的矩形将消失.我希望先前绘制的矩形出现在屏幕上

谁能告诉我如何解决这个问题.

import java.awt.*;

import java.applet.Applet;

import java.awt.event.*;

/*

* This displays a framed area. When the user drags within

* the area, this program displays a rectangle extending from

* where the user first pressed the mouse button to the current

* cursor location.

*/

public class RectangleDemo extends Applet {

SelectionArea drawingPanel;

Label label;

public void init() {

GridBagLayout gridBag = new GridBagLayout();

GridBagConstraints c = new GridBagConstraints();

setLayout(gridBag);

drawingPanel = new SelectionArea(this);

c.fill = GridBagConstraints.BOTH;

c.weighty = 1.0;

c.gridwidth = GridBagConstraints.REMAINDER; //end row

gridBag.setConstraints(drawingPanel, c);

add(drawingPanel);

label = new Label("Drag within the framed area.");

c.fill = GridBagConstraints.HORIZONTAL;

c.weightx = 1.0;

c.weighty = 0.0;

gridBag.setConstraints(label, c);

add(label);

drawingPanel.setVisible(true);

validate();

}

public void paint(Graphics g){

drawingPanel.repaint();

}

public void update(Graphics g){

paint(g);

}

}

class SelectionArea extends Canvas implements ActionListener, MouseListener, MouseMotionListener{

Rectangle currentRect;

RectangleDemo controller;

//for double buffering

Image image;

Graphics offscreen;

public SelectionArea(RectangleDemo controller) {

super();

this.controller = controller;

addMouseListener(this);

addMouseMotionListener(this);

}

public void actionPerformed(ActionEvent ae){

repaintoffscreen();

}

public void repaintoffscreen(){

image = createImage(this.getWidth(), this.getHeight());

offscreen = image.getGraphics();

Dimension d = getSize();

if(currentRect != null){

Rectangle box = getDrawableRect(currentRect, d);

//Draw the box outline.

offscreen.drawRect(box.x, box.y, box.width - 1, box.height - 1);

//repaint();

}

}

public void mouseEntered(MouseEvent me) {}

public void mouseExited(MouseEvent me){ }

public void mouseClicked(MouseEvent me){}

public void mouseMoved(MouseEvent me){}

public void mousePressed(MouseEvent me) {

currentRect = new Rectangle(me.getX(), me.getY(), 0, 0);

repaintoffscreen();

}

public void mouseDragged(MouseEvent me) {

System.out.println("here in dragged()");

currentRect.setSize(me.getX() - currentRect.x, me.getY() - currentRect.y);

repaintoffscreen();

repaint();

}

public void mouseReleased(MouseEvent me) {

currentRect.setSize(me.getX() - currentRect.x, me.getY() - currentRect.y);

repaintoffscreen();

repaint();

}

public void update(Graphics g){

paint(g);

}

public void paint(Graphics g) {

g.drawImage(image, 0, 0, this);

}

Rectangle getDrawableRect(Rectangle originalRect, Dimension drawingArea) {

int x = originalRect.x;

int y = originalRect.y;

int width = originalRect.width;

int height = originalRect.height;

//Make sure rectangle width and height are positive.

if (width < 0) {

width = 0 - width;

x = x - width + 1;

if (x < 0) {

width += x;

x = 0;

}

}

if (height < 0) {

height = 0 - height;

y = y - height + 1;

if (y < 0) {

height += y;

y = 0;

}

}

//The rectangle shouldn't extend past the drawing area.

if ((x + width) > drawingArea.width) {

width = drawingArea.width - x;

}

if ((y + height) > drawingArea.height) {

height = drawingArea.height - y;

}

return new Rectangle(x, y, width, height);

}

}

另外,如果我在全屏模式下运行此代码,那么我会看到只有在释放鼠标后矩形才会出现在屏幕上.但是我希望在拖动鼠标时在屏幕上显示矩形,并且应该根据当前鼠标坐标更改矩形的尺寸.

谁能帮助我.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值