hi,
i want to draw rectangle based on mousedrag event. if user dragging the mouse, then the rectangle on the applet should increase or decrease basing on current mouse coordinates.
i have the following code.

in the following code i am using SelectionArea class which extends a canvas on which i am performing drawing operation. i am using image variable in this class for double buffering to reduce flickering and to save the applet's previous state(i.e drawing content of applet)

but the code is working fine if i draw first rectangle. if i start to draw second rectangle the previously drawn rectangle is disappearing. i want the previously drawn rectangle to be on the screen

can any one tell me how to solve this.

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

also if i run this code on full screen mode then i am seeing that the rectangle is appering on screen only after i released the mouse. but i want the rectangle to be on the screen while dragging the mouse and it should change it's dimension according to the current mouse coordinates.
can any one help me pls.

How about storing all of the rectangles, not just the current rectangle, and painting them all on the image (not sure you really want to use an image rather than drawing the rectangles directly onto the applet. Does it really reduce flickering?), rather than just painting the current one? You can add a new rectangle every time mousePressed is called. Add the previous one to a Vector of rectangles, then paint them all onto your image's Graphics object. Keep in mind, the way you have it now, you keep creating a NEW image every time you call repaintoffscreen, so anything you painted on the old images from previous rectangles isn't going to be there anymore. So if you want to keep the old rectangles, I think you need to either keep track of a bunch of rectangles and paint them all each time, or keep the same image and paint the new rectangle onto it, but right now I think you are constantly creating a blank image and painting a single rectangle onto it.

hi
i modified my code as follows.
but now the rectangle is visible on the applet only after i release my mouse. but now i am able to draw multiple rectangles. the only problem is i can not see the rectangle while dragging the mouse.

import java.applet.*;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class testingDemo extends Applet {    
    private final int RECT_OP = 1;
    public int opStatus = 5;
    private int mousex = 0;
    private int mousey = 0;    
    public boolean dragging = false;
    private int Orx = 0;
    private int Ory = 0;
    private int OrWidth = 0;
    private int OrHeight = 0;
    private int drawX = 0;
    private int drawY = 0;    
    drawingPanel drawPanel;
    public Image image;    
    
    public void init() {
        setLayout(new BorderLayout());
        drawPanel = new drawingPanel();
        drawPanel.setVisible(true);
        this.setBackground(Color.white);
        add(drawPanel, BorderLayout.CENTER);
    }

    class drawingPanel extends Panel implements MouseListener, MouseMotionListener {

        drawingPanel() {
            addMouseListener(this);
            addMouseMotionListener(this);
        }

        public void rectOperation(MouseEvent e){
            mousex = e.getX();
            mousey = e.getY();
            setActualBoundry();
            if(image == null){
                image = this.createImage(this.getWidth(), this.getHeight());
            }
            Graphics offscreen = image.getGraphics();
            offscreen.setXORMode(Color.white);
            offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
            offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
            repaint();
        }

        public boolean mouseHasMoved(MouseEvent e) {
            return (mousex != e.getX() || mousey != e.getY());
        }

        public void setActualBoundry() {
            if (mousex < Orx || mousey < Ory) {
                if (mousex < Orx) {
                    OrWidth = Orx - mousex;
                    drawX = Orx - OrWidth;
                } else {
                    drawX = Orx;
                    OrWidth = mousex - Orx;
                }
                if (mousey < Ory) {
                    OrHeight = Ory - mousey;
                    drawY = Ory - OrHeight;
                } else {
                    drawY = Ory;
                    OrHeight = mousey - Ory;
                }
            } else {
                drawX = Orx;
                drawY = Ory;
                OrWidth = mousex - Orx;
                OrHeight = mousey - Ory;
            }
        }

        public void setGraphicalDefaults(MouseEvent e) {
            mousex = e.getX();
            mousey = e.getY();
            Orx = e.getX();
            Ory = e.getY();
            drawX = e.getX();
            drawY = e.getY();
            OrWidth =  0;
            OrHeight = 0;
        }

        public void mouseDragged(MouseEvent e) {            
            dragging = true;
            System.out.println("In mousedragged");
            opStatus = RECT_OP;
            rectOperation(e);            
        }

        public void mouseReleased(MouseEvent e) {            
            System.out.println("in mouseReleased()");
            dragging = false;
            opStatus = RECT_OP;
            mousex = e.getX();
            mousey = e.getY();
            setActualBoundry();
            releasedRect();            
        }

        public void mouseEntered(MouseEvent e) { }

        public void releasedRect() {
            System.out.println("in releasedRect()");            
            Graphics offscreen = image.getGraphics();
            offscreen.setXORMode(Color.white);
            offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
            offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
            offscreen.setPaintMode();
            offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
            offscreen.dispose();
            repaint();            
        }

        public void mouseClicked(MouseEvent e) {}

        public void mouseExited(MouseEvent e) {}

        public void mouseMoved(MouseEvent e) {}

        public void mousePressed(MouseEvent e) {
            setGraphicalDefaults(e);
            rectOperation(e);
        }

        public void paint(Graphics g) {
            g.drawImage(image, 0, 0, this);           
        }

        public void update(Graphics g) {
            paint(g);
        }
    }

    public void paint(Graphics g) {
        drawPanel.repaint();
    }

    public void update(Graphics g) {
        paint(g);
    }
}

