Solved

Constant Flickering in Java Applet

Posted on 2003-11-12
18
880 Views
Last Modified: 2013-12-03
I am trying to create a Java applet with a TextField and a Button.
The program is supposed to change a certain text into what I enter in the text field when I press said Button.
That does happen, but then the Button and the TextField start flickering, and I can't use them anymore...

[code]
/**********
 * Basic applet test...
 * Uses an internet page with the code
 * <applet code="AppletTest.class" width="300" height="200"> </applet>
 * inserted in it.
 * All this was done with examples from: http://www.realapplets.com/tutorial/
 **********/

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

public class AppletTest extends Applet implements ActionListener
{
      Font font;
      Color bgColor;
      Color fontColor;
      TextField command;
      Button okButton;
      
    public void init()
    {
          setLayout(new FlowLayout());
          
          font = new Font("Verdana",Font.PLAIN,10);
          fontColor = Color.black;
          bgColor = Color.white;
          setBackground(bgColor);
          
          command = new TextField("Hello World!",290);
          command.setBounds(5,178,290,20);
          
          okButton = new Button("OK");
          okButton.setBounds(15,60,20,20);
          okButton.addActionListener(this);
    }
   
    public void stop()
    {}
      
    public void paint(Graphics g)
    {
          g.setFont(font);
          g.setColor(fontColor);
          
          g.drawString(": : BIG SHINY LETTERS : :",80,20);
            g.drawLine(5,45,295,45);
            add(okButton);
            g.drawString(command.getText(),5,100);
            g.drawLine(5,163,295,163);
            g.drawString("enter text:",10,174);
            add(command);
    }
   
    public void actionPerformed(ActionEvent evt)
    {
          repaint();
    }
}
[/code]
0
Comment
Question by:Düber
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 3
  • +2
18 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9735443
Don't add components in your paint method
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9735575
Create an action listener on your button then
add this :

// Use this to change text on your textfiled
command.setText(": : BIG SHINY LETTERS : :");

Hope this helps . . .
JAVATM
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9735579
import java.applet.*;  
import java.awt.*;
import java.awt.event.*;

public class AppletTest extends Applet implements ActionListener
{
     Font font;
     Color bgColor;
     Color fontColor;
     TextField command;
     Button okButton;
     
    public void init()
    {
         setLayout(new FlowLayout());
         
         font = new Font("Verdana",Font.PLAIN,10);
         fontColor = Color.black;
         bgColor = Color.white;
         setBackground(bgColor);
         
         command = new TextField("Hello World!",290);
         command.setBounds(5,178,290,20);
         
         okButton = new Button("OK");
         okButton.setBounds(15,60,20,20);
         okButton.addActionListener(this);
    }
   
    public void stop()
    {}
         
    public void actionPerformed(ActionEvent evt)
    {
         command.setText(": : BIG SHINY LETTERS : :");
    }
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 9735625
something like:

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

public class AppletTest extends Applet implements ActionListener
{
     Font font;
     Color bgColor;
     Color fontColor;
     TextField command;
     Button okButton;
     Label entered = new Label();

    public void init()
    {
         setLayout(new FlowLayout());
         
         font = new Font("Verdana",Font.PLAIN,10);
         fontColor = Color.black;
         bgColor = Color.white;
         setBackground(bgColor);
         
         command = new TextField("Hello World!",290);
         
         okButton = new Button("OK");
         okButton.addActionListener(this);

         add(okButton);
         add(command);
         add(label);
    }
   
    public void stop()
    {}
         
    public void actionPerformed(ActionEvent evt)
    {
         label.setText(command.getText());
    }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9735676
There's nothing to stop you using your custom paint method - just don't add components in it:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/**
 *Description of the Class
 *
 * @author     protean
 * @created    12 November 2003
 */
public class AppletTest extends Applet implements ActionListener {
  Font font;
  Color bgColor;
  Color fontColor;
  TextField command;
  Button okButton;


