Something different..........

I'm creating an animation by moving a same picture some pixels after every 80 milliseconds.

my problem is flickering. I have tried double buffering but i dont know wheather i can use it in this case as in every example of double buffering i've seen drawing components at offscreen.

I've overridden update() method and acc. to double buffering I have tried getImage(); in offscreen but it doesnt solve my problem.I'm working on it and only this is left in my project.

Will somebody help me out with a bit of code.I'll be very thankful to him.

[586 byte] By [bontz] at [2007-9-26 2:23:35]
# 1
If you could post the code that gets run ever 80 miliseconds I might be able to see the problem. Right now we have no idea what you are doing in your code.
dubwai at 2007-6-29 9:31:50 > top of Java-index,Archived Forums,Java Programming...
# 2
The whole idea of double buffering is first rendering the image to an off-screen image which can then be painted on screen fast.It's difficult to suggest anything without seeing your code...You could find http://www.cfxweb.net/article.php?sid=169 worth reading.
jsalonen at 2007-6-29 9:31:50 > top of Java-index,Archived Forums,Java Programming...
# 3
The whole idea of double buffering is first rendering the image to an off-screen image which can then be painted on screen fast.It's difficult to suggest anything without seeing your code...You could find http://www.cfxweb.net/article.php?sid=169 worth reading.
jsalonen at 2007-6-29 9:31:50 > top of Java-index,Archived Forums,Java Programming...
# 4
The whole idea of double buffering is first rendering the image to an off-screen image which can then be painted on screen fast.It's difficult to suggest anything without seeing your code...You could find http://www.cfxweb.net/article.php?sid=169 worth reading.
jsalonen at 2007-6-29 9:31:50 > top of Java-index,Archived Forums,Java Programming...
# 5

its very very long

/*

<applet code="Dynamics" width=800 height=600>

<param name="img1" value="cart.gif">

<param name="img2" value="mass.gif">

<param name="img3" value="backimg.gif">

</applet>

*/

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class Dynamics extends Applet implements Runnable,ActionListener,ItemListener

