0 members and 442 guests
No Members online
» Site Navigation
» Stats
Members: 35,443
Threads: 103,072
Posts: 826,684
Top Poster: cc.RadillacVIII (7,429)
|
-
A simple drawing program.
this is a simple drawing program. Should already be commented appropriately.
Code:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
/**
* YEAH!
*/
public class DrawingProgram extends Applet
implements MouseListener, MouseMotionListener {
private static final int
BLACK = 0,
RED = 1, // Some constants to make
GREEN = 2, // the code more readable.
BLUE = 3, // These numbers code for
CYAN = 4, // the differnet drawing colors.
MAGENTA = 5,
YELLOW = 6;
private static final int PALETTE_WIDTH = 56;
private static final int WHITE_AREA = 50;
private static final int BORDER_WIDTH = 3;
private int currentColour = BLACK; // The currently selected drawing color,
// coded as one of the above constants.
private int prevX, prevY; // The previous location of the mouse.
private boolean isDragging; // This is set to true while the user is drawing.
private Graphics graphicsContext; // A graphics context for the applet
// that is used to draw the user's curve.
/**
* Add listeners to the applet.
*/
public void init() {
addMouseListener(this);
addMouseMotionListener(this);
}
/**
* Overriding update so that it doesn't fill the applet with
* bg colour when callin paint()
* @see java.awt.Container#update(java.awt.Graphics)
*/
public void update(Graphics g) {
paint(g);
}
/**
* Overriding paint method; draws contents of the applet.
* @see java.awt.Container#paint(java.awt.Graphics)
*/
public void paint(Graphics g) {
int width = getSize().width; // Width of the applet.
int height = getSize().height; // Height of the applet.
// Distance between the top of one colored rectangle in the palette
// and the top of the rectangle below it.
int colourSpacing = (height - PALETTE_WIDTH) / 7;
// fill the drawing area.
g.setColor(Color.white);
g.fillRect(BORDER_WIDTH, BORDER_WIDTH, width - 59, height - 6);
//draw the border.
g.setColor(Color.gray);
g.drawRect(0, 0, width-1, height-1);
g.drawRect(1, 1, width-BORDER_WIDTH, height-BORDER_WIDTH);
g.drawRect(2, 2, width-5, height-5);
//draw the colour palette
g.fillRect(width - PALETTE_WIDTH, 0, PALETTE_WIDTH, height);
//draw the clear button
g.setColor(Color.white);
g.fillRect(width-53, height-53, WHITE_AREA, WHITE_AREA);
g.setColor(Color.black);
g.drawRect(width-53, height-53, 49, 49);
g.drawString("CLEAR", width-48, height-23);
//draw the colour swatches
g.setColor(Color.black);
g.fillRect(width-53, BORDER_WIDTH + 0*colourSpacing,
WHITE_AREA, colourSpacing-BORDER_WIDTH);
g.setColor(Color.red);
g.fillRect(width-53, BORDER_WIDTH + 1*colourSpacing,
WHITE_AREA, colourSpacing-BORDER_WIDTH);
g.setColor(Color.green);
g.fillRect(width-53, BORDER_WIDTH + 2*colourSpacing,
WHITE_AREA, colourSpacing-BORDER_WIDTH);
g.setColor(Color.blue);
g.fillRect(width-53, BORDER_WIDTH + BORDER_WIDTH*colourSpacing,
WHITE_AREA, colourSpacing-BORDER_WIDTH);
g.setColor(Color.cyan);
g.fillRect(width-53, BORDER_WIDTH + 4*colourSpacing,
WHITE_AREA, colourSpacing-BORDER_WIDTH);
g.setColor(Color.magenta);
g.fillRect(width-53, BORDER_WIDTH + 5*colourSpacing,
WHITE_AREA, colourSpacing-BORDER_WIDTH);
g.setColor(Color.yellow);
g.fillRect(width-53, BORDER_WIDTH + 6*colourSpacing,
WHITE_AREA, colourSpacing-BORDER_WIDTH);
//draw colour highlight
g.setColor(Color.white);
g.drawRect(width-55, 1 + currentColour*colourSpacing, 53, colourSpacing);
g.drawRect(width-54, 2 + currentColour*colourSpacing, 51, colourSpacing-2);
}
/**
* Change the drawing colour after the user has clicked the mouse
* on the colour palette.
* @param y
*/
private void changeColor(int y) {
int width = getSize().width; // Width of applet.
int height = getSize().height; // Height of applet.
int colorSpacing = (height - PALETTE_WIDTH) / 7; // Space for one color rectangle.
int newColor = y / colorSpacing; // Which color number was clicked?
if (newColor < 0 || newColor > 6) // Make sure the color number is valid.
return;
/* Remove the hilite from the current color, by drawing over it in gray.
Then change the current drawing color and draw a hilite around the
new drawing color. */
Graphics g = getGraphics();
g.setColor(Color.gray);
g.drawRect(width-55, 1 + currentColour*colorSpacing, 53, colorSpacing);
g.drawRect(width-54, 2 + currentColour*colorSpacing, 51, colorSpacing-2);
currentColour = newColor;
g.setColor(Color.white);
g.drawRect(width-55, 1 + currentColour*colorSpacing, 53, colorSpacing);
g.drawRect(width-54, 2 + currentColour*colorSpacing, 51, colorSpacing-2);
g.dispose();
}
/**
* Set the colour of the current sketch.
*/
private void setUpDrawingGraphics() {
graphicsContext = getGraphics();
switch (currentColour) {
case BLACK:
graphicsContext.setColor(Color.black);
break;
case RED:
graphicsContext.setColor(Color.red);
break;
case GREEN:
graphicsContext.setColor(Color.green);
break;
case BLUE:
graphicsContext.setColor(Color.blue);
break;
case CYAN:
graphicsContext.setColor(Color.cyan);
break;
case MAGENTA:
graphicsContext.setColor(Color.magenta);
break;
case YELLOW:
graphicsContext.setColor(Color.yellow);
break;
}
}
/**
* Called when the user presses the button anywhere.
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
*/
public void mousePressed(MouseEvent evt) {
int x = evt.getX(); // x-coordinate where the user clicked.
int y = evt.getY(); // y-coordinate where the user clicked.
int width = getSize().width; // Width of the applet.
int height = getSize().height; // Height of the applet.
//do nothing if already drawing.
if (isDragging == true)
return;
if (x > width - 53) {
//selects the colour palette.
if (y > height - 53)
repaint(); // Clicked on "CLEAR button".
else
changeColor(y); // Clicked on the color palette.
}
else if (x > BORDER_WIDTH && x < width - PALETTE_WIDTH && y > BORDER_WIDTH && y < height - BORDER_WIDTH) {
// The user has clicked on the white drawing area.
// Start drawing a curve from the point (x,y).
prevX = x;
prevY = y;
isDragging = true;
setUpDrawingGraphics();
}
}
/**
* Called whenever the user releases the mouse button.
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
*/
public void mouseReleased(MouseEvent evt) {
// Nothing to do because the user isn't drawing.
if (isDragging == false)
return; // Nothing to do because the user isn't drawing.
isDragging = false;
graphicsContext.dispose();
graphicsContext = null;
}
/**
* Called whenever the user moves the mouse
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
*/
public void mouseDragged(MouseEvent evt) {
if (isDragging == false)
return; // Nothing to do because the user isn't drawing.
int x = evt.getX(); // x-coordinate of mouse.
int y = evt.getY(); // y=coordinate of mouse.
if (x < BORDER_WIDTH) // Adjust the value of x,
x = BORDER_WIDTH; // to make sure it's in
if (x > getSize().width - 57) // the drawing area.
x = getSize().width - 57;
if (y < BORDER_WIDTH) // Adjust the value of y,
y = BORDER_WIDTH; // to make sure it's in
if (y > getSize().height - 4) // the drawing area.
y = getSize().height - 4;
graphicsContext.drawLine(prevX, prevY, x, y); // Draw the line.
prevX = x; // Get ready for the next line segment in the curve.
prevY = y;
}
// implementing the MouseListener interface requires us to
// define these methods, even if we never use them.
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseMoved(MouseEvent evt) { }
}
Last edited by carrotderek; 08-29-2008 at 09:27 AM.
-
Damn derek nice work. You code this is your spare time?
-
No this was for a first year course at school. I wouldn't code Java in my spare time haha. I'm not a fan of it.
-
not to bring up an old thread or anything..but uhh..what's it do?
My DevART
RATCHET is my bitch
Andrew says:
u ever stolen a bible?
Apathy says:
no
used the last two pages to roll a joint though
Andrew says:
wow
thats fucking hard core
^^HAHAHA, dm sucks XD
-
Originally Posted by Papa
not to bring up an old thread or anything..but uhh..what's it do?
A basic drawing program like paint
Similar Threads
-
By Dutch-Soldier(nl) in forum Digital Art
Replies: 6
Last Post: 03-09-2006, 03:40 PM
-
By BaNgRnLeEvR in forum Sigs & Manips
Replies: 3
Last Post: 10-22-2005, 06:25 PM
-
By Juicy in forum The Void
Replies: 11
Last Post: 06-29-2005, 01:29 AM
-
By EMPeiTQ in forum The Void
Replies: 2
Last Post: 06-28-2005, 09:03 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|