Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Java Applets - Events and Graphics

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Applets - Events and Graphics

    // Hi, Will someone kindly take a peek at the code below, corrrect it and let me know why this simple applet does not work.

    // All I am trying to do is, as the buttons are pressed, 1000 to increment or decrement accordingly. Thank you for your time.

    import java.applet.*; import java.awt.*; import javax.swing.*; import java.awt.event.*;

    public class button2 extends Applet implements ActionListener{
    Button increase, decrease;
    int value = 1000;

    public void init (){
    increase = new Button("Increase the value");
    add(increase);
    decrease = new Button("Decrease the value");
    add(decrease);
    }

    public void actionPerformed( ActionEvent e) {
    Object obj = e.getSource( );

    if (obj == increase)
    value++;
    if (obj == decrease)
    value--;
    repaint();
    }

    public void paint (Graphics g){
    g.drawString("The value is currently " + value, 50, 80);
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Java Applets - Events and Graphics

    why this simple applet does not work.
    Please explain what "does not work" means.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Applets - Events and Graphics

    When I click the "Increase" button, the value of 1000 should be increased to 1001 but nothing happens. Can you please help. Thank you for your time.


    import java.applet.*;  import java.awt.*;  import javax.swing.*;  import java.awt.event.*;
     
    public class buttonDemo2 extends Applet implements ActionListener{
       Button increase, decrease;
       int value = 1000;
     
       public void init (){
    	 increase = new Button("Increase the value");
         add(increase);
         decrease = new Button("Decrease the value");
         add(decrease);
       }
     
      public void actionPerformed( ActionEvent e) {
    	Object obj = e.getSource( );
     
        if (obj == increase)
         value++;
        if (obj == decrease)
         value--;
        repaint();
       }
     
       public void paint (Graphics g){
    	 g.drawString("The value is currently " + value, 50, 80);
       }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Java Applets - Events and Graphics

    See this thread for how to add a listener to a button:
    Java Applets
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Applets - Events and Graphics

    Norm,

    How do I tie the actionPerfomed method to the Paint method.

    Ashliii

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Java Applets - Events and Graphics

    Why do you want to do that? The code currently calls the repaint() method which will cause the jvm to call the paint() method.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Applets - Events and Graphics

    Can you please see why then, the code does not respond to the actionPerfomed method and repaint. Thanks.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Java Applets - Events and Graphics

    Try debugging the code by adding some println statements to it that print out a message when any method is called. Then you will know what methods are being called.
    Execute the code, copy the contents of the console with the print outs and paste it here.

    Also Post the current version of the code.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Applets - Events and Graphics

    I am sorry, it seems println statements do not work in applets, but I have pasted the code.

    [code = java]
    import java.applet.*; import java.awt.*; import javax.swing.*; import java.awt.event.*;

    public class buttonDemo2 extends Applet implements ActionListener{
    Button increase, decrease;
    int value = 1000;

    public void init (){
    increase = new Button("Increase the value");
    add(increase);
    decrease = new Button("Decrease the value");
    add(decrease);
    }

    public void actionPerformed( ActionEvent e) {
    Object obj = e.getSource( );

    if (obj == increase){
    value++;
    }

    if (obj == decrease){
    value--;
    }

    repaint();
    }

    public void paint (Graphics g){
    g.drawString("The value is currently " + value, 50, 80);
    }
    }
    [/code]

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Java Applets - Events and Graphics

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Where does the code add the action listener to the button? See: Java Applets


    println statements do not work in applets
    They do. I use them all the time.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Applets - Events and Graphics

    Object obj = e.getSource();

    if (obj == increase)
    value++;

    BTW, can we see an applet code with a println statment in it. Thanks.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Java Applets - Events and Graphics

    Have you solved the problem now? You haven't responded to my suggestions and requests so I assume that you don't need any more help.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Applets - Events and Graphics

    Ok, thanks, I got it by adding the addActionListener(this); to both buttons, thank you for help and time.

Similar Threads

  1. Java Applets
    By ashliii in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2013, 02:36 PM
  2. How to draw a houses using Java Applets and Awt?
    By Heizzer10 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 31st, 2012, 04:08 AM
  3. mysql in java applets
    By micro kid in forum JDBC & Databases
    Replies: 7
    Last Post: June 11th, 2012, 06:40 AM
  4. How do I debug my Java applets to see what's going wrong?
    By diyaots in forum AWT / Java Swing
    Replies: 2
    Last Post: November 10th, 2011, 08:37 AM
  5. Learn applets in java
    By mohsendeveloper in forum Java Applets
    Replies: 2
    Last Post: June 25th, 2009, 02:50 PM