Hello

I'm new learner in java, and I'm facing a problem to develop a Java application that will ask the user for the time in terms of hour and minutes and display the time by drawing a clock.
I have created two classes, and the code is below.

Please could you figure out my problem in the code.

this is the main class

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lab4;

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

/**
 *
 * @author Alsobhi
 */


    public class ClockDisplays extends JFrame 
    {

        public class ClockDisplay
        {
        // TO creat a frame that the user can enters Hour and minutes
        /**
     *
     * 
     */      

        SimpleClock clock = new SimpleClock();
      MessagePanel mp = new MessagePanel(clock.getHour()+":"+clock.getMinute()+":"+clock.getSecond());  
   //  add(clock);
   //  add(mp,BorderLayout.South);

        }


    public static void main (String[] args)
        {
     ClockDisplays frame = new ClockDisplays();


        frame.setTitle("DisplayClock");
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null); // center the frame

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);
        String stringHours;
        // The user enters the hour and minutes
        stringHours = JOptionPane.showInputDialog("Enter the hour:");
        int hour = Integer.parseInt(stringHours);
        String StringMinute = JOptionPane.showInputDialog ("Enter the minute");
        int minute = Integer.parseInt(StringMinute);

         SimpleClock sc = new SimpleClock();

        sc.setHour(hour);
        sc.setMinute(minute);



        }

    }

this is the other class

        /*
         * To change this template, choose Tools | Templates
         * and open the template in the editor.
         */
        package lab4;

        import java.awt.Color;
        import java.awt.Dimension;
        import java.awt.Graphics;
        import javax.swing.JPanel;

        /**
         *
         * @author TORKI ALSOBHI
         */
        public class SimpleClock extends JPanel {


            public SimpleClock(int hour, int minute) {
                this.hour = hour;
                this.minute = minute;
            }
         public SimpleClock(){}

            public int getSecond() {
                return second;
            }

            public void setSecond(int second) {
                this.second = second;
            }



          //  System.out.println("Message to slow things down");

           public int second;  
           public int hour;  // Initilaze Variable Hour as integer type Public which read every class 
           public int minute; // Initilaze Variable minute as integer type Public which read every class
            // press right Click and Choice insert code once the diallog apper choice Getter & Setter Methode 
            // The next part will appear a box where you must click correct box both Getter & Setter 
           // the Getter And Setter Methods Generate in the Class .. 

           Graphics graphic ; 



            public void setHour(int hour)
            {
                this.hour = hour;
            }
          // This Method will set the Value Hour as interger where you can summon in the main class and return nothing. 
            public int getHour() 
            {
                return hour;
            }
          // This Method will return the value hour as integer where there is no parameter to send value  

            public void setMinute(int minute)
            {
                this.minute = minute;
            }

            public int getMinute() 
            {
                return minute;
            }






            // Draw A clock


        @Override
        protected void paintComponent (Graphics g)
        {
            super.paintComponent(g);

            // initalize clock parameters
            int clockRadius = (int)(Math.min(getWidth(),getHeight()) * 0.8 * 0.5);
            int xCenter = getWidth () / 2;
            int yCenter = getHeight() / 2;

            // Draw circle
            g.setColor(Color.blue);
            g.drawOval(xCenter - clockRadius, yCenter - clockRadius, 2 * clockRadius, 2 *clockRadius);
            g.drawString("12", xCenter - 5, yCenter - clockRadius + 12);
            g.drawString("9", xCenter - clockRadius +3, yCenter + 5);
             g.drawString("3", xCenter + clockRadius - 10, yCenter + 3);
             g.drawString("6", xCenter - 3, yCenter + clockRadius - 3);

             // Draw minute hand
             int mLength = (int) (clockRadius * 0.65);
             int xMinute = (int) (xCenter + mLength * 
              Math.sin(minute * (2* Math.PI / 60)));
            int yMinute = (int) (xCenter - mLength * 
              Math.cos(minute * (2* Math.PI / 60)));
            g.setColor(Color.white);
            g.drawLine(xCenter, yCenter, xMinute, yMinute);


            // Draw hour hand
            int hLength = (int) (clockRadius * 0.5);
            int xHour = (int) (xCenter + hLength*
                    Math.sin(hour % 12 + minute /60.0) * (2 * Math.PI / 12));
            int yHour = (int) (yCenter - hLength*
                    Math.cos(hour % 12 + minute /60.0) * (2 * Math.PI / 12));
            g.setColor(Color.yellow);
            g.drawLine(xCenter, yCenter, xHour, yHour);


        }
        //Override
        @Override
        public Dimension getPreferredSize()
        {
            return new Dimension (200,200);
        }
    }

