Results 1 to 4 of 4
  1. #1
    NoNickName is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default What am I doing wrong??

    Below is the following code to create a detailed clock. I am getting a minor error. Any help is great, thanks!
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Exercise14_20 extends JFrame {
      public static void main(String[] args) {
        // Obtain the total seconds since the midnight, Jan 1, 1970
        long totalSeconds = System.currentTimeMillis() / 1000;
    
        // Compute the current second in the minute in the hour
        int currentSecond = (int)(totalSeconds % 60);
    
        // Obtain the total minutes
        long totalMinutes = totalSeconds / 60;
    
        // Compute the current minute in the hour
        int currentMinute = (int)(totalMinutes % 60);
    
        // Obtain the total hours
        long totalHours = totalMinutes / 60;
    
        // Compute the current hour
        int currentHour = (int)(totalHours % 24);
    
        // Create a frame to hold the clock
        Exercise14_20 frame = new Exercise14_20();
        frame.setTitle("Exercise14_20");
        frame.getContentPane().add(new DetailedClock
                                   (currentHour, currentMinute, currentSecond));
        MessagePanel messagePanel = new MessagePanel(
          currentHour + ":" + currentMinute + ":" + currentSecond + " GMT");
        messagePanel.setCentered(true);
        messagePanel.setForeground(Color.blue);
        frame.getContentPane().add(messagePanel, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 350);
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setVisible(true);
      }
    }
    
    class DetailedClock extends JPanel {
      private int hour;
      private int minute;
      private int second;
      protected int xCenter, yCenter;
      protected int clockRadius;
    
      public DetailedClock() {
      }
    
      // Construct a clock panel
      public DetailedClock(int hour, int minute, int second) {
        this.hour = hour;
        this.minute = minute;
        this.second = second;
      }
    
      // Draw the clock
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);
    
        // Initialize clock parameters
        clockRadius =
          (int)(Math.min(getSize().width, getSize().height) * 0.9 * 0.5);
        xCenter = (getSize().width) / 2;
        yCenter = (getSize().height) / 2;
    
        // Draw circle
        g.setColor(Color.black);
        g.drawOval(xCenter - clockRadius, yCenter - clockRadius,
                   2 * clockRadius, 2 * clockRadius);
    
        // Draw second hand
        if (secondHandVisible) {
          int sLength = (int)(clockRadius * 0.7);
          int xSecond =
            (int)(xCenter + sLength * Math.sin(second * (2 * Math.PI / 60)));
          int ySecond =
            (int)(yCenter - sLength * Math.cos(second * (2 * Math.PI / 60)));
          g.setColor(Color.red);
          g.drawLine(xCenter, yCenter, xSecond, ySecond);
        }
    
        // Draw minute hand
        if (minuteHandVisible) {
          int mLength = (int)(clockRadius * 0.6);
          int xMinute =
            (int)(xCenter + mLength * Math.sin(minute * (2 * Math.PI / 60)));
          int yMinute =
            (int)(yCenter - mLength * Math.cos(minute * (2 * Math.PI / 60)));
          g.setColor(Color.blue);
          g.drawLine(xCenter, yCenter, xMinute, yMinute);
        }
        // Draw hour hand
        if (hourHandVisible) {
          int hLength = (int)(clockRadius * 0.5);
          int xHour = (int)(xCenter +
                            hLength *
                            Math.sin((hour + minute / 60.0) * (2 * Math.PI / 12)));
          int yHour = (int)(yCenter -
                            hLength *
                            Math.cos((hour + minute / 60.0) * (2 * Math.PI / 12)));
          g.setColor(Color.black);
          g.drawLine(xCenter, yCenter, xHour, yHour);
        }
        // Display more details on the clock
        for (int i = 0; i < 60; i++) {
          double percent;
    
          if (i % 5 == 0) {
            percent = 0.9;
          }
          else {
            percent = 0.95;
          }
    
          int xOuter = (int)(xCenter +
                             clockRadius * Math.sin(i * (2 * Math.PI / 60)));
          int yOuter = (int)(yCenter -
                             clockRadius * Math.cos(i * (2 * Math.PI / 60)));
          int xInner = (int)(xCenter +
                             percent * clockRadius * Math.sin(i * (2 * Math.PI / 60)));
          int yInner = (int)(yCenter -
                             percent * clockRadius * Math.cos(i * (2 * Math.PI / 60)));
    
          g.drawLine(xOuter, yOuter, xInner, yInner);
        }
    
        // Display hours on the clock
        g.setColor(Color.black);
        for (int i = 0; i < 12; i++) {
          int x = (int)(xCenter +
                        0.8 * clockRadius * Math.sin(i * (2 * Math.PI / 12)));
          int y = (int)(yCenter -
                        0.8 * clockRadius * Math.cos(i * (2 * Math.PI / 12)));
    
          g.drawString("" + ((i == 0) ? 12 : i), x, y);
        }
      }
    
      /** Return hour */
      public int getHour() {
        return hour;
      }
    
      /** Set a new hour */
      public void setHour(int hour) {
        this.hour = hour;
        repaint();
      }
    
      /** Return minute */
      public int getMinute() {
        return minute;
      }
    
      /** Set a new minute */
      public void setMinute(int minute) {
        this.minute = minute;
        repaint();
      }
    
      /** Return second */
      public int getSecond() {
        return second;
      }
    
      /** Set a new second */
      public void setSecond(int second) {
        this.second = second;
        repaint();
      }
    
      private boolean hourHandVisible = true;
      private boolean minuteHandVisible = true;
      private boolean secondHandVisible = true;
    
      public boolean isHourHandVisible() {
        return hourHandVisible;
      }
    
      public boolean isMinuteHandVisible() {
        return hourHandVisible;
      }
    
      public boolean isSecondHandVisible() {
        return secondHandVisible;
      }
    
      public void setHourHandVisible(boolean hourHandVisible) {
        this.hourHandVisible = hourHandVisible;
        repaint();
      }
    
      public void setMinuteHandVisible(boolean minuteHandVisible) {
        this.minuteHandVisible = minuteHandVisible;
        repaint();
      }
    
      public void setSecondHandVisible(boolean secondHandVisible) {
        this.secondHandVisible = secondHandVisible;
        repaint();
      }
    
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    > I am getting a minor error.

    It might be a good idea to elaborate on this.

    Compiler message or runtime error? Is there a message - if so, what is it? If the message refers to some line of your code - which line? Is the behaviour of the application not what you expect or intend - if so what do you expect or intend and what does happen? Does the unwanted behaviour happen always or sometimes?...

  3. #3
    NoNickName is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    These are the two errors...Sorry for bieng so brief but Im sort of new to this:confused:


    Exercise14_20.java:30: cannot find symbol
    symbol : class MessagePanel
    location: class Exercise14_20
    MessagePanel messagePanel = new MessagePanel(
    ^
    Exercise14_20.java:30: cannot find symbol
    symbol : class MessagePanel
    location: class Exercise14_20
    MessagePanel messagePanel = new MessagePanel(
    ^
    2 errors

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    You have no MessagePanel class! Or at any rate you didn't post one.

    As a little experiment try changing a few lines like this:

    Java Code:
    JLabel messagePanel = new JLabel(
            currentHour + ":" + currentMinute + ":" + currentSecond + " GMT");
    //MessagePanel messagePanel = new MessagePanel(
    //	      currentHour + ":" + currentMinute + ":" + currentSecond + GMT");
    //messagePanel.setCentered(true);
    messagePanel.setForeground(Color.blue);
    frame.getContentPane().add(messagePanel, BorderLayout.SOUTH);
    
    // etc
    The idea here is to replace the MessagePanel with a humble JLabel. (By substituting a different constructor and removing the centering method.) With those changes it compiles and runs fine.

    What you need to do is to write a MessagePanel class. For that you should consult whatever information came with the exercise. (Either to see if such a class was supplied, or what it should do if you have to actually write it yourself.)

Similar Threads

  1. what wrong
    By pro85 in forum New To Java
    Replies: 3
    Last Post: 02-09-2009, 02:07 PM
  2. what's wrong in here!!!
    By Annatar in forum New To Java
    Replies: 8
    Last Post: 11-14-2008, 03:55 AM
  3. So, what am I doing wrong?
    By Charles_Smith in forum New To Java
    Replies: 0
    Last Post: 10-29-2008, 03:50 PM
  4. right or wrong
    By jot321 in forum New To Java
    Replies: 7
    Last Post: 09-25-2008, 12:45 PM
  5. Can someone tell me what I did wrong??
    By booter4429 in forum New To Java
    Replies: 7
    Last Post: 08-13-2008, 09:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •