Düber
asked on
Constant Flickering in Java Applet
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,29 0,20);
okButton = new Button("OK");
okButton.setBounds(15,60,2 0,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.getTe xt(),5,100 );
g.drawLine(5,163,295,163);
g.drawString("enter text:",10,174);
add(command);
}
public void actionPerformed(ActionEven t evt)
{
repaint();
}
}
[/code]
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,
fontColor = Color.black;
bgColor = Color.white;
setBackground(bgColor);
command = new TextField("Hello World!",290);
command.setBounds(5,178,29
okButton = new Button("OK");
okButton.setBounds(15,60,2
okButton.addActionListener
}
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.getTe
g.drawLine(5,163,295,163);
g.drawString("enter text:",10,174);
add(command);
}
public void actionPerformed(ActionEven
{
repaint();
}
}
[/code]
Don't add components in your paint method
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
add this :
// Use this to change text on your textfiled
command.setText(": : BIG SHINY LETTERS : :");
Hope this helps . . .
JAVATM
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,29 0,20);
okButton = new Button("OK");
okButton.setBounds(15,60,2 0,20);
okButton.addActionListener (this);
}
public void stop()
{}
public void actionPerformed(ActionEven t evt)
{
command.setText(": : BIG SHINY LETTERS : :");
}
}
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,
fontColor = Color.black;
bgColor = Color.white;
setBackground(bgColor);
command = new TextField("Hello World!",290);
command.setBounds(5,178,29
okButton = new Button("OK");
okButton.setBounds(15,60,2
okButton.addActionListener
}
public void stop()
{}
public void actionPerformed(ActionEven
{
command.setText(": : BIG SHINY LETTERS : :");
}
}
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(ActionEven t evt)
{
label.setText(command.getT ext());
}
}
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,
fontColor = Color.black;
bgColor = Color.white;
setBackground(bgColor);
command = new TextField("Hello World!",290);
okButton = new Button("OK");
okButton.addActionListener
add(okButton);
add(command);
add(label);
}
public void stop()
{}
public void actionPerformed(ActionEven
{
label.setText(command.getT
}
}
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.getTe xt(), 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(ActionEven t evt) {
repaint();
}
}
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
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.getTe
g.drawLine(5, 163, 295, 163);
g.drawString("enter text:", 10, 174);
}
/**
*Description of the Method
*
* @param evt Description of Parameter
*/
public void actionPerformed(ActionEven
repaint();
}
}
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.getTe xt(), 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(ActionEven t evt) {
repaint();
}
}
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
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.getTe
g.drawLine(5, 163, 295, 163);
g.drawString("enter text:", 10, 174);
}
/**
*Description of the Method
*
* @param evt Description of Parameter
*/
public void actionPerformed(ActionEven
repaint();
}
}
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(ActionEven t evt) {
text = command.getText();
repaint();
}
}
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
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(ActionEven
text = command.getText();
repaint();
}
}
ASKER
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.
http://home.no.net/npsycho/applet-test.gif
No matter what values I enter into the Button and TextField setBounds.
Try getting rid of:
setLayout(new FlowLayout());
setLayout(new FlowLayout());
>>All the different code examples result in this:
Yes, but what is it that you want the result to be? ;-)
Yes, but what is it that you want the result to be? ;-)
> All the different code examples result in this:
Thats why I suggested using seperate components so you could contol the layout.
Thats why I suggested using seperate components so you could contol the layout.
ASKER
>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!
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!
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> setLayout(null);
Isn't that the same as I suggested? Just missing out the setLayout() altogether?
I'm not sure if it is ;-)
Tim
Isn't that the same as I suggested? Just missing out the setLayout() altogether?
I'm not sure if it is ;-)
Tim
> 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.
No leaving it out will use the default layout manager for an applet.
ASKER
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!
Just goes to show, the internet is not only used for porn!
Thanks again!