{

Image img1,img2,img3;

Graphics gr;

Thread t=null;

int accQ,fg;

int[] Af=new int[20];

int[] Am=new int[20];

int[] Aa=new int[20];

int[] AA=new int[20];

double[] fAf=new double[20];

double[] fAm=new double[20];

double[] fAa=new double[20];

double[] fAA=new double[20];

int a=260,b=130,x=522,y=200,i=373,j=200;

int Wc=500,Wm=200,ai=0;

int xGraph=600,yGraph=600;

double Mtot,F,acc;

boolean stopflag,fuWc=false,flWc=true,fuWm=false,flWm=true,fmain=true,fgraph=false,ftable=false,fexit=false;

Label LWc,LWm,La;

Button start,stop,reset,help,BWc,BWc1,BWm,BWm1,graphs,table;

Button exit;

Choice gr1,gr2;

Frame f;

String str1,str2;

public void init()

{

setLayout(null);

setBackground(Color.white);

img1=getImage(getDocumentBase(),getParameter("img1"));

img2=getImage(getDocumentBase(),getParameter("img2"));

img3=getImage(getDocumentBase(),getParameter("img3"));

stopflag=false;

start=new Button("Start");

start.setBounds(275,50,60,30);

stop=new Button("Pause");

stop.setBounds(350,50,60,30);

reset= new Button("Reset");

reset.setBounds(425,50,60,30);

help = new Button("Help");

help.setBounds(500,50,60,30);

BWc=new Button("Add 50 Grams");

BWc.setBounds(185,220,120,25);

BWc1=new Button("Reduce 50 Grams");

BWc1.setBounds(185,245,120,25);

BWm=new Button("Add 100 Grams");

BWm.setBounds(387,320,120,25);

BWm1=new Button("Reduce 100 Grams");

BWm1.setBounds(387,345,120,25);

graphs=new Button("Graphs");

graphs.setBounds(65,300,60,30);

table=new Button("Table");

table.setBounds(65,330,60,30);

exit=new Button("Exit");

exit.setBounds(510,650,60,30);

LWc=new Label();

LWc.setBounds(180,200,125,20);

LWc.setBackground(Color.white);

LWm=new Label();

LWm.setBounds(380,300,130,20);

LWm.setBackground(Color.white);

La=new Label();

La.setBounds(120,115,40,18);

La.setBackground(Color.white);

gr1=new Choice();

gr1.setBounds(150,650,100,25);

gr2=new Choice();

gr2.setBounds(300,650,100,25);

gr1.add("Force");

gr2.add("Force");

gr1.add("Mass");

gr2.add("Mass");

gr1.add("Acceleration");

gr2.add("Acceleration");

gr1.add("1/Acceleration");

gr2.add("1/Acceleration");

cal(Wc,Wm);

add(start);

add(stop);

add(reset);

add(help);

add(BWc);

add(BWc1);

add(BWm);

add(BWm1);

add(LWc);

add(LWm);

add(La);

add(graphs);

add(table);

add(gr1);

add(gr2);

add(exit);

start.addActionListener(this);

stop.addActionListener(this);

reset.addActionListener(this);

help.addActionListener(this);

BWc.addActionListener(this);

BWc1.addActionListener(this);

BWm.addActionListener(this);

BWm1.addActionListener(this);

graphs.addActionListener(this);

table.addActionListener(this);

exit.addActionListener(this);

gr1.addItemListener(this);

gr2.addItemListener(this);

//hidemain(fmain);

hidegraph(fgraph);

}

public void cal(int c,int m)

{

if(Wc>=1000){fuWc=true;}

else fuWc=false;

if(Wc<=500){flWc=true;}

else flWc=false;

if(Wm>=1000){fuWm=true;}

else fuWm=false;

if(Wm<=200){flWm=true;}

else flWm=false;

BWc.setEnabled(!fuWc);

BWc1.setEnabled(!flWc);

BWm.setEnabled(!fuWm);

BWm1.setEnabled(!flWm);

this.Mtot = (m+c)/1000.0;

this.F=m/1000.0*9.81;

this.acc=this.F/this.Mtot;

accQ=(int)((700-acc*100)/4);

LWc.setText("Wieght on Cart: "+Wc+" g");

LWm.setText("Wieght of Mass: "+Wm+" g");

//La.setText((""+acc).substring(0,4)+" m/s2");

//TWc.setText("Wieght on Cart is "+Wc+" Grammes");

//TWm.setText("Wieght of Mass is "+Wm+" Grammes");

//Ta.setText("Acceleration = "+(""+acc).substring(0,4)+" m/s");

}

/*public void start()

{

Graphics g=getGraphics();

g.drawImage(img3,0,0,this);

}

*/

public void run()

{

while(a<=390)

{

try{

a++;y++;i++;j++;

repaint();

Thread.sleep(accQ);

if(stopflag) break;

}catch(InterruptedException e){}

}

}

public void paint(Graphics g)

{

if(fmain==true)

{

fg=0;

g.drawImage(img3,0,0,this);

g.drawImage(img1,a,b,this);

g.drawImage(img2,x,y,this);

g.drawLine(i,148,525,148);

g.drawLine(544,164,544,j);

}

else if(fgraph==true)

{

g.setColor(Color.lightGray);

for(int i=15;i<=xGraph;i+=15)

g.drawLine(i,0,i,yGraph);

for(int j=15;j<=yGraph;j+=15)

g.drawLine(0,j,xGraph,j);

g.setColor(Color.black);

g.drawLine(0,0,xGraph,0);

g.drawLine(0,0,0,yGraph);

g.drawLine(0,yGraph,xGraph,yGraph);

g.drawLine(xGraph,0,xGraph,yGraph);

switch(fg)

{

case 0: break;

case 1: {double ix=.4905,iy=.10;makeGraph(Af,Am,fAf,fAm,ix,iy);break;}

case 2: {double ix=.4905,iy=.327;makeGraph(Af,Aa,fAf,fAa,ix,iy);break;}

case 3: {double ix=.4905,iy=.0306;makeGraph(Af,AA,fAf,fAA,ix,iy);break;}

case 4: {double ix=.10,iy=.327;makeGraph(Am,Aa,fAm,fAa,ix,iy);break;}

case 5: {double ix=.10,iy=.0306;makeGraph(Am,AA,fAm,fAA,ix,iy);break;}

case 6: {double ix=.327,iy=.0306;makeGraph(Aa,AA,fAa,fAA,ix,iy);break;}

default: g.drawString("Data Mismatch",250,250);

}

}

else if(ftable==true)

{

//Font f=new Font("Dialog",Font.BOLD,16);

//setFont(f);

double a=0.0,b=0.0,c=0.0,d=0.0;

String str1,str2,str3,str4;

int j=55,k=0;

//g.drawString("hellow you're in table",20,20);

g.setColor(Color.lightGray);

g.drawLine(0,50,650,50);

g.drawLine(50,0,50,700);

g.setColor(Color.black);

g.drawString("Force (N)",100,45);

g.drawString("Total Mass (Kg)",250,45);

g.drawString("Acceleration (m/s2)",400,45);

g.drawString("1/Acceleration",550,45);

for(int i=0;i<20;i++)

{

j+=15;k++;

a=fAf;str1=(a+"").substring(0,7);

b=fAm;str2=(b+"").substring(0,7);

c=fAa;str3=(c+"").substring(0,7);

d=fAA;str4=(d+"").substring(0,7);

if(a==0.0||b==0.0||c==0.0||d==0.0) break;

if(a==fAf[i+1] && b==fAm[i+1]) {j-=15;k--;}

else

{

g.drawString((""+k),35,j);

g.drawString(str1,100,j);

g.drawString(str2,250,j);

g.drawString(str3,400,j);

g.drawString(str4,550,j);

}

}

}

}

public void update(Graphics g)

{

paint(g);

}

public void hidemain(boolean fmain)

{

start.setVisible(fmain);

stop.setVisible(fmain);

reset.setVisible(fmain);

help.setVisible(fmain);

BWc.setVisible(fmain);

BWc1.setVisible(fmain);

BWm.setVisible(fmain);

BWm1.setVisible(fmain);

LWc.setVisible(fmain);

LWm.setVisible(fmain);

La.setVisible(fmain);

graphs.setVisible(fmain);

table.setVisible(fmain);

}

public void hidegraph(boolean fgraph)

{

exit.setVisible(fexit);

gr1.setVisible(fgraph);

gr2.setVisible(fgraph);

}

public void hide(boolean fmain,boolean fgraph)

{

hidegraph(fgraph);

hidemain(fmain);

Graphics g=getGraphics();

g.setColor(Color.white);

g.fillRect(0,0,800,700);

}

public void makeGraph(int[] arr1,int[] arr2,double[] ar1,double[] ar2,double ix,double iy)

{

int a=0;int b=0;

double da=0.0,db=0.0;

double fx=0.0,fy=0.0;

String str1="",str2="";

Graphics g=getGraphics();

for(int k=0;k<=20;k++)

{

a=arr1[k];b=arr2[k];

da=ar1[k];db=ar2[k];

str1=(da+"").substring(0,3);

str2=(db+"").substring(0,4);

int c=arr1[k+1];int d=arr2[k+1];

if(a==0||b==0||c==0||d==0) break;

g.drawLine(a,yGraph-b,c,yGraph-d);

g.setColor(Color.red);

g.fillOval(a-2,yGraph-2-b,4,4);

g.setColor(Color.black);

g.drawString(("x="+str1+" y="+str2),a,yGraph-b);

}

g.drawString(("x="+str1+" y="+str2),a,yGraph-b);

g.setColor(Color.red);

g.fillOval(a-2,yGraph-2-b,4,4);

g.setColor(Color.black);

for(int i=0;i<xGraph;i+=30)

{

g.drawString(((fx+"").substring(0,4)),i,yGraph+10);

fx+=ix;

}

for(int i=0;i<yGraph;i+=30)

{

g.drawString(((fy+"").substring(0,4)),xGraph+5,yGraph-i+5);

fy+=iy;

}

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getActionCommand().equals("Start"))

{

stopflag=false;

Af[ai]=(int)(F*61.162);

Am[ai]=(int)(Mtot*300);

Aa[ai]=(int)(acc*91.743);

AA[ai]=(int)(1/acc*980.39);

fAf[ai]=F;

fAm[ai]=Mtot;

fAa[ai]=acc;

fAA[ai]=1/acc;

t=new Thread(this);

t.start();

La.setText((""+acc).substring(0,4));

BWc.setEnabled(false);

BWc1.setEnabled(false);

BWm.setEnabled(false);

BWm1.setEnabled(false);

ai++;

System.out.println("M= "+Am[ai]+" acc= "+Aa[ai]+" F= "+Af[ai]+" 1/acc= "+AA[ai]+" ai= "+ai);

}

else if(ae.getActionCommand().equals("Pause"))

{

stopflag=true;

cal(Wc,Wm);

}

else if(ae.getActionCommand().equals("Reset"))

{

La.setText("");

stopflag=true;

i=373;j=200;

a=260;y=200;

Wc=500;Wm=200;

cal(Wc,Wm);

repaint();

}

else if(ae.getActionCommand().equals("Help"))

{f=new Help();

f.setSize(400,400);

f.setVisible(true);

}

else if(ae.getActionCommand().equals("Add 50 Grams"))

{

if(Wc><1000) Wc+=50;

cal(Wc,Wm);

}

else if(ae.getActionCommand().equals("Reduce 50 Grams"))

{

if(Wc>500) Wc-=50;

cal(Wc,Wm);

}

else if(ae.getActionCommand().equals("Add 100 Grams"))

{

if(Wm<1000) Wm+=100;

cal(Wc,Wm);

}

else if(ae.getActionCommand().equals("Reduce 100 Grams"))

{

if(Wm>200) Wm-=100;

cal(Wc,Wm);

}

else if(ae.getActionCommand().equals("Graphs"))

{

fmain=false;

fgraph=true;

fexit=true;

hide(fmain,fgraph);

repaint();

}

else if(ae.getActionCommand().equals("Table"))

{

System.out.println("Table clicked");

fmain=false;

//fgraph=false;

ftable=true;

fexit=true;

hide(fmain,fgraph);

repaint();

}

else if(ae.getActionCommand().equals("Exit"))

{

//System.out.println("exit called");

fgraph=false;

ftable=false;

fexit=false;

fmain=true;

setFont(null);

hide(fmain,fgraph);

repaint();

}

}

public void itemStateChanged(ItemEvent ie)

{

String str1=gr1.getSelectedItem();

String str2=gr2.getSelectedItem();

if(str1=="Force" && str2=="Mass")

{

hide(fmain,fgraph);

fg=1;

repaint();

}

else if(str1=="Force" && str2=="Acceleration")

{

hide(fmain,fgraph);

fg=2;

repaint();

}

else if(str1=="Force" && str2=="1/Acceleration")

{

hide(fmain,fgraph);

fg=3;

repaint();

}

else if(str1=="Mass" && str2=="Acceleration")

{

hide(fmain,fgraph);

fg=4;

repaint();

}

else if(str1=="Mass" && str2=="1/Acceleration")

{

hide(fmain,fgraph);

fg=5;

repaint();

}

else if(str1=="Acceleration" && str2=="1/Acceleration")

{

hide(fmain,fgraph);

fg=6;

repaint();

}

else

{

System.out.println("data mismatch");

hide(fmain,fgraph);

fg=100;

repaint();

}

System.out.println("Item state changed. gr1 = "+str1+" gr2= "+str2);

}

}

bontz at 2007-6-29 9:31:50 > top of Java-index,Archived Forums,Java Programming...
# 6
Sorry about that multiple posting, something weird was going on with the JSP, server or my browser...
jsalonen at 2007-6-29 9:31:50 > top of Java-index,Archived Forums,Java Programming...
# 7

