g.drawString command not working.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: g.drawString command not working.

  1. #1
    Join Date
    Oct 2004
    Posts
    11

    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?

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    What is calling spiltTime() in the above code?

  3. #3
    Join Date
    Oct 2004
    Posts
    11
    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.

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    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.

  5. #5
    Join Date
    Feb 2004
    Posts
    808
    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
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  6. #6
    Join Date
    Feb 2004
    Posts
    808
    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
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center