Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to run/display this .java file, and with what?

Posted on 2011-02-24
6
466 Views
Last Modified: 2012-05-11
Hi

I have downloaded the attached file, and am trying to run it, have downloaded eclipse, jre development kit, and it will not run, stating it misses a main class? Is the file incomplete?

So my question is = can anyone assist me with running this application?
//Title:        A Simple Drawing Tool
//Version:      1.0
//Copyright:    Copyright (c) 2001
//Author:       Yasir Feroze Minhas
//Company:      KAPS Computing (pvt) Ltd.
//Description:  This is a simple tool written using AWT for drawing basic shapes.

package graph;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SimpleDrawingTool extends Frame{

  //constants for menu shortcuts
  private static final int kControlA = 65;
  private static final int kControlD = 68;
  private static final int kControlC = 67;
  private static final int kControlR = 82;
  private static final int kControlP = 80;
  private static final int kControlT = 84;
  private static final int kControlX = 88;

  private RectangleShape rectangle = new RectangleShape();
  private OvalShape oval = new OvalShape();
  private PolygonShape polygon = new PolygonShape();
  private TriangleShape triangle = new TriangleShape();

  private DrawingPanel panel;

  public SimpleDrawingTool() {
    //set frame's title
    super("Simple Drawing Tool");
    //add menu
    addMenu();
    //add drawing panel
    addPanel();
    //add window listener
    this.addWindowListener(new WindowHandler());
    //set frame size
    this.setSize(400, 400);
    //make this frame visible
    this.setVisible(true);
  }

  public static void main(String[] args) {
    SimpleDrawingTool simpleDrawingTool = new SimpleDrawingTool();
  }
  /**
  This method creates menu bar and menu items and then attach the menu bar
  with the frame of this drawing tool.
  */
  private void addMenu()
  {
    //Add menu bar to our frame
    MenuBar menuBar = new MenuBar();
    Menu file = new Menu("File");
    Menu shape = new Menu("Shapes");
    Menu about = new Menu("About");
    //now add menu items to these Menu objects
    file.add(new MenuItem("Exit", new MenuShortcut(kControlX))).addActionListener(new WindowHandler());

    shape.add(new MenuItem("Rectangle", new MenuShortcut(kControlR))).addActionListener(new WindowHandler());
    shape.add(new MenuItem("Circle", new MenuShortcut(kControlC))).addActionListener(new WindowHandler());
    shape.add(new MenuItem("Triangle", new MenuShortcut(kControlT))).addActionListener(new WindowHandler());
    shape.add(new MenuItem("Polygon", new MenuShortcut(kControlP))).addActionListener(new WindowHandler());
    shape.add(new MenuItem("Draw Polygon", new MenuShortcut(kControlD))).addActionListener(new WindowHandler());

    about.add(new MenuItem("About", new MenuShortcut(kControlA))).addActionListener(new WindowHandler());
    //add menus to menubar
    menuBar.add(file);
    menuBar.add(shape);
    menuBar.add(about);
    //menuBar.setVisible(true);
    if(null == this.getMenuBar())
    {
      this.setMenuBar(menuBar);
    }
  }//addMenu()

  /**
  This method adds a panel to SimpleDrawingTool for drawing shapes.
  */
  private void addPanel()
  {
    panel = new DrawingPanel();
    //get size of SimpleDrawingTool frame
    Dimension d = this.getSize();
    //get insets of frame
    Insets ins = this.insets();
    //exclude insets from the size of the panel
    d.height = d.height - ins.top - ins.bottom;
    d.width = d.width - ins.left - ins.right;
    panel.setSize(d);
    panel.setLocation(ins.left, ins.top);
    panel.setBackground(Color.white);
    //add mouse listener. Panel itself will be handling mouse events
    panel.addMouseListener(panel);
    this.add(panel);
  }//end of addPanel();

  //Inner class to handle events
  private class WindowHandler extends WindowAdapter implements ActionListener
  {
    public void windowClosing(WindowEvent e)
    {
      System.exit(0);
    }

    public void actionPerformed(ActionEvent e)
    {
      //check to see if the action command is equal to exit
      if(e.getActionCommand().equalsIgnoreCase("exit"))
      {
        System.exit(0);
      }
      else if(e.getActionCommand().equalsIgnoreCase("Rectangle"))
      {
        Menu menu = getMenuBar().getMenu(1);
        for(int i = 0;i < menu.getItemCount();menu.getItem(i).setEnabled(true),i++);
        getMenuBar().getShortcutMenuItem(new MenuShortcut(kControlR)).setEnabled(false);
        panel.drawShape(rectangle);

      }
      else if(e.getActionCommand().equalsIgnoreCase("Circle"))
      {
        Menu menu = getMenuBar().getMenu(1);
        for(int i = 0;i < menu.getItemCount();menu.getItem(i).setEnabled(true),i++);
        getMenuBar().getShortcutMenuItem(new MenuShortcut(kControlC)).setEnabled(false);
        panel.drawShape(oval);
      }
      else if(e.getActionCommand().equalsIgnoreCase("Triangle"))
      {
        Menu menu = getMenuBar().getMenu(1);
        for(int i = 0;i < menu.getItemCount();menu.getItem(i).setEnabled(true),i++);
        getMenuBar().getShortcutMenuItem(new MenuShortcut(kControlT)).setEnabled(false);
        panel.drawShape(triangle);
      }
      else if(e.getActionCommand().equalsIgnoreCase("Polygon"))
      {
        Menu menu = getMenuBar().getMenu(1);
        for(int i = 0;i < menu.getItemCount();menu.getItem(i).setEnabled(true),i++);
        getMenuBar().getShortcutMenuItem(new MenuShortcut(kControlP)).setEnabled(false);
        panel.drawShape(polygon);
      }
      else if(e.getActionCommand().equalsIgnoreCase("Draw Polygon"))
      {
        Menu menu = getMenuBar().getMenu(1);
        for(int i = 0;i < menu.getItemCount();menu.getItem(i).setEnabled(true),i++);
        getMenuBar().getShortcutMenuItem(new MenuShortcut(kControlP)).setEnabled(false);
        panel.repaint();
      }
      else if(e.getActionCommand().equalsIgnoreCase("About"))
      {
        JOptionPane.showMessageDialog(null, "This small freeware program is written by Yasir Feroze Minhas.", "About", JOptionPane.PLAIN_MESSAGE);
      }
    }//actionPerformed()

  }//windowHandler - Inner Class ends here
}//SimpleDrawingTool

