Jump to content

Java Clock Help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
nic m 123

nic m 123

    Newbie

  • Members
  • Pip
  • 3 posts
hey guys,
I was tryin to create a program to demonstate Threads and i found this example for a clock in my school textbook. I ve copied i out exactly the same but I keep gettin an error, it says it expects a "{" at line 26.

I'm not sure how to paste the code.. so here it is I guess....

Quote

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

class Clock extends JFrame implements Runnable
{
Thread runner;
Font clockFont;

public Clock()
{
super("Java Clock"); setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

clockFont= new Font("Serif", Font.BOLD, 40);

Container contentSrea = getContentPane();
ClockPanel timeDisplay = new ClockPanel();
contentArea.add(timeDisplay);
setContentPane(contentArea);

start();
}

class ClockPanelextends JPanel (<<line 26) {
public void paintComponent(Graphics painter)
{
painter.setColor(Color.white);
painter.fillRect(0,0,300,100);
painter.setColor(Color.black);
painter.drawSring( timeNow(), 60, 40);
}
}

public String timeNow()
{
Calender now = Calender.getInstance();
int hrs = now.get(Calender.HOUR_OF_DAY);
int min = now.get(Calender.MINUTE);
int sec = now.get(Calender.SECOND);
String time = zero(hrs)+":"+zero(min)+":"+zero(sec);
return time;
}

public String zero(int num)
{
String number=( num<10 ) ? ("0"+num) : (""+num);
return number;
}

public void start()
{
if(runer == null) runner = new Thread(this);
runner.start();
}

public void run()
{
while (runner == Thread.currentThread() )
{
repaint();
try { Thread.sleep(1000); }
catch(InterruptedException e) {}
}

}

public static void main(String[] args)
{
Clock eg = new Clock();
}
}

P.S. can you reply as sson as possible want to use in assignment due this week. Thanks

#2
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
That error indicates that you are missing a brace bracket. I don't think you are though. Everything matches up as far as I can tell without copying it into my IDE.

Try this:

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

class Clock extends JFrame implements Runnable
{
    Thread runner;
    Font clockFont;

    public Clock()
    {
        super("Java Clock"); setSize(300, 100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        clockFont= new Font("Serif", Font.BOLD, 40);

        Container contentSrea = getContentPane();
        ClockPanel timeDisplay = new ClockPanel();
        contentArea.add(timeDisplay);
        setContentPane(contentArea);

        start();
    }

    class ClockPanel extends JPanel {
        public void paintComponent(Graphics painter)
        {
            painter.setColor(Color.white);
            painter.fillRect(0,0,300,100);
            painter.setColor(Color.black);
            painter.drawSring( timeNow(), 60, 40);
        }
    }

    public String timeNow()
    {
        Calender now = Calender.getInstance();
        int hrs = now.get(Calender.HOUR_OF_DAY);
        int min = now.get(Calender.MINUTE);
        int sec = now.get(Calender.SECOND);
        String time = zero(hrs)+":"+zero(min)+":"+zero(sec);
        return time;
    }

    public String zero(int num)
    {
        String number=( num<10 ) ? ("0"+num) : (""+num);
        return number;
    }

    public void start()
    {
        if(runer == null) runner = new Thread(this);
        runner.start();
    }

    public void run()
    {
        while (runner == Thread.currentThread() )
        {
            repaint();
            try { Thread.sleep(1000); }
            catch(InterruptedException e) {}
        }

    }

    public static void main(String[] args)
    {
        Clock eg = new Clock();
    }
}


#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
This works with me
package net.sourceforge.algomusic;

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

class Clock extends JFrame implements Runnable {
    Thread runner;
    Font clockFont;

    public Clock() {
        super("Java Clock");
        setSize(300, 100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        clockFont = new Font("Serif", Font.BOLD, 40);

        Container contentArea = getContentPane();
        ClockPanel timeDisplay = new ClockPanel();
        contentArea.add(timeDisplay);
        setContentPane(contentArea);

        start();
    }

    class ClockPanel extends JPanel {
        public void paintComponent(Graphics painter) {
            painter.setColor(Color.white);
            painter.fillRect(0, 0, 300, 100);
            painter.setColor(Color.black);
            painter.drawString(timeNow(), 60, 40);
        }
    }

    public String timeNow() {
        Calendar now = Calendar.getInstance();
        int hrs = now.get(Calendar.HOUR);
        int min = now.get(Calendar.MINUTE);
        int sec = now.get(Calendar.SECOND);
        String time = zero(hrs) + ":" + zero(min) + ":" + zero(sec);
        return time;
    }

    public String zero(int num) {
        String number = (num < 10) ? ("0" + num) : ("" + num);
        return number;
    }

    public void start() {
        if (runner == null) runner = new Thread(this);
        runner.start();
    }

    public void run() {
        while (runner == Thread.currentThread()) {
            repaint();
            try {
                Thread.sleep(1000);
            }
            catch (InterruptedException e) {
            }
        }

    }

    public static void main(String[] args) {
        Clock eg = new Clock();
    }
}

It is simply your code without the typos :p
like Calender instead of Calendar
runer instead of runner
contentSrea instead of contentArea
...

#4
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
You're error was just a couple typos and you forgot to put a space before extends on your inner class.

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

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

class Clock extends JFrame implements Runnable {

    Thread runner;
    Font clockFont;

    public Clock() {
        super("Java Clock");
        setSize(300, 100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        clockFont = new Font("Serif", Font.BOLD, 40);

        Container contentArea = getContentPane();
        ClockPanel timeDisplay = new ClockPanel();
        contentArea.add(timeDisplay);
        setContentPane(contentArea);

        start();
    }

    class ClockPanel extends JPanel  {
            
            public void paintComponent(Graphics painter) {
painter.setColor(Color.white);
painter.fillRect(0, 0, 300, 100);
            painter.setColor(Color.black);
            painter.drawSring(timeNow(), 60, 40);
        }
    }

    public String timeNow() {
        Calendar now = Calendar.getInstance();
        int hrs = now.get(Calendar.HOUR_OF_DAY);
        int min = now.get(Calendar.MINUTE);
        int sec = now.get(Calendar.SECOND);
        String time = zero(hrs) + ":" + zero(min) + ":" + zero(sec);
        return time;
    }

    public String zero(int num) {
        String number = (num < 10) ? ("0" + num) : ("" + num);
        return number;
    }

    public void start() {
        if (runner == null) {
            runner = new Thread(this);
        }
        runner.start();
    }

    public void run() {
        while (runner == Thread.currentThread()) {
            repaint();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }

    }

    public static void main(String[] args) {
        Clock eg = new Clock();
    }
}


#5
nic m 123

nic m 123

    Newbie

  • Members
  • Pip
  • 3 posts
Props man works now, kinda weird tho.

Thanks chile5 too I didnt use your code but I appreciate the help :)