  /**
   *Description of the Method
   */
  public void init() {
    setLayout(new FlowLayout());

    font = new Font("Verdana", Font.PLAIN, 10);
    fontColor = Color.black;
    bgColor = Color.white;
    setBackground(bgColor);

    command = new TextField("Hello World!", 290);
    command.setBounds(5, 178, 290, 20);

    okButton = new Button("OK");
    okButton.setBounds(15, 60, 20, 20);
    okButton.addActionListener(this);

    add(okButton);
    add(command);
  }


  /**
   *Description of the Method
   */
  public void stop() { }


  /**
   *Description of the Method
   *
   * @param  g  Description of Parameter
   */
  public void paint(Graphics g) {
    g.setFont(font);
    g.setColor(fontColor);

    g.drawString(": : BIG SHINY LETTERS : :", 80, 20);
    g.drawLine(5, 45, 295, 45);

    g.drawString(command.getText(), 5, 100);
    g.drawLine(5, 163, 295, 163);
    g.drawString("enter text:", 10, 174);
  }


  /**
   *Description of the Method
   *
   * @param  evt  Description of Parameter
   */
  public void actionPerformed(ActionEvent evt) {
    repaint();
  }
}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9735958
but if you override paint()  make sure you override update too :-)

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/**
 *Description of the Class
 *
 * @author     protean
 * @created    12 November 2003
 */
public class AppletTest extends Applet implements ActionListener {
  Font font;
  Color bgColor;
  Color fontColor;
  TextField command;
  Button okButton;


  /**
   *Description of the Method
   */
  public void init() {
    setLayout(new FlowLayout());

    font = new Font("Verdana", Font.PLAIN, 10);
    fontColor = Color.black;
    bgColor = Color.white;
    setBackground(bgColor);

    command = new TextField("Hello World!", 290);
    command.setBounds(5, 178, 290, 20);

    okButton = new Button("OK");
    okButton.setBounds(15, 60, 20, 20);
    okButton.addActionListener(this);

    add(okButton);
    add(command);
  }


  /**
   *Description of the Method
   */
  public void stop() { }

  public void update( Graphics g )
  {
      paint( g ) ;
  }

  /**
   *Description of the Method
   *
   * @param  g  Description of Parameter
   */
  public void paint(Graphics g) {
    g.setFont(font);
    g.setColor(fontColor);

    g.drawString(": : BIG SHINY LETTERS : :", 80, 20);
    g.drawLine(5, 45, 295, 45);

    g.drawString(command.getText(), 5, 100);
    g.drawLine(5, 163, 295, 163);
    g.drawString("enter text:", 10, 174);
  }


  /**
   *Description of the Method
   *
   * @param  evt  Description of Parameter
   */
  public void actionPerformed(ActionEvent evt) {
    repaint();
  }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 9736030
If you want to handle the painting yourself then the following should do more along the lines that you want, though it still has a variety of problems. I'd still suggest using specific components to handle the display of the various items, a lot more flexible and neater:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/**
 *Description of the Class
 *
 * @author     protean
 * @created    12 November 2003
 */
public class AppletTest extends Applet implements ActionListener {
  Font font;
  Color bgColor;
  Color fontColor;
  TextField command;
  Button okButton;
  String text = "";

  /**
   *Description of the Method
   */
  public void init() {
    setLayout(new FlowLayout());

    font = new Font("Verdana", Font.PLAIN, 10);
    fontColor = Color.black;
    bgColor = Color.white;
    setBackground(bgColor);

    command = new TextField("Hello World!", 290);
    command.setBounds(5, 178, 290, 20);

    okButton = new Button("OK");
    okButton.setBounds(15, 60, 20, 20);
    okButton.addActionListener(this);

    add(okButton);
    add(command);
  }


  /**
   *Description of the Method
   */
  public void stop() { }

  public void update( Graphics g )
  {
      paint( g ) ;
  }

