Solved

ClockPanel file compules but then gives an error message

Posted on 2004-10-11
11
272 Views
Last Modified: 2008-02-01
I have a file that I am trying to view as an applet as well as an application.  So far, I have the code for the application but when I go to command prompt, it compiles for me, yet when I try to run the program, I am given an error message.  Would anyone be able to assist me in trying to figure out what the problem is?  Thank you.  (I have two files included on here -- one with the .java file and another with .class file.  My ultimate goal is to design a simple clock animation first as an applet and then as an application.  Somehow I've got to implement it as a Runnable interface and put the code for the animation in the "run" method, making a thread object launch the thread.  Also, the app's are supposed to show an analog and digital combination hat can display the time in at least three different zones.  What kind of HTML code could be used with this?)  Any help would be great, thank you.

Here's the .java file:

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

public class ClockPanel extends JPanel
{  

    private int hour, minute, second;
    private Font clockFaceFont;     // Font for number display on clock
    private Color backColor;        // Color of the background
    private Color handColor;        // Color of main hands and dial
    private Color numberColor;      // Color of second hand and numbers

       
    public ClockPanel()
    {
        // set the font and colors for the clock display
        clockFaceFont = new Font( "Serif", Font.PLAIN, 14 );
        backColor = Color.black;
        handColor = Color.green;
        numberColor = new Color( Integer.parseInt( "ff00ff", 16 ) );
    }

    public void setTime( int h, int m, int s )
    {
        hour = h;
        minute = m;
        second = s;
    }
   
    // Plotpoints allows calculation to only cover 45 degrees of the circle,
    // and then mirror
    private void plotpoints( int x0, int y0, int x, int y, Graphics g )
    {
        g.drawLine( x0+x, y0+y, x0+x, y0+y );
        g.drawLine( x0+y, y0+x, x0+y, y0+x );
        g.drawLine( x0+y, y0-x, x0+y, y0-x );
        g.drawLine( x0+x, y0-y, x0+x, y0-y );
        g.drawLine( x0-x, y0-y, x0-x, y0-y );
        g.drawLine( x0-y, y0-x, x0-y, y0-x );
        g.drawLine( x0-y, y0+x, x0-y, y0+x );
        g.drawLine( x0-x, y0+y, x0-x, y0+y );
    }

    // Circle is just Bresenham's algorithm for a scan converted circle
    // center - (x0, y0); radius - r
    private void circle( int x0, int y0, int r, Graphics g )
    {
        int x,y;
        float d;
        x = 0;
        y = r;
        d = (5 / 4) - r;
        plotpoints( x0, y0, x, y, g );

        while ( y > x ){
            if ( d < 0) {
                d = d+2*x+3;
                x++;
            }
            else {
                d = d + 2*(x-y) + 5;
                x++;
                y--;
            }
            plotpoints( x0, y0, x, y, g );
        }
    }


    public void paintComponent( Graphics g )
    {
        int xh, yh, xm, ym, xs, ys, xcenter, ycenter, radius;
       
        super.paintComponent( g );

        // get the center for the clock display based on the current
        // width and height of the panel
        xcenter = getWidth() / 2;
        ycenter = getHeight() / 2;
       
         // diameter is 90% of the min. of width & height
        radius = (int) (0.9 * Math.min( getWidth(), getHeight() ) / 2);
   
        // a= s* pi/2 - pi/2 (to switch 0,0 from 3:00 to 12:00)
        // x = r(cos a) + xcenter, y = r(sin a) + ycenter
   
        // the seconds hand length is 90% of the radius
        // the minutes hand length is 80% of the radius
        // the hour hand length is 60% of the radius
        xs = (int)(Math.cos(second * 3.14f/30 - 3.14f/2) * (radius * 0.9) + xcenter);
        ys = (int)(Math.sin(second * 3.14f/30 - 3.14f/2) * (radius * 0.9) + ycenter);
        xm = (int)(Math.cos(minute * 3.14f/30 - 3.14f/2) * (radius * 0.8) + xcenter);
        ym = (int)(Math.sin(minute * 3.14f/30 - 3.14f/2) * (radius * 0.8) + ycenter);
        xh = (int)(Math.cos((hour*30 + minute/2) * 3.14f/180 - 3.14f/2) * (radius * 0.6) + xcenter);
        yh = (int)(Math.sin((hour*30 + minute/2) * 3.14f/180 - 3.14f/2) * (radius * 0.6) + ycenter);
   
        // Draw the circle and numbers
   
        setBackground( backColor );
        g.setFont( clockFaceFont );
        g.setColor( handColor );
        circle( xcenter, ycenter, radius, g );
        g.setColor( numberColor );
        g.drawString( "9", xcenter-radius+5, ycenter+3 );
        g.drawString( "3", xcenter+radius-10, ycenter+3);
        g.drawString( "12", xcenter-5, ycenter-radius+13);
        g.drawString( "6", xcenter-3, ycenter+radius-5);

        // Draw the three hands
        g.drawLine( xcenter, ycenter, xs, ys );
        g.setColor( handColor );
        g.drawLine( xcenter, ycenter-1, xm, ym );
        g.drawLine( xcenter-1, ycenter, xm, ym );
        g.drawLine( xcenter, ycenter-1, xh, yh );
        g.drawLine( xcenter-1, ycenter, xh, yh );

    }

}
-----------------------------------------------------------------
Here's the .class file that I have:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.JTextComponent;

