3 Replies - 845 Views - Last Post: 17 February 2010 - 10:30 PM Rate Topic: -----

#1 Chlose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 06-December 09

Java Code Help

Posted 17 February 2010 - 06:44 PM

Ok i was given a project to do in my programming class which was to look up an applet written in java and present it telling what the bits of code do. I have looked at the java syntax help but i still have no idea what some of these things do. So if a kind soul can lay out comments for me/tell me what bits of code mean, i would be grateful. Oh and this isnt my program, the credits are on the top.

// Abstract space
// Copyright (C) 2002 Charles Foster
// Please visit www.cfoster.net

import java.awt.*;
import java.applet.*;

public class AbstractSpace extends Applet implements Runnable
{
	double[][] stars;
	double centerx, centery;

	Thread myThread = null;
	Image myImage = null;
	Graphics g2;

	boolean clicked = false;
	boolean credits = true;

	double speedx = 0.02, speedy = 0.02, angrot = 0.05;
	double oldx = 0, oldy = 0;
	int stramt, j;

	public void init()
	{
		centerx = (int)size().width/2;
		centery = (int)size().height/2;
		getParameters();
		for(int i=0;i<stars.length;i++)
		{
			buildStar(i);
		}
		myImage = createImage(size().width,size().height);
		g2 = myImage.getGraphics();
	}
	public void getParameters()
	{
		speedx = (double)(Integer.parseInt(getParameter("speedx")))/100;
		speedy = (double)(Integer.parseInt(getParameter("speedy")))/100;
		angrot = (double)(Integer.parseInt(getParameter("angrot")))/100;
		stramt = Integer.parseInt(getParameter("stramt"));
		stars = new double[stramt][6];
	}
	public void buildStar(int s)
	{
		stars[s][0] = 0;
		stars[s][1] = 0;

		stars[s][2] = 0;
		stars[s][3] = 0;

		stars[s][4] = Math.random()*360; // angle to start
		stars[s][5] = Math.random()*size().width*2; // life expectancy
	}
	public void start()
	{
		if(myThread == null)
		{
			myThread = new Thread(this);
			myThread.start();
		}
	}
	public void stop()
	{
		if(myThread != null)
		{
			myThread.stop();
			myThread = null;
		}
	}
	public void run()
	{
		while(true)
		{
			g2.setColor(Color.black);
			g2.fillRect(0,0,size().width,size().height);
			if(credits)
			{
				g2.setColor(Color.green);
				g2.drawString("Abstract Space, Copyright (C) Charles Foster 2002",20,20);
				g2.drawString("www.cfoster.net",20,35);
				if(++j > 400) { credits = false; }
			}
			g2.setColor(Color.white);
			for(int i=0;i<stars.length;i++)
			{
				if(!clicked)
				{
					if(stars[i][0] > stars[i][5]) { buildStar(i); }
					stars[i][0] = Math.cos(oldx+stars[i][4])*stars[i][2]*180/Math.PI+centerx;
					stars[i][1] = Math.sin(oldy+stars[i][4])*stars[i][3]*180/Math.PI+centery;
					stars[i][2] += speedx;
					stars[i][3] += speedy;
					stars[i][4] += angrot;
				}
				else
				{
					stars[i][0] = Math.cos(oldx+stars[i][4])*stars[i][2]*180/Math.PI+centerx;
					stars[i][1] = Math.sin(oldy+stars[i][4])*stars[i][3]*180/Math.PI+centery;
				}
				g2.fillRect((int)stars[i][0],(int)stars[i][1],2,2);
			}
			repaint();
			try { myThread.sleep(25); } catch(InterruptedException e) {}
		}
	}
	public boolean mouseDrag(Event evt,int x,int y)
	{
		clicked = true;
		oldy = (double)(y-oldy)/180*Math.PI;
		oldx = (double)(oldx-x)/180*Math.PI;
		return true;
	}
	public boolean mouseUp(Event e, int x, int y)
	{
		clicked = false;
		return true;
	}
	public void paint(Graphics g){}
	public void update(Graphics g)
	{
		paint(g2);
		g.drawImage(myImage, 0, 0, this);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java Code Help

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Java Code Help

Posted 17 February 2010 - 07:00 PM

import java.awt.*;
import java.applet.*;

public class AbstractSpace extends Applet implements Runnable
{
        // Set up all the instance variables
        double[][] stars;
        double centerx, centery;

        Thread myThread = null;
        Image myImage = null;
        Graphics g2;

        boolean clicked = false;
        boolean credits = true;

        double speedx = 0.02, speedy = 0.02, angrot = 0.05;
        double oldx = 0, oldy = 0;
        int stramt, j;

        // init() is always the very first method called on an
        // applet. It works very much like a constructor
        // does and essentially sets up "stuff"
        public void init()
        {
                // Sets the center by using the applet's 
                // size() method. 
                centerx = (int)size().width/2;
                centery = (int)size().height/2;

                // Loads all the parameters passed into the program
                // via HTML. This calls the method.
                getParameters();

                // For every star...
                for(int i=0;i<stars.length;i++)
                {
                        // Make a star.
                        buildStar(i);
                }

                // Makes an image on which to paint and then setting
                // the graphics FOR that image.
                myImage = createImage(size().width,size().height);
                g2 = myImage.getGraphics();
        }
 
        public void getParameters()
        {
                // All this does is loads variables from he HTML into 
                // the program to be later used.
                speedx = (double)(Integer.parseInt(getParameter("speedx")))/100;
                speedy = (double)(Integer.parseInt(getParameter("speedy")))/100;
                angrot = (double)(Integer.parseInt(getParameter("angrot")))/100;
                stramt = Integer.parseInt(getParameter("stramt"));
                stars = new double[stramt][6];
        }

        public void buildStar(int s)
        {
                // Each star has 6 values denoted by the second set of brackets
                stars[s][0] = 0;
                stars[s][1] = 0;

                stars[s][2] = 0;
                stars[s][3] = 0;

                stars[s][4] = Math.random()*360; // angle to start
                stars[s][5] = Math.random()*size().width*2; // life expectancy
        }

        // start() is the second method called on an applet. In this one,
        // a thread is set up and started. The myThread.start() calls
        // the run method of the Thread or Runnable class (this one)
        public void start()
        {
                if(myThread == null)
                {
                        myThread = new Thread(this);
                        myThread.start();
                }
        }
   
        // This is the 3rd method called in an applet. All that this does is
        // stopped and set to null.
        public void stop()
        {
                if(myThread != null)
                {
                        myThread.stop();
                        myThread = null;
                }
        }

        // When the Thread.start() method is called, so is this one. 
        // All these drawing method do is to draw on the image and then
        // takes a short break, allowing for a distinct frame rate.
        public void run()
        {
                while(true)
                {
                        g2.setColor(Color.black);
                        g2.fillRect(0,0,size().width,size().height);
                        if(credits)
                        {
                                g2.setColor(Color.green);
                                g2.drawString("Abstract Space, Copyright (C) Charles Foster 2002",20,20);
                                g2.drawString("www.cfoster.net",20,35);
                                if(++j > 400) { credits = false; }
                        }
                        g2.setColor(Color.white);
                        for(int i=0;i<stars.length;i++)
                        {
                                if(!clicked)
                                {
                                        if(stars[i][0] > stars[i][5]) { buildStar(i); }
                                        stars[i][0] = Math.cos(oldx+stars[i][4])*stars[i][2]*180/Math.PI+centerx;
                                        stars[i][1] = Math.sin(oldy+stars[i][4])*stars[i][3]*180/Math.PI+centery;
                                        stars[i][2] += speedx;
                                        stars[i][3] += speedy;
                                        stars[i][4] += angrot;
                                }
                                else
                                {
                                        stars[i][0] = Math.cos(oldx+stars[i][4])*stars[i][2]*180/Math.PI+centerx;
                                        stars[i][1] = Math.sin(oldy+stars[i][4])*stars[i][3]*180/Math.PI+centery;
                                }
                                g2.fillRect((int)stars[i][0],(int)stars[i][1],2,2);
                        }
                        repaint();
                        try { myThread.sleep(25); } catch(InterruptedException e) {}
                }
        }

        // This part is normally part of a mouse event, but I'm not familiar
        // with how Applets deal with MouseEvents.
        public boolean mouseDrag(Event evt,int x,int y)
        {
                clicked = true;
                oldy = (double)(y-oldy)/180*Math.PI;
                oldx = (double)(oldx-x)/180*Math.PI;
                return true;
        }
        public boolean mouseUp(Event e, int x, int y)
        {
                clicked = false;
                return true;
        }

        // This paint method does nothing because it is all done in the  
        // thread
        public void paint(Graphics g){}

        // update() is called many times per second and it paints the 
        // image (onto which we draw) onto the actual applet.
        public void update(Graphics g)
        {
                paint(g2);
                g.drawImage(myImage, 0, 0, this);
        }
}



Now, for yours, you will obviously have to be far more detailed, but I hope I helped you get started. :)
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java Code Help

Posted 17 February 2010 - 07:21 PM

Please respect the rule #5 of this forum and give your topic title a meaningful title
"Java code help" is kind of useless in this forum
Was This Post Helpful? 0
  • +
  • -

#4 Chlose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 06-December 09

Re: Java Code Help

Posted 17 February 2010 - 10:30 PM

Thanks! Will go through it and hopefully will understand a bit more. And sorry about the title... I just kinda... Put it up...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1