Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

JDraw


  • Please log in to reply

#1
nimzyjava

nimzyjava

    New Member

  • Member
  • Pip
  • 2 posts
/* Author : Nimesh Oza
* Company name : Synergetics India
* Code Name : JDrawver3
* A simple Java Program used to draw the basic shapes in java
* using AWT. and delegation model for handling events
* Also one can use 3 basic color to draw shapes moreover u can save the shapes and
* retrieve them...... so happy drawing. :tazz:)
*
*/
import java.awt.BorderLayout;
import java.awt.Choice;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Hashtable;
import java.util.Vector;

public class JDrawver3 extends Frame {

/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 1L;

MenuBar mainMenuBar = new MenuBar();

Menu fileMenu = new Menu("File");

Menu shapesMenu = new Menu("Shapes");

Menu aboutMenu = new Menu("About");

MenuItem openMenuItem = new MenuItem("Open");

MenuItem saveMenuItem = new MenuItem("Save");

MenuItem exitMenuItem = new MenuItem("Exit");

MenuItem newMenuItem = new MenuItem("New");

MenuItem lineMenuItem = new MenuItem("Line");

MenuItem rectangleMenuItem = new MenuItem("Rectangle");

MenuItem ellipseMenuItem = new MenuItem("Ellipse");

MenuItem eraseShape = new MenuItem("Erase");

Choice colorChoice = new Choice();

Panel mainPanel = new Panel();

Shapes currentShape = new LineShape();

Vector shapesVector = new Vector();

Hashtable colorhash = new Hashtable();

Color currentColor = Color.BLACK;

FileDialog saveFileDailog = new FileDialog(this, "Save", FileDialog.SAVE);

FileDialog openFileDailog = new FileDialog(this, "Open", FileDialog.LOAD);



public void addChoice() {
setLayout(new BorderLayout());
add(mainPanel, BorderLayout.NORTH);
colorChoice.add("Black");
colorhash.put("Black", Color.BLACK);
colorChoice.add("Red");
colorhash.put("Red", Color.RED);
colorChoice.add("Blue");
colorhash.put("Blue", Color.BLUE);
colorChoice.add("Green");
colorhash.put("Green", Color.GREEN);
mainPanel.add(colorChoice);
}

public void addMenu() {
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(exitMenuItem);
shapesMenu.add(lineMenuItem);
shapesMenu.add(rectangleMenuItem);
shapesMenu.add(ellipseMenuItem);
shapesMenu.add(eraseShape);
mainMenuBar.add(fileMenu);
mainMenuBar.add(shapesMenu);
setMenuBar(mainMenuBar);
}

public void addListeners() {
addWindowListener(new CloseWindowListener());
lineMenuItem.addActionListener(new LineListener());
rectangleMenuItem.addActionListener(new RectangleListener());
ellipseMenuItem.addActionListener(new EllipseListener());
eraseShape.addActionListener(new EraseListener());
addMouseListener(new NetJDrawMouseListener());
addMouseMotionListener(new NetJDrawMouseMotionListener());
colorChoice.addItemListener(new ColorListener());
saveMenuItem.addActionListener(new SaveListener());
openMenuItem.addActionListener(new OpenListener());
exitMenuItem.addActionListener(new ExitListener());
newMenuItem.addActionListener(new NewLsitener());

}

public JDrawver3() {

// Add the menus to the Frame
addMenu();

addListeners();

addChoice();

setVisible(true);

// Problem: The window is not maximized, and it does not close
setSize(400, 400);
// Problem: The window is maximized but it still does not close
// So by adding handleEvent() method one can use various fields and
// write the
// implementation for Handling events using if else or switch case
// Introduce the Delegation Model and implement the
// ActionListener/Adapter using Inner Classes
// add the MouseListener with the mousePressed and mouseReleased methods
// add the if else for various shapes....
// since the shapes appear only after the mouse release event fired
// add MouseMotionListener with the implementation for mouseDragged
// method
// in mouse Pressed get the x1, and y1 coordinates and in mouse Released
// get
// the x2 and y2 coordinates the shapes still appear only after the
// mouse
// click only............
// so rectify this error by getting the x2 and y2 coordinates in
// MouseMotion
// Listener with implementation of mouseDragged method get the x2 and y2
// coordinates in
// method and draw the respective shape in mouseDragged method.....
// now since there are lots a lines being drawn instead of 1 line u need
// to use XORMode
// to prevent the unecessary lines from being drawn
// so get the instance of Graphics and set XORMode(Color.WHITE);... then
// redraw the shape so dat the extra lines drawn will get erased... by
// the XORMode shapes
// Drawn ... the steps hav 2 b followed in the mouseReleased and
// mouseDragged methods

}

class ExitListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
}

class NewLsitener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
shapesVector.clear();
repaint();

}

}

class OpenListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
openFileDailog.show(true);
System.out.println(openFileDailog.getDirectory()
+ openFileDailog.getFile());
System.out.println(openFileDailog.getFile());

String fileName = openFileDailog.getDirectory()
+ openFileDailog.getFile();
System.out.println("Filename " + fileName);
File openFile = new File(fileName);
ObjectInputStream ois = null;
try {
Graphics g = getGraphics();
ois = new ObjectInputStream(new FileInputStream(openFile));
shapesVector = (Vector) ois.readObject();
repaint();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
ois.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}

