2

私はJavaを使用しています。mousedrag イベントに基づいて四角形を描画したい。ユーザーがマウスをドラッグすると、アプレットの四角形は現在のマウス座標に基づいて増減する必要があります。私は次のコードを持っています。

次のコードでは、描画操作を実行しているキャンバスを拡張する [b]SelectionArea[/b] クラスを使用しています。このクラスで [b]image[/b] 変数を使用してダブル バッファリングを行い、ちらつきを減らし、アプレットの以前の状態を保存します (つまり、アプレットのコンテンツを描画します)。

しかし、最初の長方形を描画すると、コードは正常に機能します。2 番目の四角形の描画を開始すると、以前に描画された四角形が消えます。前に描いた四角形を画面に表示したい

誰でもこれを解決する方法を教えてもらえますか。

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);
}

}

また、このコードを全画面表示モードで実行すると、マウスを離した後にのみ四角形が画面に表示されることがわかります。しかし、マウスをドラッグしている間、長方形を画面上に表示したいので、現在のマウス座標に従って寸法を変更する必要があります。誰でも私を助けることができますか?

4

3 に答える 3

3

宿題?

基本的にあなたがする必要があるのは:

  1. マウスダウン時に、マウスダウン座標を保持して再描画します
  2. マウスの移動時に現在のマウス座標を保持して再描画
  3. マウスアップ時に、マウスダウン座標を無効にして四角形がないことを示し、再描画します。
  4. ペイントでは、背景を描画してから、マウスダウン座標とカーソルカーソル座標の間を調整します。

背景画像を保持したくない場合は、Graphics xor 関数を使用してトリックを実行できます。同じ四角形を 2 回描画すると古い四角形が消去されるため、これを使用してグラフィックス オブジェクトで古い画像を直接復元できます。

編集: コード xor 使用例:

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(xor(x)) = x

したがって、同じピクセルを 2 回 xor すると、元の色が復元されます。

于 2009-05-19T02:47:50.517 に答える
0

対処する必要のある問題がいくつかあります。

まず、描画できる長方形は1つだけですが、これはプログラムの設計によるものです。repaintoffscreenコードでは、メソッドが呼び出されるたびに、currectRectフィールドを使用して長方形を描画します。ただし、過去に作成された長方形を保持し続けるための規定はありません。

過去の長方形を保持する1つの方法は、おそらく、List<Rectangle>過去の長方形を格納するために使用される別のフィールドを作成することです。次に、マウスをadd離すと、そのリストの現在の長方形が表示されます。

次に、すべての長方形currentRectと過去の長方形を表示するには、repaintoffscreenを実行getDrawableRectoffscreen.drawRectて使用するだけでなくcurrentRect、に格納されている過去の長方形も使用する必要がありList<Rectangle>ます。(ヒント、forループを使用してリストを反復処理します。)

mouseDragged次に、メソッドを使用するのではなく、マウスボタンを離すまで長方形が表示されない場合は、メソッドを使用mouseMovedし、マウスボタンが押されていることを確認することで回避できる場合があります。(私もmouseDragged過去にこの方法を扱うのに苦労したと思います。)

MouseEventメソッドに渡されたものを使用して、メソッドmouseMovedによってボタンが押されているかどうかを確認できます。getButton

public void mouseMoved(MouseEvent e)
{
    // Check if button1 is pressed.
    if (e.getButton() == MouseEvent.BUTTON1)
    {
        // Perform sizing of rectangle and off-screen drawing, and repaint.
    }
}
于 2009-05-19T03:32:31.423 に答える
0

私の質問は、選択した長方形の反転マウスクリック位置を作成することについてでしたが、最終的には、この方法でこれを作成しました。

...     //to set the selection area
    private int iniSelX;
    private int iniSelY;
    private int endSelX;
    private int endSelY;

    private JPanel myJPanel = new JPanel() {

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());

            g.setColor(Color.red);
            g.drawLine(260, 5, 260, 260);

            g.setColor(Color.BLUE);

            //verify if go draw the rectangle
            if (iniSelX != 0 || endSelX != 0) {
                boolean revertX = iniSelX < endSelX;
                boolean revertY = iniSelY < endSelY;

                //Simple way
                //g.drawRect(iniSelX, iniSelY, endSelX - iniSelX, endSelY - iniSelY);

                //reverse way
                g.drawRect(revertX ? iniSelX : endSelX, revertY ? iniSelY : endSelY,
                        revertX ? endSelX - iniSelX : iniSelX - endSelX, revertY ? endSelY - iniSelY : iniSelY - endSelY);
            }
        }
    }; ...
        addMouseMotionListener(new MouseMotionListener() {

            @Override
            public void mouseDragged(MouseEvent m) {
                //update selection area
                endSelX = m.getX();
                endSelY = m.getY();

                repaint();
            }

            @Override
            public void mouseMoved(MouseEvent m) {
                repaint();
            }
        });

        addMouseListener(new MouseListener() {

 ...
            @Override
            public void mousePressed(MouseEvent e) {
               //start  drawing the selection
                iniSelX = e.getX() - 15;
                iniSelY = e.getY() - 20;
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                //start  drawing the selection
                iniSelX = 0;
                iniSelY = 0;
                endSelX = 0;
                endSelY = 0;
            }

...
        });

    }

    public void log() {
        System.out.println("iniSelX" + iniSelX);
        System.out.println("iniSelY" + iniSelY);
        System.out.println("endSelX" + endSelX);
        System.out.println("endSelY" + endSelY);
    } ...

これがお役に立てば幸いです。

于 2013-01-18T05:23:18.960 に答える