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;
}
For a class to be run (as an application) it needs a main() method.