public class ClockApp extends JApplet
    implements ItemListener, ActionListener, Runnable
{

    public ClockApp()
    {
    }

    public void init()
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Throwable throwable) { }
        getContentPane().setLayout(new BorderLayout());
        northPanel = new JPanel(new FlowLayout());
        timeLabel = new JLabel("Time:");
        timeLabel.setFont(new Font("Dialog", 1, 12));
        timeLabel.setName("timeLabel");
        northPanel.add(timeLabel);
        timeTextField = new JTextField(10);
        timeTextField.setEditable(false);
        timeTextField.setFont(new Font("Dialog", 1, 12));
        northPanel.add(timeTextField);
        getContentPane().add(northPanel, "North");
        clockPanel = new ClockPanel();
        getContentPane().add(clockPanel, "Center");
        southPanel = new JPanel(new GridLayout(2, 2));
        locationLabel = new JLabel("Location", 0);
        locationLabel.setName("locationLabel");
        locationLabel.setFont(new Font("Dialog", 1, 12));
        southPanel.add(locationLabel);
        formatLabel = new JLabel("Format", 0);
        formatLabel.setFont(new Font("Dialog", 1, 12));
        formatLabel.setName("formatLabel");
        southPanel.add(formatLabel);
        southwestPanel = new JPanel(new FlowLayout());
        locationComboBox = new JComboBox();
        locationComboBox.setToolTipText("Select a City");
        locationComboBox.setFont(new Font("Dialog", 0, 12));
        locationComboBox.addItem("Chicago");
        locationComboBox.addItem("Dallas");
        locationComboBox.addItem("San Francisco");
        locationComboBox.addItem("New York");
        locationComboBox.addItemListener(this);
        southwestPanel.add(locationComboBox);
        southPanel.add(southwestPanel);
        bcg = new ButtonGroup();
        buttonPanel = new JPanel(new FlowLayout());
        hour12RadioButton = new JRadioButton("12 Hour");
        hour12RadioButton.setFont(new Font("Dialog", 1, 12));
        hour12RadioButton.addActionListener(this);
        bcg.add(hour12RadioButton);
        buttonPanel.add(hour12RadioButton);
        hour24RadioButton = new JRadioButton("24 Hour");
        hour24RadioButton.setSelected(true);
        hour24RadioButton.setFont(new Font("Dialog", 1, 12));
        hour24RadioButton.addActionListener(this);
        bcg.add(hour24RadioButton);
        buttonPanel.add(hour24RadioButton);
        southPanel.add(buttonPanel);
        getContentPane().add(southPanel, "South");
        currentTimeZone = TimeZone.getTimeZone("CST");
        hourFormat = 24;
    }

    public void itemStateChanged(ItemEvent itemevent)
    {
        if(locationComboBox.getSelectedItem().equals("Chicago") || locationComboBox.getSelectedItem().equals("Dallas"))
        {
            currentTimeZone = TimeZone.getTimeZone("CST");
            runner = null;
            start();
        } else
        if(locationComboBox.getSelectedItem().equals("New York"))
        {
            currentTimeZone = TimeZone.getTimeZone("EST");
            runner = null;
            start();
        } else
        if(locationComboBox.getSelectedItem().equals("San Francisco"))
        {
            currentTimeZone = TimeZone.getTimeZone("PST");
            runner = null;
            start();
        }
    }

    public void actionPerformed(ActionEvent actionevent)
    {
        if(actionevent.getSource() == hour12RadioButton)
        {
            hourFormat = 12;
            runner = null;
            start();
        } else
        {
            hourFormat = 24;
            runner = null;
            start();
        }
    }

    public void run()
    {
        while(runner != null)
        {
            clockCalendar = new GregorianCalendar(currentTimeZone);
            if(hourFormat == 24)
                currentHour = clockCalendar.get(11);
            else
                currentHour = clockCalendar.get(10);
            if(currentHour == 0 && hourFormat == 12)
                currentHour = 12;
            currentMinute = clockCalendar.get(12);
            currentSecond = clockCalendar.get(13);
            String s1;
            if(currentMinute < 10)
                s1 = "0";
            else
                s1 = "";
            String s;
            if(currentSecond < 10)
                s = "0";
            else
                s = "";
            if(hourFormat == 12)
            {
                if(clockCalendar.get(9) == 0)
                    timeTextField.setText(" " + currentHour + ":" + s1 + currentMinute + ":" + s + currentSecond + "   A.M.");
                else
                    timeTextField.setText(" " + currentHour + ":" + s1 + currentMinute + ":" + s + currentSecond + "   P.M.");
            } else
            {
                timeTextField.setText(" " + currentHour + ":" + s1 + currentMinute + ":" + s + currentSecond);
            }
            if(hourFormat == 24)
                clockPanel.setTime(clockCalendar.get(10), currentMinute, currentSecond);
            else
                clockPanel.setTime(currentHour, currentMinute, currentSecond);
            repaint();
            try
            {
                Thread.sleep(1000L);
            }
            catch(InterruptedException interruptedexception) { }
        }
    }

    public void start()
    {
        if(runner == null)
            runner = new Thread(this);
        runner.start();
    }

    public void stop()
    {
        runner = null;
    }

    public static void main(String args[])
    {
        JFrame jframe = new JFrame("Clock Applet as Application");
        jframe.setDefaultCloseOperation(3);
        ClockApp clockapp = new ClockApp();
        clockapp.init();
        clockapp.start();
        jframe.getContentPane().add(clockapp);
        jframe.setSize(400, 300);
        jframe.setVisible(true);
    }

    private JPanel northPanel;
    private JPanel southPanel;
    private JPanel buttonPanel;
    private JPanel southwestPanel;
    private ClockPanel clockPanel;
    private JLabel timeLabel;
    private JLabel locationLabel;
    private JLabel formatLabel;
    private JTextField timeTextField;
    private JRadioButton hour12RadioButton;
    private JRadioButton hour24RadioButton;
    private JComboBox locationComboBox;
    private ButtonGroup bcg;
    private TimeZone currentTimeZone;
    private int currentHour;
    private int currentMinute;
    private int currentSecond;
    private Thread runner;
    private GregorianCalendar clockCalendar;
    private int hourFormat;
}


0
Comment
Question by:adidas7fire
[X]
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
  • 6
  • 5
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12282924
whats the error?
0
 

Author Comment

by:adidas7fire
ID: 12283017
"exception in thread "main" java.lang.NoSuchMethodError: main"
0
 
LVL 92

Expert Comment

by:objects
ID: 12283050
what command are you running it with?
should be:

java ClockApp
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:adidas7fire
ID: 12283063
that one works for the applet portion but not for the application part that i need to show. :-(

0
 
LVL 92

Expert Comment

by:objects
ID: 12283085
not sure i follow you, what do you run when you get that error?
0
 

Author Comment

by:adidas7fire
ID: 12283089
i am supposed to be able to make this file work as an applet and also as an application.  when i run the ClockPanel.java file, i get that message.  the applet works just fine.
0
 
LVL 92

Expert Comment

by:objects
ID: 12283101
Your running the wrong class, you should be running ClockApp (for both application and applet).
0
 

Author Comment

by:adidas7fire
ID: 12283107
but for instance, when i compile the ClockPanel.java file, if it compiles, it's supposed to name a ClockPanel.class, right?  
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 12283116
yes, but that is just a class. Not all class can be run.
For a class to be run (as an application) it needs a main() method.
0
 

Author Comment

by:adidas7fire
ID: 12283151
so on that first set of code i submitted... would i just insert that at the end?  something like,

public static void main( ClockPanel args[] )
{

   ClockPanel application = new ClockPanel();

   application.setDefaultCloseOperation(
     JFrame.EXIT_ON_CLOSE );
}

is that what I am needing to put in the code in order to make it work as an application?
0
 
LVL 92

Expert Comment

by:objects
ID: 12283215
no you need to run the other class, the main has already been added to it.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 58
ejb example issues 3 53
Which non-HTML GUI front end to use with Java? 3 52
Using jdbcTemplate.batchUpdate to improve INSERT performance 6 27
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

749 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