can any one tell me what should i chagne in the above code so that the rectangle will be visible while dragging the mouse.
thanks for any help

public void rectOperation(MouseEvent e){
            mousex = e.getX();
            mousey = e.getY();
            setActualBoundry();
            if(image == null){
                image = this.createImage(this.getWidth(), this.getHeight());
            }
            Graphics offscreen = image.getGraphics();
            offscreen.setXORMode(Color.white);
            offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
            offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
            repaint();
        }

I've never used XORMode, but it appears that it's visible if you draw the rectangle an odd number of times, and invisible if you draw it an even number of times. You draw the rectangle twice here. Why are you drawing it twice anyway?

Also, your dragging variable is never used. You assign it values, but never do anything with it.

import java.applet.*;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class testingdemo extends Applet {    
    private final int RECT_OP = 1;
    public int opStatus = 5;
    private int mousex = 0;
    private int mousey = 0;    
    public boolean dragging = false;
    private int Orx = 0;
    private int Ory = 0;
    private int OrWidth = 0;
    private int OrHeight = 0;
    private int drawX = 0;
    private int drawY = 0;    
    drawingPanel drawPanel;
    public Image image;    

    public void init() {
        setLayout(new BorderLayout());
        drawPanel = new drawingPanel();
        drawPanel.setVisible(true);
        this.setBackground(Color.white);
        add(drawPanel, BorderLayout.CENTER);
    }

    class drawingPanel extends Panel implements MouseListener, MouseMotionListener {

        drawingPanel() {
            addMouseListener(this);
            addMouseMotionListener(this);
        }

        public void rectOperation(MouseEvent e){
            mousex = e.getX();
            mousey = e.getY();
            setActualBoundry();
           image = null;
            //if(image == null){
           //     image = this.createImage(this.getWidth(), this.getHeight());
           // }
            Graphics offscreen = image.getGraphics();
            offscreen.setXORMode(Color.white);
            setActualBoundry();
             offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
           // offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
            repaint();
        }

        public boolean mouseHasMoved(MouseEvent e) {
            return (mousex != e.getX() || mousey != e.getY());
        }

        public void setActualBoundry() {
            if (mousex < Orx || mousey < Ory) {
                if (mousex < Orx) {
                    OrWidth = Orx - mousex;
                    drawX = Orx - OrWidth;
                } else {
                    drawX = Orx;
                    OrWidth = mousex - Orx;
                }
                if (mousey < Ory) {
                    OrHeight = Ory - mousey;
                    drawY = Ory - OrHeight;
                } else {
                    drawY = Ory;
                    OrHeight = mousey - Ory;
                }
            } else {
                drawX = Orx;
                drawY = Ory;
                OrWidth = mousex - Orx;
                OrHeight = mousey - Ory;
            }
        }

        public void setGraphicalDefaults(MouseEvent e) {
            mousex = e.getX();
            mousey = e.getY();
            Orx = e.getX();
            Ory = e.getY();
            drawX = e.getX();
            drawY = e.getY();
            OrWidth =  0;
            OrHeight = 0;
        }

        public void mouseDragged(MouseEvent e) {            
            dragging = true;
            System.out.println("In mousedragged");
            opStatus = RECT_OP;
            rectOperation(e);            
        }

        public void mouseReleased(MouseEvent e) {            
            System.out.println("in mouseReleased()");
            dragging = false;
            opStatus = RECT_OP;
            mousex = e.getX();
            mousey = e.getY();
            setActualBoundry();
            releasedRect();            
        }

        public void mouseEntered(MouseEvent e) { }

        public void releasedRect() {
            System.out.println("in releasedRect()");            
           Graphics offscreen = image.getGraphics();
            offscreen.setXORMode(Color.white);
            offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
            offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
            offscreen.setPaintMode();
            offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
            offscreen.dispose();
            repaint();            
        }

        public void mouseClicked(MouseEvent e) {}

        public void mouseExited(MouseEvent e) {}

        public void mouseMoved(MouseEvent e) {}

        public void mousePressed(MouseEvent e) {
            setGraphicalDefaults(e);
            rectOperation(e);
        }

        public void paint(Graphics g) {
             g.drawImage(image, 0, 0, this);           
        }

        public void update(Graphics g) {
            paint(g);
        }
    }

    public void paint(Graphics g) {
        drawPanel.repaint();
    }

    public void update(Graphics g) {
        paint(g);
    }
}

check this out, I have done some changes in the

public void rectOperation(MouseEvent e){

function, hope this helps!

sorry these code tagging never seems to work for me ..any help in that area?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.