Simple script annimation problem usig swing

T

Thomas

Hi I 'm writing a simple java applet similar to famous game life, I' m
trying to run it throught browser html script, but the animation does not
work. It only shos it beginning state. Don't know whats wrong with it, simce
the method repaint() in New Class is surly invoked, because the console puts
"refreshing ..." before entering the method repaint (); and the applet does
change it state. It is not a problem with deadlocks neither. The essential
code is in the Main.java and NewClass.java class.
/*********************************************/
the html script :
<applet
code=javaapplication2\Main.class

width=10000

height=10000>
<param name=fps value=20>
</applet>
/*******************************************/
/*
* Main.java
*
* Created on 7 grudzieñ 2007, 17:25
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
import java.swing.*; */


package javaapplication2;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author uracyl
*/
public class Main extends JApplet implements ActionListener{
Piaskownica pias;
javax.swing.Timer timer;
JPanel panel = new JPanel();
/** Creates a new instance of Main */
public void init(){
timer = new javax.swing.Timer(30,this);
pias = new Piaskownica(this);
NewClass nc = new NewClass(pias);
getContentPane().add(nc);
//TimerTask task = new My_task(this);
//timer.schedule(task,30);
}
public void destroy(){
}
public void stop(){
}
public void start(){
timer = new javax.swing.Timer(30,this);
pias = new Piaskownica(this);


NewClass nc = new NewClass(pias);
getContentPane().add(nc);
//TimerTask task = new My_task(this);
//timer.schedule(task,30);
timer.start();
}


public void actionPerformed(ActionEvent e){

System.out.println("refreshing ....");
repaint();
}
}
/***********************************************/
NewClass, witch implements the repainting the applet :
/***********************************************/


package javaapplication2;
import javax.swing.*;
import java.awt.*;
/**
*
* @author uracyl
*/
public class NewClass extends JPanel {
Piaskownica pias;
/** Creates a new instance of NewClass */
public NewClass(Piaskownica r) {
pias = r;
}
public void paintComponent(Graphics g){
int i,j;
//System.out.println(pias.liczba_droz);
g.clearRect(0,0,pias.liczba_droz * 20,pias.liczba_droz *20);
for(i=0;i<pias.liczba_droz - 2;i++){
for(j=0;j<pias.liczba_droz - 2;j++){
switch(pias.Krata[j].cstate){
case pusta:
//System.out.println(i +" "+ j);
g.setColor(new Color(0,0,0));
g.fillRect(pias.Krata[j].x * 20 ,pias.Krata[j].y * 20
,Drozdze.length,Drozdze.length);
break;
case drozdze:
//System.out.println(i +" "+ j);
g.setColor(new Color(128,0,0));
g.fillRect(pias.Krata[j].x * 20 ,pias.Krata[j].y * 20
,Drozdze.length,Drozdze.length);
break;
case bakteria:
//System.out.println(i +" "+ j);
g.setColor(new Color(0,128,0));
g.fillRect(pias.Krata[j].x * 20 ,pias.Krata[j].y * 20
,Drozdze.length,Drozdze.length);
break;
case bakteria_drozdz:
//System.out.println(i +" "+ j);
g.setColor(new Color(128,128,0));
g.fillRect(pias.Krata[j].x * 20 ,pias.Krata[j].y * 20
,Drozdze.length,Drozdze.length);
break;
}
}
}

}

}
/**************************************************************************/
//Drozdze java represanting a single tile in an applet's gird:

/*
*
*
* Created on 7 grudzieñ 2007, 17:28
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package javaapplication2;
import java.awt.*;
/**
*
* @author uracyl
*/

