We help IT Professionals succeed at work.

Code debug...

Mike Eghtebas
Mike Eghtebas asked
on
The attached code is to display three clocks. It the following bugs in it:

line 44:    clockControl1.resume();
line 51:    clockControl1.suspend();
line 59:    clock.resume();
line 62:    clock.suspend();

How can I correct them?

This code is from a tutorial video. I don't know how I have managed to mess it up. Yes, I have gone over it a couple of times.

Thank you.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;

public class Exercize18_14 extends JApplet {
    
    private ClockControl clockControl1, clockControl2, clockControl3;
    
    private JButton jbtResumeAll, jbtSuspendAll;
    
    public static void main(String[] args) {
        JFrame frame = new JFrame("Exercise 18.14");
        
        Exercize18_14 applet = new Exercize18_14();
        
        frame.add(applet, BorderLayout.CENTER);
        applet.init();
        applet.start();
        
    }
    public void init() {
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(1,3));
        
        p1.add(clockControl1 = new ClockControl());
        p1.add(clockControl2 = new ClockControl());
        p1.add(clockControl3 = new ClockControl());
    
        JPanel p2 = new JPanel();
        p2.setLayout(new FlowLayout());
        p2.add(jbtResumeAll = new JButton("Resume All"));
        p2.add(jbtSuspendAll = new JButton("Suspend All"));
    
        setLayout (new BorderLayout());
        add(p1, BorderLayout.CENTER);
        add(p2, BorderLayout.SOUTH);
        
        jbtResumeAll.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent arg0) {
               clockControl1.resume();
               clockControl2.resume();
               clockControl3.resume();
            }        
        });
        
        jbtSuspendAll.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
               clockControl1.suspend();
               clockControl2.suspend();
               clockControl3.suspend();
            }        
        });
    }
        public void resume() {
            clock.resume();
        }
        
        public void suspend() {
            clcok.suspend();
        }        
}

    class ClockControl extends JPanel {
        private Clock clock = new Clock();
        
        private JButton jbtSuspend = new JButton("Suspend");
        private JButton jbtResume = new JButton("Resume");
        
        public ClockControl() {
            JPanel panel = new JPanel();
            panel.add(jbtSuspend);
            panel.add(jbtResume);
            
            setLayout(new BorderLayout());
            add(clock, BorderLayout.CENTER);
            add(panel, BorderLayout.SOUTH);
            
            jbtSuspend.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    clock.suspend();
                }
            });
            
            jbtResume.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    clock.resume();
                }
            });            
        }
    
}

    class Clock extends StillClock {
        private Timer timer = new Timer(1000, new Listener());

        public Clock() {
            timer.start();
        }

        class Listener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                setCurrentTime();
                repaint();
            }
        }

        public void suspend() {
            timer.stop();
        }

        public void resume() {
            timer.start();
        }
    }

Open in new window

Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:
Where is StillClock class ?
Mike EghtebasDatabase and Application Developer

Author

Commented:
sorry, here it is:
import java.awt.*;
import javax.swing.*;
import java.util.*;

public class StillClock extends JPanel {
    private int hour;
    private int minute;
    private int second;
    
    public StillClock() {
        setCurrentTime();
    }

    public StillClock(int hour, int minute, int second) {
        this.hour = hour;
        this.minute = minute;
        this.second = second;            
    }
    
    public int getHour() {
        return hour;
    }
    public void setHour(int hour) {
        this.hour = hour;
        repaint();
    }   
    
    public int getMinute() {
        return minute;
    }
    public void setMinute(int minute) {
        this.minute = minute;
        repaint();
    }
    
    public int getSecond() {
        return second;
    }
    public void setSecond(int hour) {
        this.second = second;
        repaint();
    } 
    
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
    
        int clockRadius = 
          (int)(Math.min(getWidth(), getHeight()) * 0.8 * 0.8);
        
        int xCenter = getWidth() / 2;
        int yCenter = getHeight() / 2;
        
        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 - 5, yCenter  + 5);
        g.drawString("3", xCenter - clockRadius - 10,yCenter - 5);
        g.drawString("6", xCenter - 5, yCenter - clockRadius - 3);

        int sLength = (int)(clockRadius - 0.9);
        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);
    }
    
    public void setCurrentTime() {
        Calendar calendar = new GregorianCalendar();
        
        this.hour = calendar.get(Calendar.HOUR_OF_DAY);
        this.minute = calendar.get(Calendar.MINUTE);
        this.second = calendar.get(Calendar.SECOND);
    }
    
    public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }
}

Open in new window

Awarded 2011
Awarded 2011

Commented:
well it does not compile - variable "clock" is not decared in ClockControl
  clock.resume(); - cannot work as clock is not declared

and methods suspend and résumé are not defined in ClockControl
 clockControl1.resume();
               clockControl2.resume();
               clockControl3.resume();


why is all that?



why d you always have main() in the JApplet derived class -
why to make it messy?

It is all clearly defined - main() is for application

init() for the applet

why to make simple things confused ?

Mike EghtebasDatabase and Application Developer

Author

Commented:
re:> why d you always have main() in the JApplet derived class -

In this case, the tutorial tape had it this way.

The example asked to run as an application.

Thank you for the debug. Tomorrow morning will try to apply the correction. It is a bit late here now.

Awarded 2011
Awarded 2011
Commented:
OK. Please, check these things about undeclared clock and methods and let us know.

 At least we know that it should be run as application.