java - 如何使用鼠标拖动事件在java小程序上绘制矩形

标签 java applet drawing awt

我正在使用java。 我想根据 mousedrag 事件绘制矩形。如果用户拖动鼠标,则小程序上的矩形应根据当前鼠标坐标增大或减小。 我有以下代码。

在下面的代码中,我使用 [b]SelectionArea[/b] 类,它扩展了我正在其上执行绘图操作的 Canvas 。我在此类中使用 [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);
}

}

此外,如果我在全屏模式下运行此代码,那么我会看到只有在释放鼠标后矩形才会出现在屏幕上。但我希望在拖动鼠标时矩形出现在屏幕上,并且它应该根据当前鼠标坐标更改其尺寸。 任何人都可以帮助我吗?

最佳答案

作业?

基本上你需要做的是:

  1. 鼠标按下时保持鼠标按下坐标并重新绘制
  2. 鼠标移动时保持当前鼠标坐标并重新绘制
  3. 鼠标松开时,使鼠标按下的坐标无效以指示没有矩形,然后重新绘制。
  4. 在绘画上,绘制背景,然后在鼠标按下和当前鼠标坐标之间进行调整。

如果你不想保留背景图像,你可以使用图形异或函数来实现一个技巧,绘制相同的矩形两次会删除旧的矩形,因此你可以使用它直接在屏幕上恢复旧图像图形对象。

编辑:代码异或用法示例:

public void paint(Graphics g)
{
   g.setXORMode(Color.black);
   // draw old rect if there is one. this will erase it
   // draw new rect, this will draw xored
   g.setDrawMode(); // restore normal draw mode
}

异或有一个有趣的属性:

xor(xor(x)) = x

因此,对同一像素进行两次异或可恢复其原始颜色。

关于java - 如何使用鼠标拖动事件在java小程序上绘制矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/880753/

相关文章:

java - Hadoop 进程的 jps 命令

java - Eclipse 自动更正功能?

Java Applet 类文件

iphone - 使用 UIView 或 CALayer 绘图和制作动画?

使用 WHERE 和变量 : invalid character constant 的 JAVA 更新语句

java - 检查一个值是否为 0 比直接覆盖它更快吗?

java - HTML 没有将参数传递给 java 类

App Engine 中的 Java 小程序

iphone - iOS 开发 - 共享屏幕截图

c# - C# 2.0 可以创建发光效果吗?