enum state {pusta,drozdze,bakteria,bakteria_drozdz}
enum action{kill,respawn}
public class Drozdze extends Thread{

public state cstate;
public static int respawn = 20;
private Bakteria bakteria;
private Piaskownica piask;
int x,y;
static int length = 10;
/** Creates a new instance of NewClass */
public Drozdze(int x1,int y1,Piaskownica p) {
cstate = state.drozdze;
bakteria = null;
piask = p;
x = x1;
y = y1;
}
public synchronized void run(){
int i = 0;
try{
while(true){
if(cstate == state.pusta && sasiadzi()){
grow();
}
wait(respawn);
}
}catch (java.lang.InterruptedException IE){};


}
public boolean sasiadzi(){
return sasiad(x+1,y+1) || sasiad(x+1,y-1) || sasiad(x+1,y) ||
sasiad(x,y+1)
|| sasiad(x,y-1) || sasiad(x-1,y-1) || sasiad(x-1,y) || sasiad(x-1,y+1);
}
public boolean sasiad(int i, int j){
if(i < 0 || j < 0 )
return false;
else if(i > piask.liczba_droz || j > piask.liczba_droz )
return false;
else
synchronized(piask.Krata[x][y]){
return piask.Krata[x][y].cstate == state.drozdze;
}
}
public state get_state(){
return cstate;
}


public void set_state(state dstate){
cstate = dstate;
}
public void grow(){
if(cstate == state.bakteria)
cstate = state.bakteria_drozdz;
else if(cstate == state.pusta)
cstate = state.drozdze;
}
public void dodaj_bakterie(){
if(cstate == state.pusta)
cstate = state.bakteria;
else if(cstate == state.drozdze){
cstate = state.bakteria_drozdz;
}
}

public void usun_bakterie(){
if(cstate == state.bakteria)
cstate = state.pusta;
else if(cstate == state.bakteria_drozdz){
cstate = state.drozdze;
}
}

}



/**************************************************************************/
//simple class simulating the bacteria - a being witch should live on an
applet :
/*
* Bakteria.java
*
* Created on 7 grudzieñ 2007, 17:56
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package javaapplication2;
/**
*
* @author uracyl
*/
import java.util.*;
public class Bakteria extends Thread{
private static Random rand = new Random(2);
public static Piaskownica ref;
public static int interval = 20;
public static int interval2 = 10;
int glod;
int x,y;
Node node;
/** Creates a new instance of Bakteria */
public Bakteria(int x1,int y1,Node n) {
x = x1;
y = y1;
glod = 0;
node = n;
ref.Krata[x][y].dodaj_bakterie();

}
public synchronized void run(){
Node nowy;
try{
while(!Thread.interrupted()){
System.out.println("BAkteria " + x + " " + y + " " + glod);
if(glod == -3){
this.interrupt();
System.out.println("Konice BAkterii " );
}
else if(glod == 3){
synchronized(node){
glod = 0;
nowy = new Node(++x,++y);
nowy.next = this.node.next;
nowy.prev = this.node;
node.next = nowy;
System.out.println("respawn " );
wait(interval);

}
}
else {
synchronized(ref.Krata[x][y]){
if (ref.Krata[x][y].get_state()==state.bakteria){
ref.Krata[x][y].set_state(state.pusta);
x+= (rand.nextInt()%3) -1 ;
y+= (rand.nextInt()%3) -1;
synchronized(ref.Krata[x][y]){
ref.Krata[x][y].dodaj_bakterie();
glod--;
System.out.println("B--" + glod);
wait(interval2);
}
} else
if(ref.Krata[x][y].get_state()==state.bakteria_drozdz){
ref.Krata[x][y].set_state(state.pusta);
x+= (rand.nextInt()%3) -1 ;
y+= (rand.nextInt()%3) -1;
synchronized(ref.Krata[x][y]){
ref.Krata[x][y].dodaj_bakterie();
glod++;
System.out.println("B++" + glod);
wait(interval2);
}
}


}
}

}
}catch (java.lang.InterruptedException IE){};
}
}
/***************************************************************/
//a class Node, witch represents a single Node in a list whitch holds a
references for all the bacterias
//script:

