Jump to content
  • Advertisement
Sign in to follow this  
WilliamM

[java] Need some help with an Applet

This topic is 4726 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is the first time I've ever posted so I'd like to say hello to everyone and I hope to learn alot from these forums. Anywho if some one would be so kind as to explain the purpose of several statements withen a simple script. I have memorized the script and I understand a good majority of it but there a just those statements that throw me off. Oh and also the appplet simply moves two colored balls across a colored background. Instead of pasting the whole thing, I'll paste what I need helpw with; I'm sure you guys would know what else would be required. First, What are threads? How essential are they to a program? I know how to set a basic one up but why exactly do I have to set the priority of one? If I didn't what would happen? Thread.currentThread().setPriority(Thread.MIN_PRIORITY); and MAX_PRIORITY What does the Thread.Sleep(x); do exactly? It is right after the Collision tests. Is it working with that? Are there any other command I should know if I decided to develop more detailed 2d games? __ Second, What is the purpose of having Try and Catch in this block of code public void run() { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while(true) { if(xPos1 > appletSizeX - radius) { xSpeed1 = -1; } if(xPos1 < radius) { xSpeed1 = +1; } if(xPos2 > appletSizeX - radius) { xSpeed2 = -1; } if(xPos2 < radius) { xSpeed2 = +1; } xPos1 += xSpeed1; xPos2 += xSpeed2; repaint(); try { Thread.sleep(20); } catch(Interrupted Exception ex) { } Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } __ And finally third, in this block of code: dbg.setColor(getBackground()); dbg.fillRect(0, 0, this.getSize().width, this.getSize().height); dbg.setColor(getForeground()); Why do I get the background before the foreground? If anybody could provide some answers to these questions I'd Highly appreciate it =] WilliamM

Share this post


Link to post
Share on other sites
Advertisement
-What are threads? How essential are they to a program?

"Many programming languages, operating systems, and other software development environments support what are called "threads" of execution. Threads are similar to processes, in that both represent a single sequence of instructions executed in parallel with other sequences, either by time slicing or multiprocessing. Threads are a way for a program to split itself into two or more simultaneously running tasks. (The name "thread" is by analogy with the way that a number of threads are interwoven to make a piece of fabric)." - http://en.wikipedia.org/wiki/Thread_(computer_science)

In a nutshell, with threads, you can do two things at once.

Threads are important but not essential. 99% of games run on a single thread. Typically you won't need more than one unless you're writing a server or have very complex AI.

-First, What are threads? How essential are they to a program? I know how to set a basic one up but why exactly do I have to set the priority of one? If I didn't what would happen?

Threads have a priority- this relates to the frequency at which a thread is updated and/or the ammount of resources devoted to the thread. Priority between threads is relative. A high priority thread will have more resources devoted to it than a low priority thread.

In Java, threads have a range of priority of 1-10. MIN_PRIORITY = 1, MAX_PRORITY = 10. If you don't specify a priority then the priority is set to 5, which is the default. (I believe the static variable is Thread.DEFAULT_PRIORITY) You generally don't have to mess with the priority in Java. It does a good job of managing threads as long as you don't have too many. However, if a thread is set to sleep, then by convention it's priority is usually set lower- the logic is that if a thread is sleeping, it isn't very important and therefore doesn't need much attention.

-What does the Thread.Sleep(x); do exactly? It is right after the Collision tests. Is it working with that?

Thread.Sleep(x) make the thread "sleep", or, stop, for x milliseconds. This is useful in games as it allows the logic thread to run at a consistant speed. Typically a thread will sleep to allow the renderer to draw at a certain framerate. For example, if you want to draw 50 fps, you'd sleep for 20 milliseconds. (1000milliseconds per second / 50 frames = 20 milliseconds per frame)

It isn't directly working with the collision tests. What it's doing is throttling the collision tests to occur every 20 milliseconds.

-Are there any other command I should know if I decided to develop more detailed 2d games?

If you want to develop more detailed 2d games, I'd suggest ditching applets and looking into Java2D or a 3D environment like Java3D, JOGL, or LWJGL.

-What is the purpose of having Try and Catch in this block of code

It can be tricky to work with multiple threads. For example, it is possible for two threads to be editing the same data. To ensure the safety of data it's better to "ask" the thread to do something rather than "tell" it to. The try/catch block is a safeguard in case something goes wrong in "asking" the thread to sleep. It's actually quite helpful because it allows you to pinpoint errors- to see which threads mess up and why.

