trying to write a clock applet- tutorial, helpful links need help.
hi i've looked ffor quite sometime on the net now for some clock applet tutorial and have found nothing. can anyone in this forum point me out to some tutorials or something. right now i got a little digical clock that displays the time for example in the 12:00 :00 format. how would i go about changing the color of the font, the size or add some shading to it. dont i have to use the setfont method or something like that . any help with be apreciated. how can i make it display the time in normal time not military. any help at all or some helpful links will be very apreciated.
Use java.text.DateFormat and its subclasses (only SimpleDateFormat that I know of) to change the date format.Search on "java fonts" in google to find ways to play with the fonts.
My goodness - if you just type "java clock" into Google you'll get 10's of thousands of examples of Java clocks.You've got to be "funning" us about searching, right?
i did im just learning to play around with applets. i downloaded some clocks to look at their source code but they consist of like 50 +lines of code and i end up being more confused than i was before. my professor gave us this code and told us to modify it however we wanted. problem is that our TA didnt really explain what these lines of codes really accomplish i was looking over the documentation and coudlnt figure it out either. this is the code
[code]
import java.applet.Applet;
import java.awt.Graphics;
import javax.swing.Timer;
import java.awt.event.*;
import java.awt.Color;
import java.util.Date;
public class MyClock extends Applet implements ActionListener {
private Timer tm = null;
public void init() {
this.setBackground(Color.white);
tm = new Timer(1000, this);
}
public void start() {
tm.start();
}
public void actionPerformed(ActionEvent e) {
this.repaint();
}
public void paint(Graphics g) {
Date d = new Date();
g.drawString(d.toString(), 50, 25);
}
}[code]
if someone can at least explain to me what these things do i can at least play around with it until im happy with the results because right now i have no idea what im looking at and the TA really isnt much help./
http://java.sun.com/openstudio/applets/clock.html
thanks for that link michael i went over it yesterday and got more confused than help. the zip that i downloaded has like 20 files in it and the source code is like 50 lines long. :( what im looking to modify or for someone ot explain to me is the code i posted above i guess.
Go here and create and run a simple applet http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html#3and then read this introductory tutorial that teaches you about applets and has working
> my professor
> gave us this code and told us to modify it however we
> wanted. problem is that our TA didnt really explain
> what these lines of codes really accomplish i was
> looking over the documentation and coudlnt figure it
> out either.
Well, that's what the API docs are for.
// these are import statements. They tell the compiler how to interpret short class names like "Graphics"
> import java.applet.Applet;
> import java.awt.Graphics;
> import javax.swing.Timer;
> import java.awt.event.*;
> import java.awt.Color;
> import java.util.Date;
>
// Your class is called "MyClocK". It extends java.applet.Applet, which means that it's an Applet
// (it can be embedded in web pages)
// It implementes ActionListener, which means that it knows how to response to ActionEvents,
// which are produced by the GUI environment.
> public class MyClock extends Applet implements ActionListener {
// this creates a reference variable of type javax.swing.Timer (see above import statement)
> private Timer tm = null;
>
// this defines the init() method, which is a method in Applet. Browsers call this method
// when they start up an applet.
> public void init() {
// you know what these do, right? If not, look in the API docs for java.applet.Applet (for setBackground)
// and javax.swing.Timer. You know what "new" means, I hope; if not, go read the very basic
// tutorials.
>this.setBackground(Color.white);
>tm = new Timer(1000, this);
> }
>
// start() is another applet method. It's overridden here to start off the Timer when the applet is started.
> public void start() {
>tm.start();
> }
>
// You know how above it said that this class implements ActionListener? This is how it does it.
// All ActionListeners must implement this method.
// In this case, it responds to Action events by repainting the applet. I have no idea why it's doing
// that rather than just repainting every second, but there you go.
> public void actionPerformed(ActionEvent e) {
>this.repaint();
> }
// all java.awt.Components (and thus, java.applet.Applets, because java.applet.Applet subclasses
// some subclass of Component) have a paint method, which is called when you want to paint
// that component.
> public void paint(Graphics g) {
// it creates a date object, for the current time
>Date d = new Date();
// then it draws a string representation of that date
>g.drawString(d.toString(), 50, 25);
> }
> }
> ...and the TA really isnt much help./
Well, get used to that. When I was in school TAs generally sucked. So did the profs. In both cases they saw teaching as a painful burden that just got in the way of research and filling out grant applications.
hey paul thanks alot for explaining that to me. im kind of getting things now i was going over a couple of applet tutorials and im kind of getting the hang of it. i was trying to make the font type bold and size 34 and this is what i came up with but for some reason it wont start
[code]
import java.awt.*;
import java.applet.*;
import java.util.Date;
import javax.swing.Timer;
import java.awt.event.*;
public class MyClock extends Applet implements ActionListener
{
Font clockFont;//im intinializing the variable here
private Timer tm = null;
public void init() {
this.setBackground(Color.white);
clockFont = new Font("Arial",Font.BOLD,36);//implementing it .i must be doing something wrong
tm = new Timer(1000, this);
}
public void start()
{
tm.start();
}
public void actionPerformed(ActionEvent e) {
this.repaint();
}
public void paint(Graphics g) {
Date d = new Date();
g.drawString(d.toString(), 50, 25);
}
}[code]
Just instantiating the font object isn't enough. You also have to tell the java.awt.Graphics object to use it.
isnt that what im doing when i wrote clockFont = new Font("Arial",Font.BOLD,36);//implementing it .i must be doing something wrong
No, you're just creating the Font object and assigning it to a field or variable.Check out the API docs for java.awt.Graphics. There's probably a method there to set the font object used for all subsequent string draws.
my mistake, i also added -g.setFont(clockFont);in the public void pain(Graphics g) method. its still not displaying teh clock countdown in a bold, larger fomat
Are you sure that Font is installed on your system? I can't recall java's behavior if you try to instantiate a font that you don't actually have, but it may just use the default font.You may want to try logical font names.
yes its installed for sure. i gave up on that though what im trying to do now is change the clock style from military format to regular format , does any1 know how to do this. ?
import java.applet.Applet;
import java.awt.Graphics;
import javax.swing.Timer;
import java.awt.event.*;
import java.awt.Color;
import java.util.Date;
public class MyClock2 extends Applet implements ActionListener {
private Timer tm = null;
public void init() {
this.setBackground(Color.white);
tm = new Timer(1000, this);
}
public void start() {
tm.start();
}
public void actionPerformed(ActionEvent e) {
this.repaint();
}
public void paint(Graphics g) {
Date d = new Date();
g.setColor(Color.blue);
g.drawString(d.toString(), 50, 25);
}
}
You need to use java.text.SimpleDateFormat and create a custom format to format the date before displaying. Here's some code.
df=new SimpleDateFormat("E, hh:mm:ss a");
df.format(new Date());
Fri, 08:11:09 PM
Read the API for the SimpleDateFormat to see the options for fornatting the date and time. I just used some for an example.