/*
* Node.java
*
* Created on 21 grudzieñ 2007, 01:40
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package javaapplication2;

/**
*
* @author tom
*/
public class Node {
Bakteria b;
public Node next;
public Node prev;
/** Creates a new instance of Node */
public Node(int x,int y ) {
b = new Bakteria(x,y,this) ;
b.start();
}
}
/*********************************************************************/
//and finally Piaskownica.class witch means a sandbox where we holds all
needed
//simulation's for a single game:
/*
* Piaskownica.java
*
* Created on 7 grudzieñ 2007, 17:39
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package javaapplication2;
import javax.swing.*;
import java.awt.*;
import java.util.*;

/**
*
* @author uracyl
*/
public class Piaskownica {
public JApplet panel;
public static int liczba_bakteri = 30;
public final int liczba_droz = 20;
public Drozdze Krata [][] ;
public Node lista,current;
/** Creates a new instance of Piaskownica */
public Piaskownica( JApplet p) {
Bakteria.ref = this;
panel = p;
int i, j;


Krata = new Drozdze [liczba_droz] [liczba_droz];
for(i=0;i<liczba_droz - 2;i++)
for(j=0;j<liczba_droz - 2;j++){
System.out.println(i + " " + j);
Krata[j] = new Drozdze(i,j,this);
//Krata[j].start();
}

current = lista = new Node(10,10);

i =1 ;


}
}
/****************************************************************/
 
T

Thomas

U¿ytkownik "Thomas" <[email protected]> napisa³ w wiadomo¶ci

Ehh i don't expect anyone to read the whole code, but to check why the
animation fails.
Maybe it is because of running it throught browser.
Hi I 'm writing a simple java applet similar to famous game life, I' m
trying to run it throught browser html script, but the animation does not
work. It only shos it beginning state. Don't know whats wrong with it, simce
the method repaint() in New Class is surly invoked, because the console puts
"refreshing ..." before entering the method repaint (); and the applet does
change it state. It is not a problem with deadlocks neither. The essential
code is in the Main.java and NewClass.java class.
/*********************************************/
the html script :
<applet
code=javaapplication2\Main.class

width=10000

height=10000>
<param name=fps value=20>
</applet>
/*******************************************/
/*
* Main.java
*
* Created on 7 grudzieñ 2007, 17:25
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
import java.swing.*; */


package javaapplication2;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author uracyl
*/
public class Main extends JApplet implements ActionListener{
Piaskownica pias;
javax.swing.Timer timer;
JPanel panel = new JPanel();
/** Creates a new instance of Main */
public void init(){
timer = new javax.swing.Timer(30,this);
pias = new Piaskownica(this);
NewClass nc = new NewClass(pias);
getContentPane().add(nc);
//TimerTask task = new My_task(this);
//timer.schedule(task,30);
}
public void destroy(){
}
public void stop(){
}
public void start(){
timer = new javax.swing.Timer(30,this);
pias = new Piaskownica(this);


NewClass nc = new NewClass(pias);
getContentPane().add(nc);
//TimerTask task = new My_task(this);
//timer.schedule(task,30);
timer.start();
}


public void actionPerformed(ActionEvent e){

System.out.println("refreshing ....");
repaint();
}
}
/***********************************************/
NewClass, witch implements the repainting the applet :
/***********************************************/