  /**
   *Description of the Method
   *
   * @param  g  Description of Parameter
   */
  public void paint(Graphics g) {
    g.setFont(font);
    g.setColor(fontColor);

    g.drawString(": : BIG SHINY LETTERS : :", 80, 20);
    g.drawLine(5, 45, 295, 45);

    g.drawString(text, 5, 100);
    g.drawLine(5, 163, 295, 163);
    g.drawString("enter text:", 10, 174);
  }


  /**
   *Description of the Method
   *
   * @param  evt  Description of Parameter
   */
  public void actionPerformed(ActionEvent evt) {
    text = command.getText();
    repaint();
  }
}
0
 

Author Comment

by:Düber
ID: 9738499
All the different code examples result in this:
http://home.no.net/npsycho/applet-test.gif
No matter what values I enter into the Button and TextField setBounds.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9738600
Try getting rid of:

    setLayout(new FlowLayout());
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9738794
>>All the different code examples result in this:

Yes, but what is it that you want the result to be? ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 9742149
> All the different code examples result in this:

Thats why I suggested using seperate components so you could contol the layout.
0
 

Author Comment

by:Düber
ID: 9744834
>Yes, but what is it that you want the result to be? ;-)

Sorry... forgot... :P
This is what the original looks like, but when I press OK it flickers like a ...(insert bad word here).
http://home.no.net/npsycho/orig.gif

Sorry if the answer is simple, and I am not getting it.  We have not covered ANY applet coding in school, so I am a newb to the extreme!
0
 
LVL 92

Expert Comment

by:objects
ID: 9744863
The layout manager is (correctly) handling laying out your components.
If you don't want to use a layout manager then set the layout manager to null instead of using FlowLayout:

setLayout(null);

If you do want to use layout managers then you'll need to use appropriate layout managers to achieve the result you desire.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 9744886
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/**
 *Description of the Class
 *
 * @author     protean
 * @created    12 November 2003
 */
public class AppletTest extends Applet implements ActionListener {
  Font font;
  Color bgColor;
  Color fontColor;
  TextField command;
  Button okButton;
  String text = "";

  /**
   *Description of the Method
   */
  public void init() {
    setLayout(null);

    font = new Font("Verdana", Font.PLAIN, 10);
    fontColor = Color.black;
    bgColor = Color.white;
    setBackground(bgColor);

    command = new TextField("Hello World!", 290);
    command.setBounds(5, 178, 290, 20);

    okButton = new Button("OK");
    okButton.setBounds(15, 60, 20, 20);
    okButton.addActionListener(this);

    add(okButton);
    add(command);
  }


  /**
   *Description of the Method
   */
  public void stop() { }

  public void update( Graphics g )
  {
      paint( g ) ;
  }

  /**
   *Description of the Method
   *
   * @param  g  Description of Parameter
   */
  public void paint(Graphics g) {
    g.setFont(font);
    g.setColor(fontColor);

    g.drawString(": : BIG SHINY LETTERS : :", 80, 20);
    g.drawLine(5, 45, 295, 45);

    g.drawString(text, 5, 100);
    g.drawLine(5, 163, 295, 163);
    g.drawString("enter text:", 10, 174);
  }


  /**
   *Description of the Method
   *
   * @param  evt  Description of Parameter
   */
  public void actionPerformed(ActionEvent evt) {
    text = command.getText();
    repaint();
  }
}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9746027
> setLayout(null);

Isn't that the same as I suggested?  Just missing out the setLayout() altogether?

I'm not sure if it is ;-)

Tim
0
 
LVL 92

Expert Comment

by:objects
ID: 9751314
> Isn't that the same as I suggested?  Just missing out the setLayout() altogether?

No leaving it out will use the default layout manager for an applet.
0
 

Author Comment

by:Düber
ID: 9752502
Thanks to everyone who helped out!  I can finally make kickass applets!

Just goes to show, the internet is not only used for porn!

Thanks again!
0
 
LVL 92

Expert Comment

by:objects
ID: 9752685
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How  can  i  resolve  HTTP Status 404 -? 8 80
Using Specialized Fonts in CSS 1 61
glassfish admin console 1 53
netbeans can not run self signed app 8 38
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question