The Artima Developer Community
Sponsored Link

Java Answers Forum
simple java drawing packahge

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
luke

Posts: 7
Nickname: phillyhus
Registered: Dec, 2003

simple java drawing packahge Posted: Dec 9, 2003 11:22 AM
Reply to this message Reply
Advertisement
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

Square(Point keyPt, Point oppPt) // constructor
{
this.keyPt = keyPt;
this.oppPt = oppPt;
}

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

// calculate ranges and mid points
xDiff= oppPt.getX()-keyPt.getX();
yDiff= oppPt.getY()-keyPt.getY();
xMid = (oppPt.getX()+keyPt.getX())/2;
yMid = (oppPt.getY()+keyPt.getY())/2;

// draw square
g.drawLine((int)keyPt.getX(),(int)keyPt.getY(),(int)(xMid+yDiff/2),(int) (yMid-xDiff/2));
g.drawLine((int)(xMid+yDiff/2),(int)(yMid-xDiff/2),(int)oppPt.g etX(),(int)oppPt.getY());
g.drawLine((int)oppPt.getX(),(int)oppPt.getY(),(int)(x Mid-yDiff/2),(int)(yMid+xDiff/2));
g.drawLine((int)(xMid-yDiff/2),(int)(yMid+xDi ff/2),(int)keyPt.getX(),(int)keyPt.getY());
}
}

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 */

// menu components
JMenuBar mainMenu;
JMenu fileMenu;
JMenu createMenu;
JMenu transformMenu;
JMenu colourMenu;
JMenuItem exit;
JMenuItem createSquare;
JMenuItem createCircle;
JMenuItem createRectangle;
JMenuItem createTriangle;
JMenuItem move;
JMenuItem fillRed;
JMenuItem fillGreen;
JMenuItem fillBlue;


// 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);

createMenu.add(createSquare);
createMenu.add(createCircle);
createMenu.add(cr eateRectangle);
createMenu.add(createTriangle);
transformMenu.add(move);
colourM enu.add(fillRed);
colourMenu.add(fillGreen);
colourMenu.add(fillBlue);

// 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"

Thanks

Topic: how to validate username and password in java Previous Topic   Next Topic Topic: Garbage Collection in Multithreaded App

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use