-And finally third, in this block of code

Things are drawn on a first come, first serve basis to Java Graphics objects. This means that things drawn later will appear on top of things drawn earlier. Because of this, it makes sense to draw the background of a scene earlier than the foreground. In this example, it's clearing the background with a specific color and then sets the foreground color. It might be clearing the background white and setting the pen color to black- to draw black text. Or perhaps white on a black background.

I'm sure other people can come up with better explinations for these things. This should get you on your way though.

Share this post


Link to post
Share on other sites
"Many programming languages, operating systems, and other software development environments support what are called "threads" of execution. Threads are similar to processes, in that both represent a single sequence of instructions executed in parallel with other sequences, either by time slicing or multiprocessing. Threads are a way for a program to split itself into two or more simultaneously running tasks. (The name "thread" is by analogy with the way that a number of threads are interwoven to make a piece of fabric)." - http://en.wikipedia.org/wiki/Thread_(computer_science)
In a nutshell, with threads, you can do two things at once.
Threads are important but not essential. 99% of games run on a single thread. Typically you won't need more than one unless you're writing a server or have very complex AI.
-------
Ah, see I was thinking along those lines but I wasn't totally sure and just had to ask. So most games run along one thread.. What about games that support networking like Half life? I mean would you need multiple threads to support the interactions between the client and the server?

What would happen if you set a thread to an inappropriate priority? If the the 1-10 range is for the amount of resources your telling the thread to have, then what if you overloaded the thread and gave a slim amount of resources?
Would it even compile?

In the script he starts it with min_priority and ends it with Max. What was the use of that?

Also I was only planning on learning the concepts to the very basics of 2d games in applets. I know that scripting the same thing for an application is different. The concepts are still the same I would assume.
WilliamM

Share this post


Link to post
Share on other sites
can you show me why the KeyListener in JPanel not active

class 1

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.Graphics2D;
import java.util.*;
import javax.imageio.*;
import java.io.*;

public class Puyo extends JFrame
{
JLabel lb1,lb2,score,level,nameprogram,contact;
Font fontchu;
JButton pause,option;
JPanel pscorelevel,pcontact,pnameprogram,pbutton,psmallpuyo,pscore,plevel;
Image[] images=new Image[4];
PuyoShape Puyo_shape;
SmallPuyo Puyo_small;
int speed=800;
int[][] record=new int[6][12];
int points=0;//diem
public static int choosechange;
public static int randomshape,randomshape2;
public static final int WIDTH = 600;
public static final int HEIGHT = 670;
Container con=this.getContentPane();

public Puyo()
{
con.setBackground(Color.white);
con.setLayout(new BorderLayout());
//khai bao panel
pscore=new JPanel();
plevel=new JPanel();

pscorelevel=new JPanel();
pscorelevel.setLayout(new GridLayout(2,1,10,10));

pcontact=new JPanel();
pcontact.setLayout(new FlowLayout(FlowLayout.CENTER));

pnameprogram=new JPanel();
pnameprogram.setLayout(new FlowLayout(FlowLayout.CENTER));

pbutton=new JPanel();

psmallpuyo=new JPanel();
psmallpuyo.setLayout(new BorderLayout());
//khai bao component

images[0]=Toolkit.getDefaultToolkit().getImage("c:/PuyoGameLoft/puyo/puyo_blue.gif");
images[1]=Toolkit.getDefaultToolkit().getImage("c:/PuyoGameLoft/puyo/puyo_red.gif");
images[2]=Toolkit.getDefaultToolkit().getImage("c:/PuyoGameLoft/puyo/puyo_green.gif");
images[3]=Toolkit.getDefaultToolkit().getImage("c:/PuyoGameLoft/puyo/puyo_yellow.gif");



randomshape=(int)(Math.random()*3);
randomshape2=(int)(Math.random()*3);

Puyo_shape=new PuyoShape(images[randomshape],images[randomshape2],3,0,3,1,randomshape,randomsha
pe2);
Puyo_shape.repaint();
con.add(Puyo_shape,BorderLayout.CENTER);

Thread t=new Thread()
{
public void run()
{
Puyo_shape.repaint();
}
}
Puyo_shape.requestFocus(true);
// Puyo_shape.requestFocusInWindow();
Puyo_shape.repaint();

Puyo_small=new SmallPuyo();
Puyo_small.repaint();

pause=new JButton("Pause");
option=new JButton("Option");
score=new JLabel("Score");
lb1=new JLabel("DiemScore");
lb2=new JLabel("MucLevel");
level=new JLabel("Level");
contact=new JLabel("Contact");
nameprogram=new JLabel("Name Program");

//add vao panel
pscore.add(score);
pscore.add(lb1);
plevel.add(level);
plevel.add(lb2);

pscorelevel.add(pscore);
pscorelevel.add(plevel);

pcontact.add(contact);

pnameprogram.add(nameprogram);

pbutton.add(pause);
pbutton.add(option);

psmallpuyo.add(pbutton,BorderLayout.CENTER);

con.add(pnameprogram,BorderLayout.NORTH);
con.add(pcontact,BorderLayout.SOUTH);
con.add(pscorelevel,BorderLayout.EAST);
con.add(psmallpuyo,BorderLayout.WEST);



}

public static void main(String[] args)
{
Puyo frame=new Puyo();
frame.setSize(WIDTH,HEIGHT);
frame.setVisible(true);
frame.Puyo_shape.repaint();

}
}

