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