Skip to main content

simple java drawing packahge

2 replies [Last post]
philly hustler
Offline
Joined: 2006-02-17
Points: 0

hi i am inexperiecned in java. i am trying to create a simple drawing packaage that draws squares, circles and squares. i do not want u to do it for me, i was just wondering if you could give some pointers, or have any ideas where to find the material i need to find the answer.

here is my code:

<br />
/** GrafPack.java  -  Version 1.0 - created by Philly hustler - 2003 */<br />
import java.awt.*;<br />
import javax.swing.*;<br />
import java.awt.event.*;  </p>
<p>abstract class Shape<br />
{<br />
	/** This is the base class for Shapes in the application. It must be subclassed */<br />
	Shape()   // constructor<br />
	{<br />
	}</p>
<p>	abstract void draw(Graphics g);<br />
	/** This method will draw the shape using the supplied Graphics object */<br />
}</p>
<p>class Square extends Shape<br />
{<br />
	/** This class contains the specific details for a square defined in terms of opposite corners */</p>
<p>	Point keyPt=null, oppPt=null;      // these points identify opposite corners of the square</p>
<p>	Square(Point keyPt, Point oppPt)   // constructor<br />
	{<br />
		this.keyPt = keyPt;<br />
		this.oppPt = oppPt;<br />
	}</p>
<p>	void draw(Graphics g)<br />
	{<br />
		/** This method draws the square by calculating the positions of the other 2 corners */<br />
		double xDiff,yDiff,xMid,yMid;   // range and mid points of x & y  </p>
<p>		// calculate ranges and mid points<br />
		xDiff= oppPt.getX()-keyPt.getX();<br />
		yDiff= oppPt.getY()-keyPt.getY();<br />
		xMid = (oppPt.getX()+keyPt.getX())/2;<br />
		yMid = (oppPt.getY()+keyPt.getY())/2;</p>
<p>		// draw square<br />
		g.drawLine((int)keyPt.getX(),(int)keyPt.getY(),(int)(xMid+yDiff/2),(int)(yMid-xDiff/2));<br />
		g.drawLine((int)(xMid+yDiff/2),(int)(yMid-xDiff/2),(int)oppPt.getX(),(int)oppPt.getY());<br />
		g.drawLine((int)oppPt.getX(),(int)oppPt.getY(),(int)(xMid-yDiff/2),(int)(yMid+xDiff/2));<br />
		g.drawLine((int)(xMid-yDiff/2),(int)(yMid+xDiff/2),(int)keyPt.getX(),(int)keyPt.getY());<br />
	}<br />
}</p>
<p>public class GrafPack extends JFrame implements ActionListener,MouseListener<br />
{<br />
	/** This class provides the screen and handles all the interactions. It responds to<br />
            the menu and mouse clicks in the drawing area */</p>
<p>	// menu components<br />
	JMenuBar mainMenu;<br />
	JMenu fileMenu;<br />
	JMenu createMenu;<br />
	JMenu transformMenu;<br />
	JMenu colourMenu;<br />
	JMenuItem exit;<br />
	JMenuItem createSquare;<br />
	JMenuItem createCircle;<br />
	JMenuItem createRectangle;<br />
	JMenuItem createTriangle;<br />
	JMenuItem move;<br />
	JMenuItem fillRed;<br />
	JMenuItem fillGreen;<br />
	JMenuItem fillBlue;</p>
<p>	// varaibles used in processing<br />
	Shape aShape=null;              // holds reference to current shape<br />
	Point nextPoint = null;         // used to collect point from mouse event<br />
	String command=" ";             // command that was issued from menu</p>
<p>	public GrafPack()<br />
	{<br />
		/** Constructor that sets up the screen and processes the required actions */</p>
<p>		super("2D Drawing Package");	        		// call base class constructor<br />
		Dimension size = Toolkit.getDefaultToolkit().getScreenSize();  // get screen dimensions<br />
		setBounds(0,0,size.width,size.height);                         // use all screen</p>
<p>		mainMenu = new JMenuBar();              // main menu bar</p>
<p>		fileMenu = new JMenu("File");           // main menu options<br />
		createMenu =  new JMenu("Create");<br />
		transformMenu = new JMenu("Transform");<br />
    colourMenu = new JMenu("Colour");</p>
<p>    exit = new JMenuItem("Exit");                       // menu items<br />
		createSquare = new JMenuItem("Create Square");<br />
		createCircle = new JMenuItem("Create Circle");<br />
		move = new JMenuItem("Move");<br />
    fillRed = new JMenuItem("Fill Red");<br />
    fillGreen = new JMenuItem("Fill Green");<br />
    fillBlue = new JMenuItem("Fill Blue");   </p>
<p>		// Build Menus<br />
		setJMenuBar(mainMenu);</p>
<p>		fileMenu.add(exit);</p>
<p>		mainMenu.add(fileMenu);<br />
		mainMenu.add(createMenu);<br />
		mainMenu.add(transformMenu);<br />
    mainMenu.add(colourMenu); </p>
<p>    createMenu.add(createSquare);<br />
		createMenu.add(createCircle);<br />
		createMenu.add(createRectangle);<br />
		createMenu.add(createTriangle);<br />
		transformMenu.add(move);<br />
		colourMenu.add(fillRed);<br />
		colourMenu.add(fillGreen);<br />
		colourMenu.add(fillBlue);</p>
<p>		// activate action listeners<br />
		exit.addActionListener(this);<br />
		createSquare.addActionListener(this);<br />
		createCircle.addActionListener(this);<br />
		move.addActionListener(this);</p>
<p>		// and mouse listener<br />
		addMouseListener(this);</p>
<p>		// see the screen<br />
		setVisible(true);</p>
<p>		// process comamnds. The command will be set by the action performed in response to menu selections<br />
		while (!command.equals("Exit"))<br />
		{<br />
			processCommand();	// act upon selected command<br />
		}</p>
<p>		// On leaving loop, closedown<br />
		System.exit(0);<br />
	}</p>
<p>	public void actionPerformed(ActionEvent e)<br />
	{<br />
		/** This extracts the text from the menu item for processing by the constructor of GrafPack.<br />
	      Note it should not involve much processing or it will hold up the event processing thread */</p>
<p>		aShape=null;                            // cancel any incomplete shapes<br />
		nextPoint=null;	                        // and any waiting points<br />
		command = e.getActionCommand();         // collect String used to define menu item's text<br />
	}</p>
<p>	void processCommand()<br />
	{<br />
		/** This method handles all the comamnds selected from the menu */<br />
		if (command.equals("Create Square"))<br />
		{<br />
			aShape = new Square(getPoint(),getPoint());     // create the square by 2 mouse clicks<br />
			repaint();  					// redraw screen<br />
		}<br />
	}</p>
<p>	public void mouseClicked(MouseEvent eve)<br />
	{<br />
		/** This method collects a point from the mouse clicks. It must not involve much processing<br />
	      or it will hold up the event thread.*/</p>
<p>		nextPoint = eve.getPoint();     // collect point<br />
		command =" ";                   // reset command that was last called.<br />
	}</p>
<p>	Point getPoint()<br />
	{<br />
		/** This method provides a point after a mouse click */</p>
<p>		Point aPoint = null;       // Point to be returned<br />
		while (nextPoint==null);   // wait for mouse click to change nextPoint</p>
<p>		aPoint = nextPoint;				// store the Point found<br />
		nextPoint=null;					// reset nextPoint<br />
		return aPoint;<br />
	}</p>
<p>	// these methods need to be here but have no effect<br />
	public void 	mouseEntered(MouseEvent e)<br />
	{<br />
	}<br />
	;<br />
	public void 	mouseExited(MouseEvent e)<br />
	{<br />
	}<br />
	;<br />
	public void 	mousePressed(MouseEvent e)<br />
	{<br />
	}<br />
	;<br />
	public void 	mouseReleased(MouseEvent e)<br />
	{<br />
	}<br />
	;</p>
<p>	public void paint(Graphics g)<br />
	{<br />
		// to redraw screen<br />
		super.paint(g);         // repaint parent<br />
		if (aShape != null)<br />
		aShape.draw(g);         // draw the shape if it exists<br />
	}</p>
<p>	public static void main(String[] args)<br />
	{<br />
		GrafPack gPack = new GrafPack();<br />
	}<br />
}<br />

the code doesnt compile though!!it returns this error! any pointers much appreciated.

<br />
java.lang.NullPointerException<br />
	at javax.swing.JMenu.add(JMenu.java:563)<br />
	at GrafPack.(GrafPack.java:110)<br />
	at GrafPack.main(GrafPack.java:208)<br />
Exception in thread "main"<br />

Thanks

Message was edited by: shan-man (formatted code and added [ code ] tags)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kor_wanderer
Offline
Joined: 2003-08-18
Points: 0

createMenu.add(createTriangle);
createTreangle is unintialized class member
so trying to add it to menu raises NullPointerException

comment:It is highly recommended to use java.awt.shape as the standart class wich representates Shapes.

philly hustler
Offline
Joined: 2006-02-17
Points: 0

thanks for the help. much appreciated