class SaveListener implements ActionListener {

public void actionPerformed(ActionEvent ae) {
saveFileDailog.show(true);
String fileName = saveFileDailog.getDirectory() + ""
+ saveFileDailog.getFile();
System.out.println(fileName);
//File saveFile = new File(fileName);
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(fileName));
oos.writeObject(shapesVector);
oos.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
oos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}

class ColorListener implements ItemListener {
public void itemStateChanged(ItemEvent ie) {
String colorSelected = colorChoice.getSelectedItem();
currentColor = (Color) colorhash.get(colorSelected);
}
}

class LineListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
currentShape = new LineShape();
}
}

class RectangleListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
currentShape = new RectangleShape();// 3rd
}
}

class EllipseListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
currentShape = new EllipseShape();// 3rd
}
}

class EraseListener implements ActionListener
{

public void actionPerformed(ActionEvent ae) {
currentShape = new Eraser();
}

}

class NetJDrawMouseMotionListener extends MouseMotionAdapter {
public void mouseDragged(MouseEvent me) {
if (currentShape.getClass().getName() == "Eraser") {
Graphics g1 = getGraphics();// 4thb
g1.setXORMode(Color.WHITE);// 4thb
currentShape.drawCursor(g1);// 4thb
Graphics g = getGraphics();// 2nd
currentShape.setStartPoint(me.getPoint());// 2nd
currentShape.drawCursor(g);// 2nd

} else {
Graphics g1 = getGraphics();// 4thb
g1.setXORMode(Color.WHITE);// 4thb
currentShape.draw(g1);// 4thb

currentShape.setEndPoint(me.getPoint());// 2nd
currentShape.draw(g1);// 2nd

}
}
}

class NetJDrawMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent me) {
currentShape.setForegroundColor(currentColor);
currentShape.setStartPoint(me.getPoint());// 1st
currentShape.setEndPoint(me.getPoint());// 1st
}

public void mouseReleased(MouseEvent me) {
Graphics g = getGraphics();// 1st
currentShape.setEndPoint(me.getPoint());// 1st
currentShape.draw(g);// 1st
shapesVector.addElement(currentShape);// 5tha
currentShape = currentShape.newShape();
}
}

public void paint(Graphics g) {
for (int i = 0; i < shapesVector.size(); i++) {
Shapes temp = (Shapes) shapesVector.elementAt(i);
temp.draw(g);
}
}// 5thb

class CloseWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}

public static void main(String[] args) {
JDrawver3 njd = new JDrawver3();
}
}

import java.awt.Graphics;

/*
* Created on Dec 12, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author nimeshoza
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class EllipseShape extends Shapes{


public void draw(Graphics g) {
int x1 = getStartPoint().x;
int y1 = getStartPoint().y;
int x2 = getEndPoint().x;
int y2 = getEndPoint().y;
g.setColor(getForegroundColor());

g.drawOval(x1, y1, x2 - x1, y2 - y1);
}//3rd

/* (non-Javadoc)
* @see Shapes#newShape()
*/
public Shapes newShape() {
// TODO Auto-generated method stub
return new EllipseShape();
}//6th


}

import java.awt.Graphics;

public class Eraser extends Shapes{

public Eraser() {
}

public void drawCursor(Graphics g)
{
g.drawRect(getStartPoint().x - 10, getStartPoint().y - 10, 18, 18 );
System.out.println("drawCursor");
}

/* (non-Javadoc)
* @see Shapes#draw(java.awt.Graphics)
*/
public void draw(Graphics g) {

}

/* (non-Javadoc)
* @see Shapes#newShape()
*/
public Shapes newShape() {
// TODO Auto-generated method stub
return new Eraser();
}

}


import java.awt.Graphics;


public class LineShape extends Shapes{

public void draw(Graphics g) {
g.setColor(getForegroundColor());
g.drawLine(getStartPoint().x, getStartPoint().y, getEndPoint().x, getEndPoint().y);
}

/* (non-Javadoc)
* @see Shapes#newShape()
*/
public Shapes newShape() {
// TODO Auto-generated method stub
return new LineShape();
}//6th


}

import java.awt.Graphics;

/*
* Created on Dec 12, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author nimeshoza
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class RectangleShape extends Shapes{

public void draw(Graphics g) {
int x1 = getStartPoint().x;
int y1 = getStartPoint().y;
int x2 = getEndPoint().x;
int y2 = getEndPoint().y;
g.setColor(getForegroundColor());
g.drawRect(x1, y1, x2 - x1, y2 - y1);
}//3rd

/* (non-Javadoc)
* @see Shapes#newShape()
*/
public Shapes newShape() {
// TODO Auto-generated method stub

return new RectangleShape();
}//6th


}

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.io.Serializable;

/*
* Created on Dec 12, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author nimeshoza
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public abstract class Shapes implements Serializable{
Point startPoint = new Point(0, 0);
Point endPoint = new Point(0, 0);
Color foregroundColor = new Color(0);

public Color getForegroundColor() {
return foregroundColor;
}
public void setForegroundColor(Color foregroundColor) {
this.foregroundColor = foregroundColor;
}
public Point getEndPoint() {
return endPoint;
}
public void setEndPoint(Point endPoint) {
this.endPoint = endPoint;
}
public Point getStartPoint() {
return startPoint;
}
public void setStartPoint(Point startPoint) {
this.startPoint = startPoint;
}
public abstract void draw(Graphics g);

public abstract Shapes newShape();//6th

public void drawCursor(Graphics g)
{}
}

Edited by nimzyjava, 28 December 2005 - 01:02 AM.

  • 0

Advertisements







Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP