开发者

Drawing rectangle using mouse [duplicate]

This question already has answers here: 开发者_如何转开发 Closed 11 years ago.

Possible Duplicate:

How to draw a rectangle on a java applet using mouse drag event and make it stay

Hello. I would like to know how I can draw rectangle using mousedragged event. I know that I must record mouse location using class Point. I need to implement paint function in paint method and in mousedragged call it or my paint code should implement in mousedragged event?

At this time I put my code in mouse dragged event. This is code:

@Override
    public void mousePressed(MouseEvent e)
    {
        super.mousePressed(e);
        System.out.println("f.getGlassPane() mousePressed");
        if(e.getButton() == MouseEvent.BUTTON1)


        frame.getGlassPane().setVisible(true);

        startPoint=e.getPoint();

        Graphics2D g = null;
            Graphics2D g2 = (Graphics2D) g;
        Rectangle2D prostokat = new Rectangle2D.Double();
        prostokat.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,startPoint.x, startPoint.y);
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5F));
        g2.setColor(Color.BLUE);
        g2.fill(prostokat);
        g2.draw(prostokat);

    }

    });


You'll also need to handle mouseReleased and mouseDragged, as shown here and here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