package javaapplication2;
import javax.swing.*;
import java.awt.*;
/**
*
* @author uracyl
*/
public class NewClass extends JPanel {
Piaskownica pias;
/** Creates a new instance of NewClass */
public NewClass(Piaskownica r) {
pias = r;
}
public void paintComponent(Graphics g){
int i,j;
//System.out.println(pias.liczba_droz);
g.clearRect(0,0,pias.liczba_droz * 20,pias.liczba_droz *20);
for(i=0;i<pias.liczba_droz - 2;i++){
for(j=0;j<pias.liczba_droz - 2;j++){
switch(pias.Krata[j].cstate){
case pusta:
//System.out.println(i +" "+ j);
g.setColor(new Color(0,0,0));
g.fillRect(pias.Krata[j].x * 20 ,pias.Krata[j].y * 20
,Drozdze.length,Drozdze.length);
break;
case drozdze:
//System.out.println(i +" "+ j);
g.setColor(new Color(128,0,0));
g.fillRect(pias.Krata[j].x * 20 ,pias.Krata[j].y * 20
,Drozdze.length,Drozdze.length);
break;
case bakteria:
//System.out.println(i +" "+ j);
g.setColor(new Color(0,128,0));
g.fillRect(pias.Krata[j].x * 20 ,pias.Krata[j].y * 20
,Drozdze.length,Drozdze.length);
break;
case bakteria_drozdz:
//System.out.println(i +" "+ j);
g.setColor(new Color(128,128,0));
g.fillRect(pias.Krata[j].x * 20 ,pias.Krata[j].y * 20
,Drozdze.length,Drozdze.length);
break;
}
}
}

}

}
/**************************************************************************/
//Drozdze java represanting a single tile in an applet's gird:

/*
*
*
* Created on 7 grudzieñ 2007, 17:28
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package javaapplication2;
import java.awt.*;
/**
*
* @author uracyl
*/

enum state {pusta,drozdze,bakteria,bakteria_drozdz}
enum action{kill,respawn}
public class Drozdze extends Thread{

public state cstate;
public static int respawn = 20;
private Bakteria bakteria;
private Piaskownica piask;
int x,y;
static int length = 10;
/** Creates a new instance of NewClass */
public Drozdze(int x1,int y1,Piaskownica p) {
cstate = state.drozdze;
bakteria = null;
piask = p;
x = x1;
y = y1;
}
public synchronized void run(){
int i = 0;
try{
while(true){
if(cstate == state.pusta && sasiadzi()){
grow();
}
wait(respawn);
}
}catch (java.lang.InterruptedException IE){};


}
public boolean sasiadzi(){
return sasiad(x+1,y+1) || sasiad(x+1,y-1) || sasiad(x+1,y) ||
sasiad(x,y+1)
|| sasiad(x,y-1) || sasiad(x-1,y-1) || sasiad(x-1,y) || sasiad(x-1,y+1);
}
public boolean sasiad(int i, int j){
if(i < 0 || j < 0 )
return false;
else if(i > piask.liczba_droz || j > piask.liczba_droz )
return false;
else
synchronized(piask.Krata[x][y]){
return piask.Krata[x][y].cstate == state.drozdze;
}
}
public state get_state(){
return cstate;
}


public void set_state(state dstate){
cstate = dstate;
}
public void grow(){
if(cstate == state.bakteria)
cstate = state.bakteria_drozdz;
else if(cstate == state.pusta)
cstate = state.drozdze;
}
public void dodaj_bakterie(){
if(cstate == state.pusta)
cstate = state.bakteria;
else if(cstate == state.drozdze){
cstate = state.bakteria_drozdz;
}
}

public void usun_bakterie(){
if(cstate == state.bakteria)
cstate = state.pusta;
else if(cstate == state.bakteria_drozdz){
cstate = state.drozdze;
}
}

}



