CMPC
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Another Applet

Go down

Another Applet Empty Another Applet

Post by Paul Mon Dec 07, 2009 7:43 am

This applet creates a line that looks like the horizon or the silhouette of mountains or valleys.
Code:

import java.applet.*;
import java.awt.*;
import java.util.ArrayList;

public class Terrain1 extends Applet
{
    private int width, height;
    private Point t1,t2;
   
    public void init()
    {
        width = getSize().width;
        height = getSize().height;
        t1 = new Point(0, height/2);
        t2 = new Point(width, height/2);
        setBackground( Color.white );
    }
   
    public void paint( Graphics g )
    {
        g.setColor( Color.black );
        ArrayList<LineSeg> list = new ArrayList<LineSeg>();
        list.add(new LineSeg(t1,t2));
        list.get(0).disp(g);
        terRec(g,list,height/3, 5);
    }
   
    public void terRec(Graphics g, ArrayList<LineSeg> list, int Hrange, int n)
    {
        if( n == 0 ) return;
        ArrayList<LineSeg> out = new ArrayList<LineSeg>();
        for(int i = 0; i < list.size(); i++)
        {
            LineSeg temp1 = new LineSeg( list.get(i).start, list.get(i).middle() );
            LineSeg temp2 = new LineSeg( temp1.end, list.get(i).end );
            temp1.end.y += mrand(Hrange);
            temp2.start.y = temp1.end.y;
            out.add(temp1);
            out.add(temp2);
        }
        g.clearRect(0,0,width,height);
        for(int i =0;i < out.size(); i++)
        {
            out.get(i).disp(g);
          mDelay(500);
        }
        mDelay(10);
        terRec(g, out,Hrange/2, n-1);
    }
    public int mrand(int psN)
    {
        if(Math.round(Math.random())==0)return (int)( Math.round(Math.random()*psN) );
        else return (int)( -Math.round(Math.random()*psN) );
    }
    public void mDelay(long millis)
    {
        long end = System.currentTimeMillis();
        while(System.currentTimeMillis() <= end){}
        return;
    }
}
Paul
Paul
Pickaxe

Posts : 611

Back to top Go down

Another Applet Empty Re: Another Applet

Post by Paul Mon Dec 07, 2009 7:44 am

It uses this class:
Code:

import java.awt.*;
public class LineSeg
{
    public Point start, end;
   
    public LineSeg()
    {
      start = new Point(0,0);
      end = new Point(1,1);
    }
   
    public LineSeg(Point begin, Point finish)
    {
        start = begin;
        end = finish;
    }
   
    public Point middle()
    {
        return start.midpoint(end);
    }
   
    public double length()
    {
        return start.distance(end);
    }
   
    public void disp( Graphics g )
    {
        g.drawLine(start.x,start.y,end.x,end.y);
    }
}
Paul
Paul
Pickaxe

Posts : 611

Back to top Go down

Another Applet Empty Re: Another Applet

Post by Paul Mon Dec 07, 2009 7:45 am

Both of which use this class:
Code:

public class Point
{
    public int x,y;
    public Point()
    {
        x = 0;
        y = 0;
    }
    public Point( int xc, int yc )
    {
        x = xc;
        y = yc;
    }
    public Point midpoint(Point between)
    {
        return new Point( (this.x+between.x)/2, (this.y+between.y)/2 );
    }
    public double distance(Point to)
    {
        return Math.sqrt( Math.pow(this.x-to.x,2)+Math.pow(this.y-to.y,2) );
    }
}
Paul
Paul
Pickaxe

Posts : 611

Back to top Go down

Another Applet Empty Re: Another Applet

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum