-
g.drawString command not working.
Code:
import java.awt.*;
import java.applet.Applet;
//<APPLET code=time.class width=300 height=150> </APPLET>
public class time extends Applet {
public void splitTime (Graphics g, int seconds) {
int mins = (seconds/60);
int nSeconds = (seconds % 60);
g.drawString(seconds + " = " + mins + " minutes " + nSeconds + " seconds", 50, 50);
}
}
when i compile the above code and run it with applet viewer, text doesn't display (compiles with no error messages). i'm new to java and can't see what is wrong with the above code.
can anyone help me?
-
What is calling spiltTime() in the above code?
-
i forgot about that
the command splitTime(307), gets aload of error messages from the compiler
Code:
import java.awt.*;
import java.applet.Applet;
//<APPLET code=time.class width=300 height=150> </APPLET>
public class time extends Applet {
splitTime(307);
public void splitTime (Graphics g, int seconds) {
int mins = (seconds/60);
int nSeconds = (seconds % 60);
g.drawString(seconds + " = " + mins + " minutes " + nSeconds + " seconds", 50, 50);
}
}
can you tell me whats wrong with the above code?
thanks for the help.
-
You've defined splitTime(Graphics g, int seconds) with two parameters, but you're calling it with only one parameter. This will result in a compiler error.
When a java GUI repaints, the components paint(Graphics g) method is called. You have to overide this method and make it do what you want it to. You need something like this in your JPanel
Code:
public void paint(Graphics g)
{
splitTime(g,307);
}
I don't see a Component in your code though, I just see an applet. I haven't used applets before so I have to admit I don't know what methods it has in it, or what it's superclasses are. Does it extend Component? If so it has a paint method you can override.
-
Originally posted by mikeBarr81
[B]You've defined splitTime(Graphics g, int seconds) with two parameters, but you're calling it with only one parameter. This will result in a compiler error.
When a java GUI repaints, the components paint(Graphics g) method is called. You have to overide this method and make it do what you want it to. You need something like this in your JPanel
Note: JPanels would be paintComponent()ed.
note2: Applets cannot be paintComponent()ed, only JApplets
Code:
public void paint(Graphics g)
{
splitTime(g,307);
}
I don't see a Component in your code though, I just see an applet. i haven't used applets before so I have to admit I don't know what methods it has in it, or what it's superclasses are. Does it extend Component?
[/quote]
yes
-
Originally posted by gunner_uk2000
[B]i forgot about that
the command splitTime(307), gets aload of error messages from the compiler
Code:
import java.awt.*;
import java.applet.Applet;
//<APPLET code=time.class width=300 height=150> </APPLET>
public class time extends Applet {
splitTime(307);
public void splitTime (Graphics g, int seconds) {
int mins = (seconds/60);
int nSeconds = (seconds % 60);
g.drawString(seconds + " = " + mins + " minutes " + nSeconds + " seconds", 50, 50);
}
}
can you tell me whats wrong with the above code?
- you cant make a method call in the class region; it must be inside a method. the only thing that can go in the class region is variable declarations
- "time" should have a capital letter
- writing Graphics g in a method decalration does not automatically provide a graphics context to draw on. you would be better following mikes advice, and overriding applet's paint(Graphics g) method, so that it calls your splitTime with g and 307
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks