Link to home
Start Free TrialLog in
Avatar of bita
bita

asked on

How to stop the flickering?

Hi,

Can you please check this program as to why the contents of the paint() method are flickering at the same time as the "COMPANY" scrolling text, and how to stop this.
Thanks.
---

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

public class BoxDemo1 extends Applet implements ItemListener,ActionListener,Runnable
{

//scroll text
Font f = new Font("Arial Black", Font.BOLD, 10);
  Color col = new Color(204,153,0);
  Color back = new Color(249,249,249);
  String wel = new String("COMPANY");
  Thread runner;
  int x;
  int stringSize;
public void start() {
     if (runner == null) {
        runner = new Thread(this);
        runner.start();
     }
  }

  public void stop() {
     if (runner != null) {
        runner = null;
     }
  }

  public void run() {
     Thread thisThread = Thread.currentThread();
     x = 10-stringSize;
     int cont = getSize().width;
     while (runner == thisThread) {
        x = x + 2;
        repaint();
        try {
           Thread.sleep(20);
        } catch (InterruptedException e) { }
        if (x > cont) x = -stringSize;
     }
  }


//checkbox
String msg=" ";
Checkbox Win98, winNT, solaris,mac;

//checkboxgroup
Checkbox one,two,three,four;
CheckboxGroup cbg;

//choicelist
Choice fruit;

//list
List vegetables;

//TextField
TextField name,pass;

public void init()
{

//scroll text
setForeground(col);
     setBackground(back);
      setFont(f);
      FontMetrics fm=getFontMetrics(f);
      stringSize=fm.stringWidth(wel);



//checkbox
Win98=new Checkbox("Win98");

winNT=new Checkbox("winNT");

solaris=new Checkbox("solaris");

mac=new Checkbox("mac");

//checkboxgroup
cbg=new CheckboxGroup();
one=new Checkbox("one", cbg,true);

two=new Checkbox("two", cbg,false);
three=new Checkbox("three", cbg,false);
four=new Checkbox("four", cbg,false);

//choicelist
fruit=new Choice();
fruit.add("apple");
fruit.add("banana");
fruit.add("grapes");
fruit.add("melon");


//list
vegetables=new List(4,true);
vegetables.add("onion");
vegetables.add("potatoes");
vegetables.add("marrow");
vegetables.add("carrot");

//textfield
Label namep=new Label("Name: ", Label.RIGHT);
Label passp=new Label("Password: ", Label.RIGHT);
name=new TextField(12);
pass=new TextField(8);
pass.setEchoChar('*');



//checkbox
add(Win98);
add(winNT);
add(solaris);
add(mac);

//checkboxgroup
add(one);
add(two);
add(three);
add(four);

//choicelist
add(fruit);

//list
add(vegetables);

//textfield
add(namep);
add(name);
add(passp);
add(pass);

//checkbox
Win98.addItemListener(this);
winNT.addItemListener(this);
solaris.addItemListener(this);
mac.addItemListener(this);

//checkboxgroup
one.addItemListener(this);
two.addItemListener(this);
three.addItemListener(this);
four.addItemListener(this);

//choicelist
fruit.addItemListener(this);

//list
vegetables.addActionListener(this);

//textfield
name.addActionListener(this);
pass.addActionListener(this);
}


public void itemStateChanged(ItemEvent ie)
{
repaint();
}

public void actionPerformed(ActionEvent ae)
{
repaint();
}


public void paint(Graphics g)
{
//scroll text
g.drawString(wel, x, 25);

//checkbox
msg="Current Status: ";
g.drawString(100,200);
msg=" Win98: " +Win98.getState();
g.drawString(msg,6,100);
msg=" winNT: " +winNT.getState();
g.drawString(msg,6,120);
msg=" Solaris: "+solaris.getState();
g.drawString(msg,6,140);
msg=" mac: "+mac.getState();
g.drawString(msg,6,160);

//checkboxgroup
msg="Number selection: ";
msg+=cbg.getSelectedCheckbox().getLabel();
g.drawString(msg,0,180);

//choicelist
msg="Fruit Selection: ";
msg+=fruit.getSelectedItem();
g.drawString(msg,40,200);

//list
int idx[];
msg="Vegetable selection: ";
idx=vegetables.getSelectedIndexes();
for(int i=0; i<idx.length;i++)
msg+=vegetables.getItem(idx[i])+" ";
g.drawString(msg,60,220);

//textfield
g.drawString("Name: "+ name.getText(),80,240);
g.drawString("Selected text in name: " + name.getSelectedText(),70,260);
g.drawString("Password: "+ pass.getText(),80,280);
}
}
ASKER CERTIFIED SOLUTION
Avatar of ahosang
ahosang
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mick Barry
Try adding the following method:

