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

New Topic/Question
Reply




MultiQuote








|