9 Replies - 729 Views - Last Post: 08 May 2009 - 06:52 PM Rate Topic: -----

#1 gigantic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 13-April 09

how the repaintRect() is working in this snippet

Posted 06 May 2009 - 06:22 AM

hi i have the following snippet that draws certain shapes(oval,rectangle) on applet. i got this snippet from somewhere and incorporated in my code.

its working fine but i didn't understand what the repaintRect() and also when i draw oval, then the oval shapes thickness is more than the actual cursor thickness.

i mean if i draw rectangle then the pen size is fine. but if i draw oval, then the pen size is more.

can any one explain me how the repaintRect() is working and what is it's logic
here is my code.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public class demoboard extends Applet implements MouseMotionListener, MouseListener, ActionListener  {
	// Some constants to make the code more readable.
	// These numbers code for// the different drawing colors.
	private final static int BLACK = 0,  RED = 1, GREEN = 2,  BLUE = 3, CYAN = 4, 
		 MAGENTA = 5,  YELLOW = 6,  WHITE = 7;
	// Some constants that code// for the different types of// figure the program can draw.
	private final static int CURVE = 0,  LINE = 1,  RECT = 2, 
		 OVAL = 3, ROUNDRECT = 4, FILLED_RECT = 5,  FILLED_OVAL = 6,  FILLED_ROUNDRECT = 7;
	/* Some variables used for backing up the contents of the panel. */
	 // The off-screen image (created in checkOSI()).
	Image OSI;
	// Current width and height of OSI. These
	int widthOfOSI, heightOfOSI; 
	// are checked against the size of the applet,
	// to detect any change in the panel's size.
	// If the size has changed, a new OSI is created.
	// The picture in the off-screen image is lost
	// when that happens.
		/* The following variables are used when the user is sketching a
		curve while dragging a mouse.*/
	// The location of the mouse.
	private int mouseX,  mouseY;
	// The previous location of the mouse.
	private int prevX,  prevY; 
	// The starting position of the mouse.
	private int startX,  startY; 
	// (Not used for drawing curves.)
	// This is set to true when the user is drawing.
	private boolean dragging; 
	// What type of figure is being drawn. This is
	// specified by the figureChoice menu.
	private int figure; 
	 // A graphics context for the off-screen image,
	// to be used while a drag is in progress.
	private Graphics dragGraphics;
	// The color that is used for the figure that is
	// being drawn.
	private Color dragColor;
	int figureChoice;
	Button btnEllipse;
	Button btnFreeHand;
	Button btnRectangle;
	//Button btnEraser;
	Panel btnPanel;
	public void init(){
		setLayout(new BorderLayout());
		setSize(new Dimension(500,500));

		dragging = false;

		btnPanel = new Panel();
		btnPanel.setLayout(new FlowLayout(FlowLayout.LEFT ));
		btnEllipse = new Button("Oval");
		btnFreeHand = new Button("FreeHand");
		btnRectangle = new Button("Rectangle");
		//btnEraser = new Button("Eraser");
		btnEllipse.addActionListener(this);
		btnFreeHand.addActionListener(this);
		btnRectangle.addActionListener(this);
	   // btnEraser.addActionListener(this);
		btnPanel.add(btnEllipse);
		btnPanel.add(btnFreeHand);
		btnPanel.add(btnRectangle);
	   // btnPanel.add(btnEraser);
		add(btnPanel,BorderLayout.NORTH);
		checkOSI();

		addMouseListener(this);
		addMouseMotionListener( this );
	}

	public void actionPerformed(ActionEvent e){
	if (e.getActionCommand().compareTo("FreeHand")==0)
		figureChoice = 0;
	if (e.getActionCommand().compareTo("Line")==0)
		figureChoice = 1;
	if (e.getActionCommand().compareTo("Eraser")==0)
		figureChoice = 0;
	if (e.getActionCommand().compareTo("Clear")==0)
		figureChoice = 0;
	if (e.getActionCommand().compareTo("Rectangle")==0)
		figureChoice = 2;
	if (e.getActionCommand().compareTo("Oval")==0)
		figureChoice = 3;
	if (e.getActionCommand().compareTo("Circle")==0)
		figureChoice = 0;
	if (e.getActionCommand().compareTo("Polygon")==0)
		figureChoice = 0;
	if (e.getActionCommand().compareTo("Black")==0)
		figureChoice = 0;
	if (e.getActionCommand().compareTo("Blue")==0)
		figureChoice = 0;
	}

	public void mousePressed(MouseEvent evt) {
		// This is called when the user presses the mouse on the
		// panel. This begins a draw operation in which the user
		// sketches a curve or draws a shape. Curves
		// are handled differently from other shapes. For CURVE,
		// a new segment of the curve is drawn each time the user
		// moves the mouse. For the other shapes, a "rubber band
		// cursor" is used. That is, the figure is drawn between
		// the starting point and the current mouse location.)

		if (dragging == true) // Ignore mouse presses that occur
		{
			return; // when user is already drawing a curve.
		}// (This can happen if the user presses
		// two mouse buttons at the same time.)

		prevX = startX = evt.getX(); // Save mouse coordinates.
		prevY = startY = evt.getY();

		figure = figureChoice;
		System.out.println("figure : "+figure);
		dragColor = getCurrentColor();
		dragGraphics = OSI.getGraphics();
		dragGraphics.setColor(dragColor);

		dragging = true; // Start drawing.

	} // end mousePressed()

	public void mouseReleased(MouseEvent evt) {
		// Called whenever the user releases the mouse button.
		// If the user was drawing a shape, we make the shape
		// permanent by drawing it to the off-screen image.
		if (dragging == false) {
			return; // Nothing to do because the user isn't drawing.
		}
		dragging = false;
		mouseX = evt.getX();
		mouseY = evt.getY();
		if (figure == CURVE) {
			// A CURVE is drawn as a series of LINEs
			drawFigure(dragGraphics, LINE, prevX, prevY, mouseX, mouseY);
			repaintRect(prevX, prevY, mouseX, mouseY);
		} else if (figure == LINE) {
			repaintRect(startX, startY, prevX, prevY);
			if (mouseX != startX || mouseY != startY) {
				// Draw the line only if it has non-zero length.
				drawFigure(dragGraphics, figure, startX, startY, mouseX, mouseY);
				repaintRect(startX, startY, mouseX, mouseY);
			}
		} else {
			repaintRect(startX, startY, prevX, prevY);
			if (mouseX != startX && mouseY != startY) {
				// Draw the shape only if both its height
				// and width are both non-zero.
				drawFigure(dragGraphics, figure, startX, startY, mouseX, mouseY);
				repaintRect(startX, startY, mouseX, mouseY);
			}
		}
		dragGraphics.dispose();
		dragGraphics = null;
	}

	public void mouseDragged(MouseEvent evt) {
		// Called whenever the user moves the mouse while a mouse button
		// is down. If the user is drawing a curve, draw a segment of
		// the curve on the off-screen image, and repaint the part
		// of the panel that contains the new line segment. Otherwise,
		// just call repaint and let paintComponent() draw the shape on
		// top of the picture in the off-screen image.

		if (dragging == false) {
			return; // Nothing to do because the user isn't drawing.
		}
		mouseX = evt.getX(); // x-coordinate of mouse.
		mouseY = evt.getY(); // y=coordinate of mouse.

		if (figure == CURVE) {
			// A CURVE is drawn as a series of LINEs.
			drawFigure(dragGraphics, LINE, prevX, prevY, mouseX, mouseY);
			repaintRect(prevX, prevY, mouseX, mouseY);
		} else {
			// Repaint two rectangles: The one that contains the previous
			// version of the figure, and the one that will contain the
			// new version. The first repaint is necessary to restore
			// the picture from the off-screen image in that rectangle.
			System.out.println("drawing shape : ");
			repaintRect(startX, startY, prevX, prevY);
			repaintRect(startX, startY, mouseX, mouseY);
		}

		prevX = mouseX; // Save coords for the next call to mouseDragged or mouseReleased.
		prevY = mouseY;
		//repaint();

	} // end mouseDragged.
	public void mouseEntered(MouseEvent evt) {
	} // Some empty routines.
	public void mouseExited(MouseEvent evt) {
	} // (Required by the MouseListener
	public void mouseClicked(MouseEvent evt) {
	} // and MouseMotionListener
	public void mouseMoved(MouseEvent evt) {
	} // interfaces).

	private void drawFigure(Graphics g, int shape, int x1, int y1, int x2, int y2) {
	// This method is called to do ALL drawing in this applet!
	// Draws a shape in the graphics context g.
	// The shape paramter tells what kind of shape to draw. This
	// can be LINE, RECT, OVAL, ROUNTRECT, FILLED_RECT,
	// FILLED_OVAL, or FILLED_ROUNDRECT. (Note that a CURVE is
	// drawn by drawing multiple LINES, so the shape parameter is
	// never equal to CURVE.) For a LINE, a line is drawn from
	// the point (x1,y1) to (x2,y2). For other shapes, the
	// points (x1,y1) and (x2,y2) give two corners of the shape
	// (or of a rectangle that contains the shape).
		if (shape == LINE) {
	// For a line, just draw the line between the two points.
			g.drawLine(x1, y1, x2, y2);
			return;
		}
		int x, y; // Top left corner of rectangle that contains the figure.
		int w, h; // Width and height of rectangle that contains the figure.
		if (x1 >= x2) { // x2 is left edge
			x = x2;
			w = x1 - x2;
		} else { // x1 is left edge
			x = x1;
			w = x2 - x1;
		}
		if (y1 >= y2) { // y2 is top edge
			y = y2;
			h = y1 - y2;
		} else { // y1 is top edge.
			y = y1;
			h = y2 - y1;
		}
		switch (shape) { // Draw the appropriate figure.
			case RECT:
				g.drawRect(x, y, w, h);
				break;
			case OVAL:
				g.drawOval(x, y, w, h);
				break;
			case ROUNDRECT:
				g.drawRoundRect(x, y, w, h, 20, 20);
				break;
			case FILLED_RECT:
				g.fillRect(x, y, w, h);
				break;
			case FILLED_OVAL:
				g.fillOval(x, y, w, h);
				break;
			case FILLED_ROUNDRECT:
				g.fillRoundRect(x, y, w, h, 20, 20);
				break;
		}
		//repaint();
	}

	private void repaintRect(int x1, int y1, int x2, int y2) {
	// Call repaint on a rectangle that contains the points (x1,y1)
	// and (x2,y2). (Add a 1-pixel border along right and bottom
	// edges to allow for the pen overhang when drawing a line.)
		int x, y; // top left corner of rectangle that contains the figure
		int w, h; // width and height of rectangle that contains the figure
		if (x2 >= x1) { // x1 is left edge
			x = x1;
			w = x2 - x1;
		} else { // x2 is left edge
			x = x2;
			w = x1 - x2;
		}
		if (y2 >= y1) { // y1 is top edge
			y = y1;
			h = y2 - y1;
		} else { // y2 is top edge.
			y = y2;
			h = y1 - y2;
		}
		repaint(x, y, w + 1, h + 1);
		//repaint();
	}

	private void checkOSI() {
		// This method is responsible for creating the off-screen image.
		// It should be called before using the OSI. It will make a new OSI if
		// the size of the panel changes.
		//if (OSI == null || widthOfOSI != getSize().width || heightOfOSI != getSize().height) {
			// Create the OSI, or make a new one if panel size has changed.
			OSI = null; // If OSI already exists
			OSI = createImage(getSize().width, getSize().height);
			widthOfOSI = getSize().width;
			heightOfOSI = getSize().height;
			Graphics OSG = OSI.getGraphics(); // Graphics context for drawing to OSI.

			OSG.setColor(Color.white);
			OSG.fillRect(0, 0, widthOfOSI, heightOfOSI);
			OSG.dispose();

		//}
	}	

	private Color getCurrentColor() {
		// Check the colorChoice menu to find the currently
		// selected color, and return the appropriate color
		// object.
		int currentColor = 0;
		switch (currentColor) {
			case BLACK:
				return Color.black;
			case RED:
				return Color.red;
			case GREEN:
				return Color.green;
			case BLUE:
				return Color.blue;
			case CYAN:
				return Color.cyan;
			case MAGENTA:
				return Color.magenta;
			case YELLOW:
				return Color.yellow;
			default:
				return Color.white;
		}
	}

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

	@Override
	public void update(Graphics g){
		g.drawImage(OSI, 0, 0, this);
		drawFigure(g, figure, startX, startY, mouseX, mouseY);
	}
}