public void update(Graphics g)
{
   paint(g);
}
That won't clear the screen though objects. I've made a little modifications to that code. See if it's any good:

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

public class BoxDemo1 extends Applet implements ItemListener, KeyListener, Runnable
{

//scroll text
 Font f = new Font("Arial Black", Font.BOLD, 18);
 Color col = new Color(204,153,0);
 Color back = new Color(249,249,249);
 String wel = new String("COMPANY");
 Thread runner;
 int x;
 int stringSize;
 
 
 GridLayout detgrid = new GridLayout(11,2,5,5);
 
 public void start() {
    if (runner == null) {
       runner = new Thread(this);
       runner.start();
    }
 }

 public void stop() {
    if (runner != null) {
       runner = null;
    }
 }

 public void run() {
    Thread thisThread = Thread.currentThread();
    x = 10-stringSize;
    int cont = getSize().width;
    while (runner == thisThread) {
       x = x + 2;
       repaint();
         try {
          Thread.sleep(20);
       } catch (InterruptedException e) { }
       if (x > cont) x = -stringSize;
    }
 }


//checkbox
String msg=" ";
Checkbox Win98, winNT, solaris,mac;

//checkboxgroup
Checkbox one,two,three,four;
CheckboxGroup cbg;

//choicelist
Choice fruit;

//list
List vegetables;

//TextField
TextField name,pass;

// Labels
Label Win98Label, statusLabel, winNTLabel, SolarisLabel, macLabel, NumberLabel, FruitLabel, VegLabel,nameLabel, nameSelectedLabel, passwordLabel;
Label Win98Labelb, statusLabelb, winNTLabelb,SolarisLabelb, macLabelb, NumberLabelb, FruitLabelb, VegLabelb,nameLabelb, nameSelectedLabelb, passwordLabelb;
Label scrollText;

public void init()
{
setLayout(null);
//scroll text


setForeground(col);
setBackground(back);
FontMetrics fm=getFontMetrics(f);
stringSize=fm.stringWidth(wel);
Panel scrollPanel=new Panel();
scrollPanel.reshape(0,0,750,50);
add(scrollPanel);
scrollPanel.setVisible(false);

//checkbox
Win98=new Checkbox("Win98");

winNT=new Checkbox("winNT");

solaris=new Checkbox("solaris");

mac=new Checkbox("mac");

//checkboxgroup
cbg=new CheckboxGroup();
one=new Checkbox("one", cbg,true);

two=new Checkbox("two", cbg,false);
three=new Checkbox("three", cbg,false);
four=new Checkbox("four", cbg,false);

//choicelist
fruit=new Choice();
fruit.add("apple");
fruit.add("banana");
fruit.add("grapes");
fruit.add("melon");


//list
vegetables=new List(4,true);
vegetables.add("onion");
vegetables.add("potatoes");
vegetables.add("marrow");
vegetables.add("carrot");

//textfield
Label namep=new Label("Name: ", Label.RIGHT);
Label passp=new Label("Password: ", Label.RIGHT);
name=new TextField(12);
name.addKeyListener(this);
name.addMouseListener(new MouseHandler());
pass=new TextField(8);
pass.addKeyListener(this);
pass.setEchoChar('*');


Panel pChoices=new Panel();
//checkbox
pChoices.add(Win98);
pChoices.add(winNT);
pChoices.add(solaris);
pChoices.add(mac);

//checkboxgroup
pChoices.add(one);
pChoices.add(two);
pChoices.add(three);
pChoices.add(four);

//choicelist
pChoices.add(fruit);

//list
pChoices.add(vegetables);

pChoices.setBounds(0,50,700,100);
add(pChoices);
pChoices.setVisible(true);


//textfield
Panel pText=new Panel();
pText.setLayout(new GridLayout(2,2));
pText.add(namep);
pText.add(name);
pText.add(passp);
pText.add(pass);


Win98Label=new Label("Win98: ", Label.RIGHT);
statusLabel=new Label("Current Status: ", Label.RIGHT);
winNTLabel=new Label("winNT: ", Label.RIGHT);
SolarisLabel=new Label("Solaris: ", Label.RIGHT);
macLabel=new Label("mac: ", Label.RIGHT);
NumberLabel=new Label("Number Selection: ", Label.RIGHT);
FruitLabel=new Label("Fruit Selected: ", Label.RIGHT);
VegLabel=new Label("Vegetable Selection: ", Label.RIGHT);
nameLabel=new Label("Name: ", Label.RIGHT);
nameSelectedLabel=new Label("Selected Text in name: ", Label.RIGHT);
passwordLabel=new Label("Password: ", Label.RIGHT);

Win98Labelb=new Label("false", Label.LEFT);
statusLabelb=new Label(" ", Label.LEFT);
winNTLabelb=new Label("false", Label.LEFT);
SolarisLabelb=new Label("false", Label.LEFT);
macLabelb=new Label("false", Label.LEFT);
NumberLabelb=new Label("one", Label.LEFT);
FruitLabelb=new Label("", Label.LEFT);
VegLabelb=new Label("", Label.LEFT);
nameLabelb=new Label(" ", Label.LEFT);
nameSelectedLabelb=new Label("", Label.LEFT);
passwordLabelb=new Label("", Label.LEFT);

Panel p=new Panel();
p.setLayout(detgrid);
p.add(statusLabel);
p.add(statusLabelb);
p.add(Win98Label);
p.add(Win98Labelb);
p.add(winNTLabel);
p.add(winNTLabelb);
p.add(SolarisLabel);
p.add(SolarisLabelb);
p.add(macLabel);
p.add(macLabelb);
p.add(NumberLabel);
p.add(NumberLabelb);
p.add(FruitLabel);
p.add(FruitLabelb);
p.add(VegLabel);
p.add(VegLabelb);
p.add(nameLabel);
p.add(nameLabelb);
p.add(nameSelectedLabel);
p.add(nameSelectedLabelb);
p.add(passwordLabel);
p.add(passwordLabelb);

p.setBounds(0,150,400,250);
add(p);
p.setVisible(true);
pText.setBounds(400,150,250,50);
add(pText);
pText.setVisible(true);

//checkbox
Win98.addItemListener(this);
winNT.addItemListener(this);
solaris.addItemListener(this);
mac.addItemListener(this);

//checkboxgroup
one.addItemListener(this);
two.addItemListener(this);
three.addItemListener(this);
four.addItemListener(this);

//choicelist
fruit.addItemListener(this);

//list
vegetables.addItemListener(this);
}

public void itemStateChanged(ItemEvent ie) {
  if (ie.getSource().equals((Object)fruit)) {
    FruitLabelb.setText(fruit.getSelectedItem());
  }
  else if (ie.getSource().equals((Object)vegetables)) {
    int idx[]=vegetables.getSelectedIndexes();
      StringBuffer sb=new StringBuffer("");
      for (int i=0;i<idx.length;i++) {
        sb.append(vegetables.getItem(idx[i])+" ");
      }
      VegLabelb.setText(sb.toString());
      invalidate();
      validate();
  }
  else {
    Checkbox cb=(Checkbox)ie.getItemSelectable();
    if (cb.getLabel().equals("Win98")) {Win98Labelb.setText((cb.getState())?"true":"false");}
      else if (cb.getLabel().equals("winNT")) {winNTLabelb.setText((cb.getState())?"true":"false");}
      else if (cb.getLabel().equals("solaris")) {SolarisLabelb.setText((cb.getState())?"true":"false");}
      else if (cb.getLabel().equals("mac")) {macLabelb.setText((cb.getState())?"true":"false");}
      else {
        NumberLabelb.setText(cb.getLabel());
      }
  }
}

public void keyTyped(KeyEvent ke) {}
public void keyPressed(KeyEvent ke) {}

public void keyReleased(KeyEvent ke) {
  if (ke.getSource().equals((Object)name)) {
    nameLabelb.setText(name.getText());
      nameSelectedLabelb.setText(name.getSelectedText());
  }
  if (ke.getSource().equals((Object)pass)) {
    passwordLabelb.setText(pass.getText());
  }
}

public void paint(Graphics g) {
  g.setFont(f);
  g.drawString(wel, x, 30);
}

class MouseHandler extends MouseAdapter {
  public void mouseReleased(MouseEvent me) {
    nameSelectedLabelb.setText(name.getSelectedText());
  }
}
}

There will be a class 'BoxDemo1$MouseHandler.class' that will be generated as a result of compiling BoxDemo1.java. Make sure that class gets put in the same directory as BoxDemo1.class for the browser to read it.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tonus
tonus

This is exactly what neutron has mentioned above.
 Have a look at this example:
     http://www.javaside.com/asp/mus.asp?page=/us/tips/j_4.shtml
This example that tonus pointed you to also handles situations of screen resizing by checking if the size of created image equals the current dimensions of applet-panel.

That is, of course, one thing you have to take care about in your applet.

Greetings,
    Ntr:)
This example that tonus pointed you to also handles situations of screen resizing by checking if the size of created image equals the current dimensions of applet-panel.

That is, of course, one thing you have to take care about in your applet.

Greetings,
    Ntr:)
neutron....that trick worked beautifully...thanks so much!
Hi xorcrack,

'Glad I could help! :-)

Greetings,
    </ntr> :)
bita:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:


[split points between neutron and ahosang]


Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.