class DrawingPanel extends Panel implements MouseListener
{

  private Point sPoint = null;
  private Point ePoint = null;
  private Shapes shape = null;
  private java.util.ArrayList list = new java.util.ArrayList();
  //override panel paint method to draw shapes
  public void paint(Graphics g)
  {
    g.setColor(Color.green);
    shape.draw(list, g);
  }
  public void drawShape(Shapes shape)
  {
    this.shape = shape;
  }
  //define mouse handler
  public void mouseClicked(MouseEvent e)
  {
    //if user wants to draw triangle, call repaint after 3 clicks
    if(shape instanceof TriangleShape)
    {
      list.add(e.getPoint());
      if(list.size() > 2)
      {
        repaint();
      }
    }
    else if(shape instanceof PolygonShape)
    {
      list.add(e.getPoint());
    }
  }//mouseClicked
  public void mouseEntered(MouseEvent e){}
  public void mouseExited(MouseEvent e){}
  public void mousePressed(MouseEvent e)
  {
    sPoint = e.getPoint();
  }//mousePressed
  public void mouseReleased(MouseEvent e)
  {
    ePoint = e.getPoint();
    if(ePoint.getX() < sPoint.getX())
    {
      Point temp = ePoint;
      ePoint = sPoint;
      sPoint = temp;
    }
    if(ePoint.getY() < sPoint.getY())
    {
      int temp = (int)ePoint.getY();
      ePoint.y = (int)sPoint.getY();
      sPoint.y = temp;
    }
    if(shape instanceof RectangleShape || shape instanceof OvalShape)
    {
      list.clear();
      list.add(sPoint);
      list.add(ePoint);
      repaint();
    }
  }//mouseReleased
}//DrawingPanel

Open in new window

0
Comment
Question by:PeterdeB
  • 3
  • 2
6 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34968782
0
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 250 total points
ID: 34968799
or simply, right click on the file --> run as--> java application
0
 
LVL 47

Accepted Solution

by:
for_yan earned 250 total points
ID: 34969026
It seem sthat some classes/interfaces are lacking in this code - like Shapes, RectangleShape, OvalShape, etc. - those are not defined
in this code - so you cannot complie - is there some additional library?

I don't have such classes in my standard java distribution
 
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:PeterdeB
ID: 34969356
Actually after adding the missing files, all the answers worked, so i guess i will split the points ok?
0
 

Author Comment

by:PeterdeB
ID: 34969363
qurvinder372, and for_yan thanks for replying so fast, i really appreciate it
0
 

Author Closing Comment

by:PeterdeB
ID: 34971924
Thank you very much
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 82
servlet example 11 49
MySqlDump not dumping triggers 1 43
jsp insert to database example 2 47
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question