the applet has three buttons namely oval(to draw oval), rectangle(to draw rectangle), freehand( to perform free hand drawing).

can any one pls explain how the repaintRect() is clearing previously drawn shape and again drawing new shape on the applet

Is This A Good Question/Topic? 0
  • +

Replies To: how the repaintRect() is working in this snippet

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how the repaintRect() is working in this snippet

Posted 06 May 2009 - 04:19 PM

What an horrible piece of code :)
Hope you won't inspire you from that stuff.

This is a less than a half completed project with lots of defects

   private Color getCurrentColor() {
		// Check the colorChoice menu to find the currently
		// selected color, and return the appropriate color
		// object.
		int currentColor = 0;
		switch (currentColor) {
			case BLACK:
				return Color.black;
			case RED:
				return Color.red;
			case GREEN:
				return Color.green;
			case BLUE:
				return Color.blue;


What ever you do this method will always return Color.black;
And switch statement is not the good way to process that type of things

	private final static int BLACK = 0,  RED = 1, GREEN = 2,  BLUE = 3, CYAN = 4, 
		 MAGENTA = 5,  YELLOW = 6,  WHITE = 7;
	private final static color[] = (Color.BLACK, Color.RED, Color.GREEN....


will do that stuff a lot faster with a lot less code... just use color[currentColor]

code has provision for a lot of shapes

   private final static int CURVE = 0,  LINE = 1,  RECT = 2, 
		 OVAL = 3, ROUNDRECT = 4, FILLED_RECT = 5,  FILLED_OVAL = 6,  FILLED_ROUNDRECT = 7;


but only RECT, CURVE and OVAL are supported

This is another example were an array is lot more appropriate than a serie of if
	if (e.getActionCommand().compareTo("FreeHand")==0)
		figureChoice = 0;
	if (e.getActionCommand().compareTo("Line")==0)
		figureChoice = 1;
	if (e.getActionCommand().compareTo("Eraser")==0)
		figureChoice = 0;



The handling of Graphics objects is not done the way it should be handled in a Java GUI
dragGraphics = OSI.getGraphics();


The getGraphics() method was not implemented for this type of handling.. in an Applet, unless you show me an example that proves the contrary, the only Graphics object that you should need to use (and that you SHOULD use) is the one provided to you by the system when it calls your paint() method whith a Graphics object as parameter

As far as your line width when drawing ovals and rectangles, I couldn't reproduce the problem. All my figures have the 1 pixel width perimeter

Happy coding... but you are better to start from scratch
Was This Post Helpful? 0
  • +
  • -

#3 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2651
  • View blog
  • Posts: 15,673
  • Joined: 24-September 08

Re: how the repaintRect() is working in this snippet

Posted 06 May 2009 - 04:28 PM

View Postpbl, on 6 May, 2009 - 03:19 PM, said:

What an horrible piece of code :)
Hope you won't inspire you from that stuff.

...



Is that what you think every time you see my code? >=[
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how the repaintRect() is working in this snippet

Posted 06 May 2009 - 04:37 PM

View PostNeoTifa, on 6 May, 2009 - 03:28 PM, said:

View Postpbl, on 6 May, 2009 - 03:19 PM, said:

What an horrible piece of code :)
Hope you won't inspire you from that stuff.

...



Is that what you think every time you see my code? >=[

Never saw one of your code with such a long snippet :D
Actually as far as I can remember from your code you always manage to reduce/shorten the problem and just post the few lines that generate a problem.
Always a pleasure to answer that type of posting... no need to spend a lot of minutes trying to figure out what all the rest do.

Cheers :wub:
Was This Post Helpful? 0
  • +
  • -

#5 gigantic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 13-April 09

Re: how the repaintRect() is working in this snippet

Posted 06 May 2009 - 09:32 PM

thanks for reply pbl.
but i am just trying to understand the coding there. i changed the method
private Color getCurrentColor() in order to have a single color while testing of it.

but what i want to understand is how the particular method
i.e private void repaintRect(int x1, int y1, int x2, int y2) is working. i wonder where the previously drawn oval while dragging mouse, are disappearing.
i want this functionality. but want to know how the oval shape is continuously changing while dragging mouse an applet. because while dragging mouse the previously drawn pixels should exist. but this method is somehow erasing all those pixels.

i like that. but i didnt understand which line is doing this job

can u explain where it is wiping of those pixels or else can u show me some other good example

as i want to implement the same thing in my assignment
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how the repaintRect() is working in this snippet

Posted 07 May 2009 - 04:55 AM

View Postgigantic, on 6 May, 2009 - 08:32 PM, said:

thanks for reply pbl.
but i am just trying to understand the coding there. i changed the method
private Color getCurrentColor() in order to have a single color while testing of it.

but what i want to understand is how the particular method
i.e private void repaintRect(int x1, int y1, int x2, int y2) is working. i wonder where the previously drawn oval while dragging mouse, are disappearing.
i want this functionality. but want to know how the oval shape is continuously changing while dragging mouse an applet. because while dragging mouse the previously drawn pixels should exist. but this method is somehow erasing all those pixels.

i like that. but i didnt understand which line is doing this job

can u explain where it is wiping of those pixels or else can u show me some other good example

as i want to implement the same thing in my assignment

The method is just figuring out the width and the height of the rectangle depending if you drag the mouse to the left or the right
so:
if x1 > x2 leftmost x is x2 and width is x1 - x2
if x2 > x1 leftmost x is x1 and width is x2 - x1
same thing for the height
then it calls repaint(x, y, width, height) to ask the system to repaint() the area where is the rectangle
Was This Post Helpful? 0
  • +
  • -

#7 gigantic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 13-April 09

Re: how the repaintRect() is working in this snippet

Posted 08 May 2009 - 09:37 AM

hi
i changed the above coding to suit my requirement. now i am able to perform drawing on the panel. now i want to add a feature to change the drawing pen size and eraser size. i.e in MS-Paint we can change the drawing pen size and eraser size whenever we want. i want to add similar functionality here.
can any one tell me how can i incorporate that feature in to my current coding and also suggest me any improvements that i can do for this.
i know there will be lot of improvement that we can brought up in to this.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class demo extends Applet implements ActionListener{
	Display drawingBoard;
	Button btnRectangle;
	Button btnEraser;
	Button btnLine;
	int figure;
	ShapePanel,drawPanel;
	boolean dragging;
	Button btnEllipse;
	Button btnFreeHand;	
	HashMap hmShape;
	Image OSI;
	int widthOfOSI, heightOfOSI;
	int mouseX,  mouseY;
	int prevX,  prevY;
	int startX,  startY;
	private int eraserLength = 5;
	Graphics dragGraphics;
	Graphics OSG;
	@Override
	public void init(){
		drawingBoard = new Display();
		setLayout(new BorderLayout());
		drawingBoard.setVisible(true);
		figure = 0;
		dragging = false;
		hmShape = new HashMap();
		
		shapePanel = new Panel();
		shapePanel.setLayout(new FlowLayout(FlowLayout.LEFT ));
		createhmShape();
		btnEllipse = new Button("Oval");
		btnFreeHand = new Button("FreeHand");
		btnRectangle = new Button("Rectangle");
		btnEraser = new Button("Eraser");
		btnLine = new Button("Line");
		btnEllipse.addActionListener(this);
		btnFreeHand.addActionListener(this);
		btnRectangle.addActionListener(this);
		btnEraser.addActionListener(this);
		btnLine.addActionListener(this);
		shapePanel.add(btnEllipse);
		shapePanel.add(btnFreeHand);
		shapePanel.add(btnRectangle);
		shapePanel.add(btnEraser);
		shapePanel.add(btnLine);

		add(shapePanel,BorderLayout.NORTH);
		add(drawingBoard,BorderLayout.CENTER);
	   

	}

	public void createhmShape(){
		hmShape.put("Oval", 1);
		hmShape.put("Rectangle",2);
		hmShape.put("FreeHand",0);
		hmShape.put("Eraser", 3);
		hmShape.put("Line", 4);
	}

	public void actionPerformed(ActionEvent ae){
		figure = Integer.parseInt(hmShape.get(ae.getActionCommand()).toString());
	}

	class Display extends Panel implements  MouseListener, MouseMotionListener, ActionListener{
		MouseEvent me;
		Display() {
			this.setPreferredSize(new Dimension(300,300));
			addMouseListener(this);
			addMouseMotionListener(this);  
		}

		public void actionPerformed(ActionEvent ae){
			checkOSI();
		}

		private void checkOSI() {
			if (OSI == null) {				 
				OSI = this.createImage(this.getSize().width, this.getSize().height);
				widthOfOSI = this.getSize().width;
				heightOfOSI = this.getSize().height;
				Graphics OSG = OSI.getGraphics(); // Graphics context for drawing to OSI.
				OSG.setColor(Color.white);
				OSG.fillRect(0, 0, widthOfOSI, heightOfOSI);
				OSG.dispose();
			}
		}


		@Override
		public void paint(Graphics g) {
			System.out.println("in mouse paintcomponent");
			System.out.println("width of component is : "+this.getWidth());
			checkOSI();
			g.drawImage(OSI, 0, 0, this);
			if (dragging && figure != 0) {
				drawShpae(g, figure, startX, startY, mouseX, mouseY);
			}
		}

		public void drawShpae(Graphics g,int drawingshape, int x1, int y1, int x2, int y2) {			
			if(drawingshape==0 || drawingshape == 4){
				g.drawLine(x1, y1, x2, y2);
				return;
			}else if(drawingshape==3){
				g.setColor(Color.white);
				g.fillRect(x1, y1, x2, y2);
				return;
			}

			int x, y;
			int w, h;

			if (x1 >= x2) {
				x = x2;
				w = x1 - x2;
			} else {
				x = x1;
				w = x2 - x1;
			}

			if (y1 >= y2) {
				y = y2;
				h = y1 - y2;
			} else {
				y = y1;
				h = y2 - y1;
			}

			switch(drawingshape){
				case 1:
					g.drawOval(x, y, w, h);
					break;
				case 2:
					System.out.println("x = "+x+"y = "+y+"w = "+w+"h = "+h);
					g.drawRect(x, y, w, h);
					break;
			}
		}

		public void mousePressed(MouseEvent evt) {			
			System.out.println("in mouse pressed");
			if (dragging == true)
			{
				return;
			}
			prevX = startX = evt.getX(); // Save mouse coordinates.
			prevY = startY = evt.getY();
			if(OSI!=null){
				dragGraphics = OSI.getGraphics();
			}else{
				checkOSI();
				dragGraphics = OSI.getGraphics();
			}
			if(figure == 3){
				setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			}
			dragging = true;
		}

		public void mouseReleased(MouseEvent evt) {
			
			System.out.println("in mouse released");
			if (dragging == false) {
				return; // Nothing to do because the user isn't drawing.
			}
			dragging = false;
			mouseX = evt.getX();
			mouseY = evt.getY();

			if (figure == 0) {
				drawShpae(dragGraphics, 0, prevX, prevY, mouseX, mouseY);
				repaintRectangle(prevX, prevY, mouseX, mouseY);
			}else if(figure == 4){
				repaintRectangle(startX, startY, prevX, prevY);
				if (mouseX != startX || mouseY != startY) {
					// Draw the line only if it has non-zero length.
					drawShpae(dragGraphics, figure, startX, startY, mouseX, mouseY);
					repaintRectangle(startX, startY, mouseX, mouseY);
				}
			}else {
				repaintRectangle(startX, startY, prevX, prevY);
				if (mouseX != startX && mouseY != startY) {
					if(figure == 3){
						setCursor(Cursor.getDefaultCursor());
					}else{
						drawShpae(dragGraphics, figure, startX, startY, mouseX, mouseY);
					}
					repaintRectangle(startX, startY, mouseX, mouseY);
				}
			}
			dragGraphics.dispose();
			dragGraphics = null;
		}

		public void mouseDragged(MouseEvent evt) {			
			System.out.println("in mouse dragged");
			if (dragging == false) {
				return; // Nothing to do because the user isn't drawing.
			}
			mouseX = evt.getX(); // x-coordinate of mouse.
			mouseY = evt.getY(); // y=coordinate of mouse.

			if (figure == 0) {
				drawShpae(dragGraphics, 0, prevX, prevY, mouseX, mouseY);
				repaintRectangle(prevX, prevY, mouseX, mouseY);
			}else if(figure == 3){
				drawShpae(dragGraphics, figure, prevX, prevY, 2*eraserLength, 2*eraserLength);
				repaintRectangle(mouseX, mouseY, 2*eraserLength, 2*eraserLength);
			}else {
				repaintRectangle(startX, startY, prevX, prevY);
				repaintRectangle(startX, startY, mouseX, mouseY);
			}

			prevX = mouseX;
			prevY = mouseY;

		} // end mouseDragged.

		public void mouseEntered(MouseEvent evt) {
		}

		public void mouseExited(MouseEvent evt) {
		}

		public void mouseClicked(MouseEvent evt) {
		}

		public void mouseMoved(MouseEvent evt) {
		}

		private void repaintRectangle(int x1, int y1, int x2, int y2) {
			System.out.println("in mouse repaintRectangle");
			if(figure == 3){
				repaint(x1, y1, x2, y2);
			}else{
				int x, y; 
				int w, h; 
				if (x2 >= x1) { 
					x = x1;
					w = x2 - x1;
				} else { 
					x = x2;
					w = x1 - x2;
				}
				if (y2 >= y1) { 
					y = y1;
					h = y2 - y1;
				} else { 
					y = y2;
					h = y1 - y2;
				}
				repaint(x, y, w + 1, h + 1);
			}
		}
	}

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

	@Override
	public void update(Graphics g){
		drawingBoard.repaint();
	}
}



also at present when i draw something on the panel, the drawing is going out of panel dimensions( as i set panel dimension to 300X300). i want to restrict this. i don't want the drawing to be performed out of bounds.

after refining this script with the help of some of folks here, i want to post this snippet to our DIC snippet collection(i hope till then other's doesn't submit this)

This post has been edited by gigantic: 08 May 2009 - 09:39 AM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how the repaintRect() is working in this snippet

Posted 08 May 2009 - 03:59 PM

View Postgigantic, on 8 May, 2009 - 08:37 AM, said:

hi
i changed the above coding to suit my requirement. now i am able to perform drawing on the panel. now i want to add a feature to change the drawing pen size and eraser size. i.e in MS-Paint we can change the drawing pen size and eraser size whenever we want. i want to add similar functionality here.
can any one tell me how can i incorporate that feature in to my current coding and also suggest me any improvements that i can do for this.
i know there will be lot of improvement that we can brought up in to this.

I guess you will have to do as MS_Paint probably does. If pen size is 3 draw 3 times the lines... this require few calculations depending on the line orientation. Perfectly horizontal or vertical it is easy just go from x-1 to x+1 or y-1 or y+1 but when other orientation it is not evident.

And BTW do you understand RepaintRec() works now ?
Was This Post Helpful? 0
  • +
  • -

#9 gigantic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 13-April 09

Re: how the repaintRect() is working in this snippet

Posted 08 May 2009 - 06:15 PM

thanks pbl
still i don't understand completely how the repainRect() method is working. but i am able to use it in my coding. that's why i am still looking some other alternative for the drawing purpose.
is there any other approach to accomplish this drawing functionality.
i know the repaintRect is repainting a particular rectangle area in which the drawing has happened.

the main thing i am finding tough is manipulating the mouse points before and after drawing( ie while dragging mouse and after releasing mouse)

any way can i suggest any coding improvements to this snippet.
initially u told me to

Quote

This is another example were an array is lot more appropriate than a serie of if

	if (e.getActionCommand().compareTo("FreeHand")==0)
		figureChoice = 0;
	if (e.getActionCommand().compareTo("Line")==0)
		figureChoice = 1;
	if (e.getActionCommand().compareTo("Eraser")==0)
		figureChoice = 0;


now i am using a hashmap here. can u point some improvements like above
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how the repaintRect() is working in this snippet

Posted 08 May 2009 - 06:52 PM

"i know the repaintRect is repainting a particular rectangle area in which the drawing has happened"

no, repaintRec is asking Swing to repaint a particular rectangle area"

there is a BIG concepteptual difference here. It is not your application that repaints, it is Swing

As far as improvements are concerned, personnaly, and I told you at first post, I'll rewrite it
It will be a good exercise for you and managing your code is always better then trying to figure out what another guy did (not really the good way)... This snippet was useful to teach you how all things have to been glued together now that you understood the concept write your own
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1