Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Clock is not repainted properly

  1. #1
    skiabox is offline Member
    Join Date
    Jul 2011
    Posts
    17
    Rep Power
    0

    Default Clock is not repainted properly

    I am reading 'Introduction to Java Programming' from Y.Daniel Liang.

    There is a clock implementation in one of the chapters, but when the line of the seconds moves, the old line is not erased.
    Here is the code:

    StillClock.java
    Java Code:
    package clockanimationproject;
    
    /**
     *
     * @author skiabox
     */
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    
    public class StillClock extends JPanel{
        
        private int hour;
        private int minute;
        private int second;
        
        //---constructors
        //Cosntruct a default clock with the current time
        public StillClock()
        {
            setCurrentTime();
        }
        
        //Construct a clock with specified hour, minute, and second
        public StillClock(int hour, int minute, int second)
        {
            this.hour = hour;
            this.minute = minute;
            this.second = second;
        }
        
        //---methods
        //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();
        }
        
        //**Draw the clock
        @Override
        protected void paintComponent(Graphics g)
        {
            int clockRadius = (int)(Math.min(getWidth(), getHeight()) * 0.8 * 0.5);
            int xCenter = getWidth() / 2;
            int yCenter = getHeight() / 2;
            
            //Draw circle
            g.setColor(Color.BLACK);
            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 second hand
            int sLength = (int)(clockRadius * 0.8);
            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
            int mLength = (int)(clockRadius * 0.65);
            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
            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.GREEN);
            g.drawLine(xCenter, yCenter, xHour, yHour);
        }
        
        public void setCurrentTime()
        {
            //Construct a calendar for the current date and time
            Calendar calendar = new GregorianCalendar();
            
            //Set current hour, minute and second
            this.hour = calendar.get(Calendar.HOUR_OF_DAY);
            this.minute = calendar.get(Calendar.MINUTE);
            this.second = calendar.get(Calendar.SECOND);
        }
        
        @Override
        public Dimension getPreferredSize()
        {
            return new Dimension(200, 200);
        }
        
    }

    ClockAnimation.java
    Java Code:
    package clockanimationproject;
    
    /**
     *
     * @author skiabox
     */
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ClockAnimation extends JFrame{
    
        private StillClock clock = new StillClock();
        
        //---default constructor
        public ClockAnimation()
        {
            add(clock);
            
            //Create a timer with delay 1000 ms
            Timer timer = new Timer(1000, new TimerListener());
            timer.start();
        }
        
        private class TimerListener implements ActionListener
        {
            //Handle the action event
            public void actionPerformed(ActionEvent e)
            {
                //Set new time and repaint teh clock to display current time
                clock.setCurrentTime();
                clock.repaint();
            }
        }
        
        
        public static void main(String[] args) {
            // TODO code application logic here
            JFrame frame = new ClockAnimation();
            frame.setTitle("ClockAnimation");
            frame.setSize(200, 200);
            frame.setLocationRelativeTo(null);  //Center the frame
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }


    Any ideas on what is going wrong here?
    Thank you.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Clock is not repainted properly

    Your paintComponent method needs to call the super's paintComponent method, usually on the first line of the method, in order to allow the JPanel to repaint its background.
    skiabox likes this.

  3. #3
    skiabox is offline Member
    Join Date
    Jul 2011
    Posts
    17
    Rep Power
    0

    Default Re: Clock is not repainted properly

    Thank you very much for your reply.It is working now, after the fix you proposed!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. 24 hour clock to 12 hour clock project.
    By bs3ac in forum New To Java
    Replies: 4
    Last Post: 01-08-2013, 11:10 AM
  2. help with clock
    By Bimz in forum New To Java
    Replies: 1
    Last Post: 09-26-2011, 02:50 PM
  3. clock
    By ws6driver in forum New To Java
    Replies: 1
    Last Post: 07-31-2009, 05:15 AM
  4. java clock
    By bugmenot in forum JCreator
    Replies: 0
    Last Post: 04-07-2009, 11:25 PM
  5. Desktop Clock
    By olamide in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-19-2009, 06:28 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
  •