class 2

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class PuyoShape extends PuyoShapeCanvas
{


public PuyoShape(Image image1,Image image2,int x1,int y1,int x2,int y2,int state1,int state2)
{
super();
super.x1=x1;
super.y1=y1;
super.x2=x2;
super.y2=y2;
super.image1=image1;
super.image2=image2;
super.state1=state1;
super.state2=state2;

System.out.println("Coco jambo");
addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent evt) {
int key = evt.getKeyCode();
System.out.println("Coco jambo");
if (key == KeyEvent.VK_LEFT)
{
System.out.println("HAHA");
moveLeft();
repaint();

}


}
});
}
public void paint(Graphics g)
{
g.drawRect(0,0,193,384);
System.out.print(state1 + " " + state2 );
g.drawImage(image1,x1*32+1,y1*32,null);
g.drawImage(image2,x2*32+1,y2*32,null);

/* g.drawImage(image1,(x1-2)*32+1,y1*32,null);
g.drawImage(image2,x2*32+1,(y2+1)*32,null);

g.drawImage(image1,(x1-1)*32+1,y1*32,null);
g.drawImage(image2,x2*32+1,(y2+2)*32,null);

g.drawImage(image1,x1*32+1,y1*32,null);
g.drawImage(image2,x2*32+1,(y2+3)*32,null);

g.drawImage(image1,(x1+1)*32+1,y1*32,null);
g.drawImage(image2,x2*32+1,(y2+4)*32,null);

g.drawImage(image1,(x1+2)*32+1,y1*32,null);
g.drawImage(image2,x2*32+1,(y2+5)*32,null);

g.drawImage(image1,(x1+2)*32+1,y1*32,null);
g.drawImage(image2,x2*32+1,(y2+6)*32,null);

g.drawImage(image1,(x1+2)*32+1,y1*32,null);
g.drawImage(image2,x2*32+1,(y2+7)*32,null);

g.drawImage(image1,(x1+2)*32+1,y1*32,null);
g.drawImage(image2,x2*32+1,(y2+8)*32,null);

g.drawImage(image1,(x1+2)*32+1,y1*32,null);
g.drawImage(image2,x2*32+1,(y2+9)*32,null);

g.drawImage(image1,(x1+2)*32+1,y1*32,null);
g.drawImage(image2,x2*32+1,(y2+10)*32,null);
*/

}
}

class 3

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class PuyoShapeCanvas extends JPanel
{
int x1,y1,x2,y2,state1,state2;
Image image1,image2;
int[][] occupy=new int[6][12];

public PuyoShapeCanvas()
{
for ( int i=0;i<6;i++)
for (int k=0;k<12;k++)
{
occupy[k]=-1;
}
}
public void moveLeft()
{
occupy[x1][y1]=-1;
occupy[x2][y2]=-1;
occupy[x1-1][y1]=state1;
occupy[x2-1][y2]=state2;
x1--;x2--;

}

}


class 4

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;

public class SmallPuyo extends JPanel
{
public SmallPuyo()
{

}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!