Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can someone help me remove flicker from this applet?

Status
Not open for further replies.

cpope

Programmer
Jul 7, 2000
58
US
// messagingClient Class
// UNDER DEVELOPMENT
// Currently Reads All Messages From Server, sends acknowledgement that messages are received
// then loops back and obtains new set of messages

import java.net.*;
import java.io.*;
import java.util.*;
import java.applet.Applet;
import java.awt.*;

public class messagingClient extends Applet implements Runnable
{
Image messageimage;
Graphics messagegraphics;
Thread me;
int xpos;
int ypos;
int textwidth;
String clienttype;
String callcenterid;
Vector messages;
messagingServerInfo server;
Socket connection;
String message;
String endmsg;
int appWidth;
int appHeight;
int FONT_SIZE;
int BK_R;
int BK_G;
int BK_B;
int TXT_R;
int TXT_G;
int TXT_B;
int Y_OFFSET;
String FONT_NAME;
int SPEED;
Color BK;
Color TXT;

public void init()
{
clienttype = getParameter("clienttype");
callcenterid = getParameter("callcenterid");
appWidth = Integer.parseInt(getParameter("appWidth"));
appHeight = Integer.parseInt(getParameter("appHeight"));

setBackground(Color.black);
messageimage = createImage(appWidth, appHeight);
messagegraphics = messageimage.getGraphics();

FONT_SIZE = Integer.parseInt(getParameter("FONT_SIZE"));
FONT_NAME = getParameter("FONT_NAME");
SPEED = Integer.parseInt(getParameter("SPEED"));

Y_OFFSET = appHeight/2 - FONT_SIZE/2;
BK_R = Integer.parseInt(getParameter("BK_R"));
BK_G = Integer.parseInt(getParameter("BK_G"));
BK_B = Integer.parseInt(getParameter("BK_B"));
TXT_R = Integer.parseInt(getParameter("TXT_R"));
TXT_G = Integer.parseInt(getParameter("TXT_G"));
TXT_B = Integer.parseInt(getParameter("TXT_B"));
BK = new Color(BK_R, BK_G, BK_B);
TXT = new Color(TXT_R, TXT_G, TXT_B);

xpos = appWidth + 10;
ypos = Y_OFFSET; // position to look right
messages = new Vector();
server = new messagingServerInfo();
connection = null;
message = "MESSAGES AWAIT!";
endmsg = "---- END OF MESSAGES ----";
me = new Thread(this);
me.start();
}

public void paint(Graphics g)
{
messagegraphics.setColor(BK); // Background colour again
messagegraphics.fillRect(0,0,appWidth + 1, appHeight + 1);
messagegraphics.setColor(TXT); // Text colour here
try
{
messagegraphics.setFont(new Font(FONT_NAME,0,FONT_SIZE));
}
catch(NullPointerException _ex) { }
messagegraphics.drawString(message, xpos, ypos);
g.drawImage(messageimage, 0, 0, this);
}

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

public void run()
{

//connect to Server
try
{
connection = new Socket(server.getServer(), server.getPort());
// System.out.println("Connected To Server");

ObjectOutputStream output = new ObjectOutputStream(connection.getOutputStream());

//SEND CLIENT PARAMETERS TO SERVER - SERVER ALWAYS EXPECTS 2 PARAMETERS FROM CLIENTS
try
{
output.flush();

output.writeObject((String)clienttype);
output.flush();

output.writeObject((String)callcenterid);
output.flush();

}
catch(IOException e)
{
//client closed connection early
}


//READ SERVER MESSAGES
while(true) //CONNECTION IS OPEN
{
ObjectInputStream input = new ObjectInputStream(connection.getInputStream());
while(true) //MESSAGES ARE BEING SENT
{
try
{
message = (String)input.readObject();
//If end of messages occurs, break from reading loop
if(message.equals(endmsg))
break;
messages.add(message);
//System.out.println(message);
}
catch(ClassNotFoundException e)
{
//System.out.println("Unknown Object Sent By Server");
}
} //END WHILE(TRUE) MESSAGES ARE BEING SENT


//**** FUTURE AREA OF SCROLLING MESSAGES FROM VECTOR ACROSS APPLET ****
for(int msgnum=0; msgnum < messages.size(); msgnum++)
{


message = (String) messages.elementAt(msgnum);
textwidth = 7 * message.length();
xpos = appWidth + 10;
while(xpos > (textwidth-2*textwidth))
{
xpos--;

try
{
Thread.sleep(SPEED);
}
catch (InterruptedException _ex) { }

repaint();
}
}

xpos = appWidth + 10;
messages.removeAllElements();

//**** AFTER COMPLETED SENDING ONE 'CYCLE', OUTPUT TO SERVER THAT ****
//**** CLIENT IS READY FOR THE NEXT 'CYCLE' ****
ObjectOutputStream ops = new ObjectOutputStream(connection.getOutputStream());
String ready = &quot;CLIENT READY&quot;;
ops.writeObject((String)ready);
output.flush();

} //END WHILE(TRUE) CONNECTION IS OPEN
}
catch(UnknownHostException e)
{
//System.out.println(&quot;Cannot Find Server&quot;);
//System.out.println(e);
//System.exit(1);
}
catch(IOException e)
{
//System.out.println(&quot;Server Found, but cannot connect to Server&quot;);
//System.out.println(e);
//System.exit(1);
}

} // END RUN

}
 
try putting

super.paint(g);

as the first line in your paint method.
 
:(

Sorry pipk... It's still flickering....

Any other ideas??
 
JRE - PLUG IN 1.3.1 BUG

I found the root of the problem, but as far as I know there is no solution. When attempting to implement double-buffering, I downloaded other applet code from the internet. I just replaced the class name of the downloaded applet into the HTML generated by htmlconverter (generated to utilize the Java Plug-in). The downloaded applet flickered.

Then I just used the regular plain <applet> tags (not using HTMLconverter and plug-in) and the downloaded applet no longer flickered.

The applet above uses functions that require the JRE plug-in, so as far as I know there is not a fix for this, maybe in 1.4. Please let me know if there is a work around for this problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top