Re: Not sure why runs but does not display I think my thread



This might be the FCS:
---------------------------------------------------------------------
<HTML><BODY>
<APPLET CODE="StarField" WIDTH="800" HEIGHT="600></APPLET>
</BODY></HTML>
---------------------------------------------------------------------
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class StarField extends Applet implements Runnable{
boolean initializing = true;
boolean done = false;
boolean start = false;
boolean go = false;
float starRadius;
String name ="Andrew Titus";

Thread starThread;
double star_dx, star_dy;
Image doubleBuffer;
Graphics dbG;
Image bgImage;
Font creator;
FontMetrics fm;
Rectangle walls;
//They're all rectangles.
Point starPoint;
Point mouse;

int[] xpointsOffsets = {0, 6, 24, 9, 15, 0, -15, -9, -24, -6};
int[] ypointsOffsets = {-25, -8, -8, 3, 20, 9, 20, 3, -8, -8};
int npoints = 10;
int[] xpoints;
int[] ypoints;

MediaTracker mt;

Polygon star;

public void init(){
int x, y;

xpoints = new int[npoints];
ypoints = new int[npoints];

starPoint = new Point();
mouse = new Point(0, 0);

mt = new MediaTracker(this);
bgImage = getImage(getCodeBase(),"charb.png");
mt.addImage(bgImage, 0);

//Font to display creator
creator = new Font("Impact", Font.BOLD, (int)(getHeight() * 0.10));

//Walls inside applet bounds
x = (int)(0.01 * getWidth());
y = (int)(0.01 * getHeight());
walls = new Rectangle(x, y, getWidth() - 2 * x, getHeight() - 2 *
y);

addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me){
if (start == false){
go = true;
startStarField();
}
}
});
}

public void start(){
try{
mt.waitForID(0);
}
catch (InterruptedException ie){
ie.printStackTrace();
}
initializing = false;
starThread = new Thread(this);
starThread.start();
}

public void stop(){
done = true;
}

public void run(){
while (! done){
testWalls();
updateStar();
repaint();
try {
Thread.sleep( 1000/60 ); // 1000ms / 60 fps
}
catch( Exception e ){
e.printStackTrace();
}
}
}

void startStarField(){
//Starting speed of the ball is scaled according to playfield.
double speedX = 0.006;
double speedY = 0.004;

start = true;
// re-center.
starPoint.x = getWidth() / 2;
starPoint.y = getHeight() / 2;

boolean isEven;
double speedScale;

isEven = ((int)(Math.random() * 10) % 2 == 0);
star_dx = speedX * getWidth() * (isEven ? -1 : 1);

isEven = ((int)( Math.random() * 10) % 2 == 0);
star_dy = speedY * getHeight() * (isEven ? -1 : 1);
}

void updateStar(){
int dx = (starPoint.x += star_dx);
int dy = (starPoint.y += star_dy);
for (int i = 0; i < npoints; ++i){
xpoints[i] = xpointsOffsets[i] + dx;
ypoints[i] = ypointsOffsets[i] + dy;
}
}

void testWalls(){
int left = walls.x;
int top = walls.y;

int right = walls.x + walls.width;
int bottom = walls.y + walls.height;

if ((starPoint.x + star_dx - 25) <= left ){
bounceStar(1);
}
else if (( starPoint.x + star_dx + 25) >= right){
bounceStar(2);
}

if ((starPoint.y + star_dy - 25) <= top){
bounceStar(3);
}
else if ((starPoint.y + star_dy + 25) >= bottom){
bounceStar(4);
}
}

void bounceStar(int condition){
switch (condition){
case 1 :
case 2 :
star_dx = 0 - star_dx;
break;
case 3:
case 4:
star_dy = 0 - star_dy;
break;
default:
}
}

public void update(Graphics g){
if (initializing){
paint(g);
}
else{
if (doubleBuffer == null){
doubleBuffer = createImage(getWidth(), getHeight());
dbG = doubleBuffer.getGraphics();
}
dbG.setColor(Color.white);
dbG.fillRect(0, 0, getWidth(), getHeight()); // erase prev
drawing
//Draw background image centered
dbG.drawImage
(bgImage, 0, 0, getWidth(), getHeight(), this);
//Draws the rectangle for applet bounds two gray and one white
dbG.setColor(Color.red);
dbG.drawRect
(walls.x - 1, walls.y - 1, walls.width + 2, walls.height + 2);
dbG.drawRect
(walls.x + 1, walls.y + 1, walls.width - 2, walls.height - 2);
dbG.setColor(Color.white);
dbG.drawRect(walls.x, walls.y, walls.width, walls.height);
//Sets color and font
dbG.setColor(Color.red);
dbG.setFont(creator);
fm = dbG.getFontMetrics(creator);
int x = getWidth() / 2;
int y = getHeight() / 2 + fm.getHeight() / 4;
//Draws creator name
dbG.drawString(name, x, y);
//Draws star
if (go){
paint(dbG);
}
g.drawImage(doubleBuffer, 0, 0, this );
}
}

public void paint(Graphics g){
if (initializing){
g.drawString("Inatialzing...", getWidth() - 150, getHeight() /
2);
}
else{
star = new Polygon(xpoints, ypoints, npoints);
g.fillPolygon(star);
}
}// paint()
} // class StarField
-----------------------------------------------------------------

.



Relevant Pages

  • Re: How to open the jsp pages by click on the APPLET
    ... I want to know is there is any way to open the new window of jsp ... requirement.Will give syntax for getting jsp page on applet. ... public void actionPerformed{ ... int prevDragY; // previous position of the mouse. ...
    (comp.lang.java.advocacy)
  • Re: I found a project
    ... private int moveNumber; ... private boolean state; // initailly true true or flase = alive or dead ... public void movePiece{ ...
    (comp.lang.java.help)
  • Please help: Exception in thread "Thread-3" java.lang.NullPointerException
    ... I get the following error when I run applet. ... public void start ... public boolean mouseDown(Event e, int x, int y) ... dbG.fillRect, getHeight()); ...
    (comp.lang.java.help)
  • Adding custom cell renderer to JTable causes delete problems
    ... public void actionPerformed{ ... class TestTable extends JTable { ... public int getNumColumns() { ... Object value, boolean isSelected, boolean hasFocus, int row, ...
    (comp.lang.java.gui)
  • problem in java question
    ... on the applet and ask another multiplication question. ... public int question() ... public void actionPerformed(ActionEvent actionEvent) ...
    (comp.lang.java.programmer)