figure out my problem in the code.

Please explain what the problem is. If you are getting errors, copy the full text of the messages and pasted them here.

There is no errors that I have.
My code is not working to show the clock.

where is the code that is supposed to show the clock?
Where is object that contains that code added to a window container so it can be seen?

If the object that draws the clock is not added to a visible container then it won't be seen.

this is the code to show the clock

    // Draw A clock

    @Override
    protected void paintComponent (Graphics g)
    {
        super.paintComponent(g);

        // initalize clock parameters
        int clockRadius = (int)(Math.min(getWidth(),getHeight()) * 0.8 * 0.5);
        int xCenter = getWidth () / 2;
        int yCenter = getHeight() / 2;

        // Draw circle
        g.setColor(Color.blue);
        g.drawOval(xCenter - clockRadius, yCenter - clockRadius, 2 * clockRadius, 2 *clockRadius);
        g.drawString("12", xCenter - 5, yCenter - clockRadius + 12);
        g.drawString("9", xCenter - clockRadius +3, yCenter + 5);
         g.drawString("3", xCenter + clockRadius - 10, yCenter + 3);
         g.drawString("6", xCenter - 3, yCenter + clockRadius - 3);

         // Draw minute hand
         int mLength = (int) (clockRadius * 0.65);
         int xMinute = (int) (xCenter + mLength * 
          Math.sin(minute * (2* Math.PI / 60)));
        int yMinute = (int) (xCenter - mLength * 
          Math.cos(minute * (2* Math.PI / 60)));
        g.setColor(Color.white);
        g.drawLine(xCenter, yCenter, xMinute, yMinute);


        // Draw hour hand
        int hLength = (int) (clockRadius * 0.5);
        int xHour = (int) (xCenter + hLength*
                Math.sin(hour % 12 + minute /60.0) * (2 * Math.PI / 12));
        int yHour = (int) (yCenter - hLength*
                Math.cos(hour % 12 + minute /60.0) * (2 * Math.PI / 12));
        g.setColor(Color.yellow);
        g.drawLine(xCenter, yCenter, xHour, yHour);


    }
    //Override
    @Override
    public Dimension getPreferredSize()
    {
        return new Dimension (200,200);
    }

object

Where is object that contains that code added to a window container so it can be seen? If the object that draws the clock is not added to a visible container then it won't be seen.

In fact, I don't know how can I add this object.

Could you explain more, please ?

Create an instance of the class that contains the code you just posted and use the add() method to add it to the JFrame class instance.

the add function cannot find in the same class please declare the add function.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lab4;

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

/**
 *
 * @author TORKI ALSOBHI
 */


    public class ClockDisplays extends JFrame 
    {

        public class ClockDisplay
        {
        // TO creat a frame that the user can enters Hour and minutes
        /**
     *
     * 
     */      

        SimpleClock clock = new SimpleClock();
      MessagePanel mp = new MessagePanel(clock.getHour()+":"+clock.getMinute()+":"+clock.getSecond());  
   //  add(clock);   Note: the add function cannot find in the same class please declare the add function.
   //  add(mp,BorderLayout.South);

        }


    public static void main (String[] args)
        {

     ClockDisplays frame = new ClockDisplays();


        frame.setTitle("DisplayClock");
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null); // center the frame

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);
        String stringHours;
        // The user enters the hour and minutes
        stringHours = JOptionPane.showInputDialog("Enter the hour:");
        int hour = Integer.parseInt(stringHours);
        String StringMinute = JOptionPane.showInputDialog ("Enter the minute");
        int minute = Integer.parseInt(StringMinute);

         SimpleClock sc = new SimpleClock();

        sc.setHour(hour);
        sc.setMinute(minute);






        }

    }

What is the class: ClockDisplay (defined on line 19) for? Why have you defined that inner class?
Where is that class used? It does not have an add() method.

What class does have an add() method? Move the code to that class.

Why are there two instances of the SimpleClock class created? One is added to the GUI to be shown and the other is given the hours and minutes from the user, but it never added anywhere so it will never be seen.

the clockdisplay is used for as main class which define the Frame and implelemnt object from another class which is called SimpleClock,However; I am facing a problem in displaying the clock in the same main class so please define what kind a problem am facing in the program.

Read the code again. ClockDisplay is an inner class in the ClockDisplays class.
I think you need to read this: http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

One of the problems you are having is this: there two instances of the SimpleClock class created. One is added to the GUI to be shown and the other is given the hours and minutes from the user, but it never added anywhere so it will never be seen.

I'm done for today. Back tomorrow.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.