Well, did you try double buffering? Add the following instance vars:

Image offI;

Graphics offG;

add this to init():

offI = createImage(size().width, size().height);

offG = offI.getGraphics();

and modify paint():

public void paint(Graphics grr) {

Graphics g = offG;

// rest of the code untouched...

grr.drawImage(offI, 0,0,null);

}

jsalonen at 2007-6-29 9:31:50 > top of Java-index,Archived Forums,Java Programming...
# 8

I've already tried CFXWEB ........but you see there's only an example of double buffering in which he uses offscreen image to paint something like lines or rectangels. but here I've to get the same image(alraedy present in same folder) to show it few pixels away every few milliseconds.

Thanks for your anticipation

bontz at 2007-6-29 9:31:50 > top of Java-index,Archived Forums,Java Programming...
# 9
Actually, as far as I can tell, you are painting a lot more than one image (what are all those drawString()s?) ... even in that case double buffering would make it faster.
jsalonen at 2007-6-29 9:31:51 > top of Java-index,Archived Forums,Java Programming...
# 10

you're getting the wrong idea man...........

I'm using the flags.

let me clear the picture.

on screen initially(when fmain is true and everything is false).......its just 3 pictures.one is constant and being used as background(img3). other 2 (img 1 and img2 ) are moving at the click of start button.a run method is called which starts a new thread which sleeps for few milliseconds inbetween and then move the same picture 1 pixel.

I've tried what you've written in the code but didnt work.

bontz at 2007-6-29 9:31:51 > top of Java-index,Archived Forums,Java Programming...
# 11

Just noticed something important in your paint method:

> g.drawImage(img3,0,0,this);

> g.drawImage(img1,a,b,this);

> g.drawImage(img2,x,y,this);

Try changing 'this' to null. Right now, when one image gets finished the component is refreshed. This can cause flickering because the other images will then be redrawn - and when they've been fully drawn the component will be refreshed and so on.

jsalonen at 2007-6-29 9:31:51 > top of Java-index,Archived Forums,Java Programming...