/**************************************************************************/
//simple class simulating the bacteria - a being witch should live on an
applet :
/*
* Bakteria.java
*
* Created on 7 grudzieñ 2007, 17:56
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package javaapplication2;
/**
*
* @author uracyl
*/
import java.util.*;
public class Bakteria extends Thread{
private static Random rand = new Random(2);
public static Piaskownica ref;
public static int interval = 20;
public static int interval2 = 10;
int glod;
int x,y;
Node node;
/** Creates a new instance of Bakteria */
public Bakteria(int x1,int y1,Node n) {
x = x1;
y = y1;
glod = 0;
node = n;
ref.Krata[x][y].dodaj_bakterie();

}
public synchronized void run(){
Node nowy;
try{
while(!Thread.interrupted()){
System.out.println("BAkteria " + x + " " + y + " " + glod);
if(glod == -3){
this.interrupt();
System.out.println("Konice BAkterii " );
}
else if(glod == 3){
synchronized(node){
glod = 0;
nowy = new Node(++x,++y);
nowy.next = this.node.next;
nowy.prev = this.node;
node.next = nowy;
System.out.println("respawn " );
wait(interval);

}
}
else {
synchronized(ref.Krata[x][y]){
if (ref.Krata[x][y].get_state()==state.bakteria){
ref.Krata[x][y].set_state(state.pusta);
x+= (rand.nextInt()%3) -1 ;
y+= (rand.nextInt()%3) -1;
synchronized(ref.Krata[x][y]){
ref.Krata[x][y].dodaj_bakterie();
glod--;
System.out.println("B--" + glod);
wait(interval2);
}
} else
if(ref.Krata[x][y].get_state()==state.bakteria_drozdz){
ref.Krata[x][y].set_state(state.pusta);
x+= (rand.nextInt()%3) -1 ;
y+= (rand.nextInt()%3) -1;
synchronized(ref.Krata[x][y]){
ref.Krata[x][y].dodaj_bakterie();
glod++;
System.out.println("B++" + glod);
wait(interval2);
}
}


}
}

}
}catch (java.lang.InterruptedException IE){};
}
}
/***************************************************************/
//a class Node, witch represents a single Node in a list whitch holds a
references for all the bacterias
//script:

/*
* Node.java
*
* Created on 21 grudzieñ 2007, 01:40
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package javaapplication2;

/**
*
* @author tom
*/
public class Node {
Bakteria b;
public Node next;
public Node prev;
/** Creates a new instance of Node */
public Node(int x,int y ) {
b = new Bakteria(x,y,this) ;
b.start();
}
}
/*********************************************************************/
//and finally Piaskownica.class witch means a sandbox where we holds all
needed
//simulation's for a single game:
/*
* Piaskownica.java
*
* Created on 7 grudzieñ 2007, 17:39
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package javaapplication2;
import javax.swing.*;
import java.awt.*;
import java.util.*;

/**
*
* @author uracyl
*/
public class Piaskownica {
public JApplet panel;
public static int liczba_bakteri = 30;
public final int liczba_droz = 20;
public Drozdze Krata [][] ;
public Node lista,current;
/** Creates a new instance of Piaskownica */
public Piaskownica( JApplet p) {
Bakteria.ref = this;
panel = p;
int i, j;


Krata = new Drozdze [liczba_droz] [liczba_droz];
for(i=0;i<liczba_droz - 2;i++)
for(j=0;j<liczba_droz - 2;j++){
System.out.println(i + " " + j);
Krata[j] = new Drozdze(i,j,this);
//Krata[j].start();
}

current = lista = new Node(10,10);

i =1 ;


}
}
/****************************************************************/
 
A

Andrew Thompson

Thomas said:
Hi I 'm writing a simple java applet..

GUId projects are more complicated than the equivalent
wiothout a GUI. Appltes are a lot more complicated to develop
and deploy (successfully) than applications.

That is why I say. You are wrong, there is no such thing
as a 'simple java applet'.
.. similar to famous game life, I' m
trying to run it throught browser html script, but the animation does not
work.

Once I wrestled that code into an SSCCE*, I ran it
(in a JOptionPane, rather than a browser) to see a single
row of static squares, and no output besides a bunch
of stuff (numbers) dumped to the System.out.

I suspect the code is blocking the EDT, but as the other
responder alluded, I was not keen to dive back in to the
380+ lines of code to discover where it was.

General tips.
- Put GUIs aside for a while
- Avoid applets for a long, *long* time.
- When posting code, post an SSCCE, rather than 380+ lines
of code spread across a number of public classes (by demoting
them to 'default access' I am currently running them from a single
'Main.java').

* said:
/*********************************************/
the html script :

Note also that HTML is a 'mark-up' language, not a scripting
language.

--
Andrew Thompson
http://www.physci.org/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200712/1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,723
Latest member
TwilaTarde

Latest Threads

Top