Results 1 to 16 of 16
  1. #1
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default graphics won't change

    I am using these if statements to change which line is drawn, but the line never changes and I dont' know why. Heres my code:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class DrawingLines extends Applet {
    
       int width, height, x;
       Boolean wait = true;
    
       public void init() {
          x = 1;
          width = getSize().width;
          height = getSize().height;
          setBackground( Color.black );
    
       }
    
       public void main(String args[]) {
          try 
          {
          Thread.sleep(2000); // do nothing for 1000 miliseconds (1 second)
          } 
          catch(InterruptedException e)
          {
          e.printStackTrace();
          }
          wait = false;
       }
    
       public void paint( Graphics g ) {
          repaint();
          g.setColor( Color.green );
          if (wait == true) {
          	g.drawLine(20, 20, 100, 100);
          }
          if (wait == false) {
          	g.drawLine(0, 0, 150, 100);
          }
       }
    }
    Hope someone can fix it, thanks in advance.

  2. #2
    67726e is offline Member
    Join Date
    Mar 2010
    Location
    Hilton Head, SC
    Posts
    34
    Rep Power
    0

    Default

    I don't usually do graphics work but I'm pretty sure you need to call repaint() after you draw anything new for it to show up.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Try debugging your code by adding a println() in the methods to see if they are being called.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    A main( ... ) method in an Applet? Go read some tutorials first and don't guess ...

    kind regards,

    Jos

  5. #5
    devang_tandel is offline Member
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Exclamation shape of land

    hey
    i want to calculate area of given land
    coordinates may entered at execution time.
    plz help me
    with applet

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by devang_tandel View Post
    i want to calculate area of given land
    coordinates may entered at execution time.
    plz help me
    with applet
    Don't hijack someone else's thread; start your own.

    Jos

  7. #7
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Well I looked for tutorials, but I couldn't find anything for making new graphics, I found stuff about repaint() and double buffering but not changing a line to a box for example.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by pizzadude223 View Post
    Well I looked for tutorials, but I couldn't find anything for making new graphics, I found stuff about repaint() and double buffering but not changing a line to a box for example.
    That is still no excuse for having a main( ... ) method in an Applet because it will never be called. There's an init() method and a start() method for that; like I wrote: read some (relevant) tutorials.

    kind regards,

    Jos

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Try debugging your code by adding a println() in the methods to see if they are being called.
    Have you tried that to see what methods in your code are being called? Or NOT called.

  10. #10
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    I'm compiling with notepad and don't really have anything else that will actually say stuff with println(), so what program would I use with that?

  11. #11
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    I got it by changing the main method to the start() method, thanks.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Sorry, what I meant by println() is System.out.println()

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Norm View Post
    Have you tried that to see what methods in your code are being called? Or NOT called.
    That one is easy; on page one of any Applet tutorial it says that a (J)Applet doesn't have a main( ... ) method; it has an init() method and a start() method and a couple more. The OP should do some reading instead of making wild guesses and wishful thinking.

    kind regards,

    Jos

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Strictly speaking an applet can have a main() method. It just won't be called by the JVM when the applet is started executing by the JVM in a browser. It would be called if the applet was started by the command: java DrawLines

    The problem was the OP expected it to be called. A println() would show it wasn't.
    Assumptions can lead you astray. Adding println()s will show you what is happening.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by prabhudass View Post
    sir u see the the java progam code it is sample for java and it is write code
    and it is big program.
    Please stop posting information-less nonsense all over the place.

    kind regards,

    Jos

  16. #16
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default starting an applet

    You are looking for a way to begin the execution
    of your experimental algorithm.

    There are two ways to do this in java.

    For Applets, you write a start(){...} method.
    For Applications you write a main(String args[]){...}
    method (sometimes referred to as the "launch point").

    Since you are working with an applet, you
    need to add a start(){...} method to get
    your applet running.

    You don't have to get rid of the main(String args[]){...}
    method in your code, but it looks obviously amature.

    I have added the following start(){...} method to
    your applet:


    Java Code:
       public void start() {
         main( new String[]{"dummy string"} );
       }
    Notice the weird call to your main(String[] args){...}
    method.

    This code runs.

    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class DrawingLines extends Applet {
    
       int width, height, x;
       Boolean wait = true;
    
       public void init() {
          x = 1;
          width = getSize().width;
          height = getSize().height;
          setBackground( Color.black );
       }
    
       public void start() {
         main( new String[]{"dummy string"} );  // Silly, but neccessary to
       }                                        // call your main method.
    
       public void main(String args[]) {
          try {
            Thread.sleep(2000); // do nothing for 1000 miliseconds (1 second)
          } 
          catch(InterruptedException e){
            e.printStackTrace();
          }
          wait = false;
       }
    
       public void paint( Graphics g ) {
          repaint();
          g.setColor( Color.green );
          if (wait == true) {
          	g.drawLine(20, 20, 100, 100);
          }
          if (wait == false) {
          	g.drawLine(0, 0, 150, 100);
          }
       }
    }

    Here is your applet without the main(String[] args){...}
    method. I haven't tested it. It should produce the same
    results as the code above.


    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class DrawingLines extends Applet {
    
       int width, height, x;
       Boolean wait = true;
    
       public void init() {
          x = 1;
          width = getSize().width;
          height = getSize().height;
          setBackground( Color.black );
       }
    
       public void start() {
          try {
            Thread.sleep(2000); // do nothing for 1000 miliseconds (1 second)
          } 
          catch(InterruptedException e){
            e.printStackTrace();
          }
          wait = false;
       }
    
       public void paint( Graphics g ) {
          repaint();
          g.setColor( Color.green );
          if (wait == true) {
          	g.drawLine(20, 20, 100, 100);
          }
          if (wait == false) {
          	g.drawLine(0, 0, 150, 100);
          }
       }
    }

Similar Threads

  1. Help me with graphics
    By 7oclock in forum New To Java
    Replies: 12
    Last Post: 04-05-2009, 12:20 AM
  2. Help with 2d graphics please
    By xbox_nutter in forum New To Java
    Replies: 0
    Last Post: 04-02-2009, 12:48 PM
  3. Graphics
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-25-2008, 07:24 PM
  4. graphics
    By Joe2003 in forum Advanced Java
    Replies: 4
    Last Post: 01-18-2008, 08:44 PM
  5. Graphics
    By feniger in forum New To Java
    Replies: 1
    Last Post: 12-29-2007, 05:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •