?
Solved

How to stop the flickering?

Posted on 2001-07-01
12
Medium Priority
?
341 Views
Last Modified: 2009-07-29
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);
}
}
0
Comment
Question by:bita
11 Comments
 
LVL 12

Accepted Solution

by:
ahosang earned 100 total points
ID: 6242186
This is the continuation of your and susan's question, right? As I said in question http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20143864 the screen will flicker with all this activity.
  If you don't use drawString to display the 'win98: true' kind of info, but instead make them labels and use setText method of label class, then you will have much less flickering. Then use drawString only for the scrolling text.
0
 
LVL 92

Expert Comment

by:objects
ID: 6243394
Try adding the following method:

public void update(Graphics g)
{
   paint(g);
}
0
 
LVL 12

Expert Comment

by:ahosang
ID: 6244619
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.
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 4

Assisted Solution

by:Neutron
Neutron earned 100 total points
ID: 6244741
objectses suggestion is close but won't remove flickering completely.

To remove flickering in your applet do this:


- Declare hidden buffer image like this:

  Image backBuffer;

- Put following code in front of your code in paint method:

  Graphics appletGraphics=g;

  if (backBuffer==null)
     backBuffer=createImage( getWidth(), getHeight() );

  g=backBuffer.getGraphics();

  g.setColor( getBackground() );
  g.fillRect( 0, 0, getWidth(), getHeight() );
  g.setColor( Color.black );

- Add following line at the end of paint method (after your code):

  appletGraphics.drawImage( backBuffer, 0, 0, null );

- Override update function in your applet like this:

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

- Say "bye" to flickering ;)

Greetings,
    Ntr:)
0
 
LVL 1

Expert Comment

by:tonus
ID: 6245202
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
0
 
LVL 4

Expert Comment

by:Neutron
ID: 6245241
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:)
0
 
LVL 4

Expert Comment

by:Neutron
ID: 6245242
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:)
0
 
LVL 1

Expert Comment

by:xorcrack
ID: 7126106
neutron....that trick worked beautifully...thanks so much!
0
 
LVL 4

Expert Comment

by:Neutron
ID: 7126340
Hi xorcrack,

'Glad I could help! :-)

Greetings,
    </ntr> :)
0
 

Expert Comment

by:CleanupPing
ID: 9062005
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.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 9253861
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

840 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