PDA

View Full Version : Test my java applet



Heysus
03-07-2011, 10:37 PM
http://heysus.host22.com/
It's meant to help people design bike ramps. I think it turned out great considering I know almost nothing about java :p.

Heres the source. It's pretty messy and there probably a lot of stuff that can be improved. I'm definately open to suggestions :).

import java.applet.Applet;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Event;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;



public class App extends Applet implements ItemListener, ActionListener, MouseListener {

TextField textBox_angle = new TextField(3);
TextField textBox_height = new TextField(3);
Button button_metric = new Button("Metric");
Button button_american = new Button("American");
Choice unitMenu;

int rx;
int ry;
double rr = 0; //ramp radius
double ra = 60; //ramp angle
double rh = 6; //ramp height
double rl = 0; //ramp length

boolean metric = false;
double unit = 1;

DecimalFormat twoDForm = new DecimalFormat("#.##");


public void paint( Graphics g ) {

//draw background
//Color background = new Color(213, 235, 240);
Color background = Color.white;
g.setColor(background);
g.fillRect(0, 0, 599, 399);

if ((ra > 0) && (rh > 0)) {
rr = rh/(1 + Math.sin(Math.toRadians(270 + ra)));
}


/*if (rh == 0) {
if ((ra > 0) && (rr > 0)) {
i = Math.sin(Math.toRadians(270 + ra));
rh = rr + i*rr;
}
}*/

rl = Math.cos(Math.toRadians(270 + ra))*rr;





int scalex = (int) (550/(rl + 6));
int scaley = (int) (300/rh);
int scale = Math.min(scalex, scaley);

// ramp pixel values
int rpr = (int) (rr*scale);
int rpa = (int) (ra);
int rph = (int) (rh*scale);
int rpl = (int) (rl*scale);


rx = 550;
ry = 350;
int bx = 20;
int by = 351;


// Draw Ramp
g.setColor(new Color(226, 209, 173));
g.fillRect(rx + (int) (0.2*scale) - rpl, ry - rph + 1, rpl, rph - 1);

g.setColor(background);
g.fillArc(rx - rpr - rpl, ry - rpr*2, rpr*2, rpr*2, 270, rpa);

g.setColor(Color.black);
g.drawArc(rx - rpr - rpl, ry - rpr*2, rpr*2, rpr*2, 270, rpa);
g.drawArc(rx - rpr - rpl, ry - rpr*2, rpr*2, rpr*2, 270, rpa);
//g.drawLine(rx, ry, rx - rpl, ry);
g.drawLine(rx + (int) (0.2*scale), ry, rx + (int) (0.2*scale), ry - rph);
g.drawLine(rx, ry - rph, rx + (int) (0.2*scale), ry - rph);





// BACK WHEEL /////////////////////////////////////////////////////////////////////////////
int cx = (int) (bx + 1.0833*scale);
int cy = (int) (by - 1.0833*scale);

//tire
int r = (int) (1.0833*scale);
g.fillOval(cx - r, cy - r, r*2, r*2);

r = (int) (0.92*scale); //0.9167
g.setColor(Color.white);
g.fillOval(cx - r, cy - r, r*2, r*2);
g.setColor(Color.black);

//back rim
g.setColor(background);
r = (int) (0.85*scale);
g.fillOval(cx - r, cy - r, r*2, r*2);
g.setColor(Color.black);
g.drawOval(cx - r, cy - r, r*2, r*2);


//back spokes
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(0))*r), cy + (int) (Math.sin(Math.toRadians(0))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(20))*r), cy + (int) (Math.sin(Math.toRadians(20))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(40))*r), cy + (int) (Math.sin(Math.toRadians(40))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(60))*r), cy + (int) (Math.sin(Math.toRadians(60))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(80))*r), cy + (int) (Math.sin(Math.toRadians(80))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(100))*r), cy + (int) (Math.sin(Math.toRadians(100))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(120))*r), cy + (int) (Math.sin(Math.toRadians(120))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(140))*r), cy + (int) (Math.sin(Math.toRadians(140))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(160))*r), cy + (int) (Math.sin(Math.toRadians(160))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(180))*r), cy + (int) (Math.sin(Math.toRadians(180))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(200))*r), cy + (int) (Math.sin(Math.toRadians(200))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(220))*r), cy + (int) (Math.sin(Math.toRadians(220))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(240))*r), cy + (int) (Math.sin(Math.toRadians(240))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(260))*r), cy + (int) (Math.sin(Math.toRadians(260))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(280))*r), cy + (int) (Math.sin(Math.toRadians(280))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(300))*r), cy + (int) (Math.sin(Math.toRadians(300))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(320))*r), cy + (int) (Math.sin(Math.toRadians(320))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(340))*r), cy + (int) (Math.sin(Math.toRadians(340))*r));

// FRONT WHEEL /////////////////////////////////////////////////////////////////////////////
cx = (int) (bx + 3.5*scale + 1.0833*scale);
cy = (int) (by - 1.0833*scale);

//tire
r = (int) (1.0833*scale);
g.fillOval(cx - r, cy - r, r*2, r*2);

r = (int) (0.92*scale); //0.9167
g.setColor(Color.white);
g.fillOval(cx - r, cy - r, r*2, r*2);
g.setColor(Color.black);

//front rim
g.setColor(background);
r = (int) (0.85*scale);
g.fillOval(cx - r, cy - r, r*2, r*2);
g.setColor(Color.black);
g.drawOval(cx - r, cy - r, r*2, r*2);


//back spokes
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(0))*r), cy + (int) (Math.sin(Math.toRadians(0))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(20))*r), cy + (int) (Math.sin(Math.toRadians(20))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(40))*r), cy + (int) (Math.sin(Math.toRadians(40))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(60))*r), cy + (int) (Math.sin(Math.toRadians(60))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(80))*r), cy + (int) (Math.sin(Math.toRadians(80))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(100))*r), cy + (int) (Math.sin(Math.toRadians(100))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(120))*r), cy + (int) (Math.sin(Math.toRadians(120))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(140))*r), cy + (int) (Math.sin(Math.toRadians(140))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(160))*r), cy + (int) (Math.sin(Math.toRadians(160))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(180))*r), cy + (int) (Math.sin(Math.toRadians(180))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(200))*r), cy + (int) (Math.sin(Math.toRadians(200))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(220))*r), cy + (int) (Math.sin(Math.toRadians(220))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(240))*r), cy + (int) (Math.sin(Math.toRadians(240))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(260))*r), cy + (int) (Math.sin(Math.toRadians(260))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(280))*r), cy + (int) (Math.sin(Math.toRadians(280))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(300))*r), cy + (int) (Math.sin(Math.toRadians(300))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(320))*r), cy + (int) (Math.sin(Math.toRadians(320))*r));
g.drawLine(cx, cy, cx + (int) (Math.cos(Math.toRadians(340))*r), cy + (int) (Math.sin(Math.toRadians(340))*r));


// FRAME //////////////////////////////////////
int[] xPoints = {0, 0, 0, 0};
int[] yPoints = {0, 0, 0, 0};

//chain stay
cx = (int) (bx + 1.0833*scale); // back wheel center
cy = (int) (by - 1.0833*scale);

xPoints[0] = cx; yPoints[0] = cy + (int) (0.0625*scale);
xPoints[1] = cx; yPoints[1] = cy - (int) (0.0625*scale);
xPoints[2] = cx + (int) (1.3333*scale); yPoints[2] = cy + (int) (0.1750*scale);
xPoints[3] = cx + (int) (1.3333*scale); yPoints[3] = cy + (int) (0.3000*scale);
g.fillPolygon(xPoints, yPoints, 4);



//seat stay
xPoints[0] = cx; yPoints[0] = cy + (int) (0.0625*scale);
xPoints[1] = cx; yPoints[1] = cy - (int) (0.07*scale);
xPoints[2] = cx + (int) (1.2300*scale); yPoints[2] = cy - (int) (0.84*scale);
xPoints[3] = cx + (int) (1.2300*scale); yPoints[3] = cy - (int) (0.70*scale);
g.fillPolygon(xPoints, yPoints, 4);



//seat tube
xPoints[0] = cx + (int) (1.1045*scale); yPoints[0] = cy - (int) (0.85*scale);
xPoints[1] = cx + (int) (1.2305*scale); yPoints[1] = cy - (int) (0.89*scale);
xPoints[2] = cx + (int) (1.3958*scale); yPoints[2] = cy + (int) (0.30*scale);
xPoints[3] = cx + (int) (1.2708*scale); yPoints[3] = cy + (int) (0.30*scale);
g.fillPolygon(xPoints, yPoints, 4);


//bottom tube
xPoints[0] = cx + (int) (1.25*scale); yPoints[0] = cy + (int) (0.225*scale);
xPoints[1] = cx + (int) (1.39*scale); yPoints[1] = cy + (int) (0.30*scale);
xPoints[2] = cx + (int) (3.0*scale); yPoints[2] = cy - (int) (1.25*scale);
xPoints[3] = cx + (int) (2.93*scale); yPoints[3] = cy - (int) (1.37*scale);
g.fillPolygon(xPoints, yPoints, 4);

//top tube
xPoints[0] = cx + (int) (1.23*scale); yPoints[0] = cy - (int) (0.833*scale);
xPoints[1] = cx + (int) (1.23*scale); yPoints[1] = cy - (int) (0.677*scale);
xPoints[2] = cx + (int) (2.96*scale); yPoints[2] = cy - (int) (1.35*scale);
xPoints[3] = cx + (int) (2.9*scale); yPoints[3] = cy - (int) (1.475*scale);
g.fillPolygon(xPoints, yPoints, 4);


//head tube
xPoints[0] = cx + (int) (2.82*scale); yPoints[0] = cy - (int) (1.1*scale);
xPoints[1] = cx + (int) (2.82*scale); yPoints[1] = cy - (int) (1.1*scale);
xPoints[2] = cx + (int) (2.92*scale); yPoints[2] = cy - (int) (1.4*scale);
xPoints[3] = cx + (int) (2.92*scale); yPoints[3] = cy - (int) (1.4*scale);
g.fillPolygon(xPoints, yPoints, 4);


//fork
xPoints[0] = cx + (int) (2.85*scale); yPoints[0] = cy - (int) (1.49*scale);
xPoints[1] = cx + (int) (2.98*scale); yPoints[1] = cy - (int) (1.53*scale);
xPoints[2] = cx + (int) (3.57*scale); yPoints[2] = cy + (int) (0.05*scale);
xPoints[3] = cx + (int) (3.45*scale); yPoints[3] = cy + (int) (0.08*scale);
g.fillPolygon(xPoints, yPoints, 4);

//stem
xPoints[0] = cx + (int) (2.85*scale); yPoints[0] = cy - (int) (1.40*scale);
xPoints[1] = cx + (int) (3.121*scale); yPoints[1] = cy - (int) (1.575*scale);
xPoints[2] = cx + (int) (3.1*scale); yPoints[2] = cy - (int) (1.7*scale);
xPoints[3] = cx + (int) (2.82*scale); yPoints[3] = cy - (int) (1.61*scale);
g.fillPolygon(xPoints, yPoints, 4);

//bars
xPoints[0] = cx + (int) (2.95*scale); yPoints[0] = cy - (int) (1.77*scale);
xPoints[1] = cx + (int) (3.07*scale); yPoints[1] = cy - (int) (1.57*scale);
xPoints[2] = cx + (int) (3.1*scale); yPoints[2] = cy - (int) (1.7*scale);
xPoints[3] = cx + (int) (3*scale); yPoints[3] = cy - (int) (1.857*scale);
g.fillPolygon(xPoints, yPoints, 4);
g.fillOval(cx + (int) (2.862*scale), cy - (int) (1.885*scale), (int) (0.16*scale), (int) (0.16*scale));

//seat
g.fillOval(cx + (int) (0.75*scale), cy - (int) (1.15*scale), (int) (0.4*scale), (int) (0.15*scale));
g.fillOval(cx + (int) (1.29*scale), cy - (int) (1.19*scale), (int) (0.14*scale), (int) (0.1*scale));

xPoints[0] = cx + (int) (1.05*scale); yPoints[0] = cy - (int) (1.01*scale);
xPoints[1] = cx + (int) (0.95*scale); yPoints[1] = cy - (int) (1.15*scale);
xPoints[2] = cx + (int) (1.33*scale); yPoints[2] = cy - (int) (1.185*scale);
xPoints[3] = cx + (int) (1.33*scale); yPoints[3] = cy - (int) (1.105*scale);
g.fillPolygon(xPoints, yPoints, 4);

//seat post
xPoints[0] = cx + (int) (1.13*scale); yPoints[0] = cy - (int) (0.77*scale);
xPoints[1] = cx + (int) (1.23*scale); yPoints[1] = cy - (int) (0.77*scale);
xPoints[2] = cx + (int) (1.19*scale); yPoints[2] = cy - (int) (1.065*scale);
xPoints[3] = cx + (int) (1.08*scale); yPoints[3] = cy - (int) (1.06*scale);
g.fillPolygon(xPoints, yPoints, 4);

/*//crank arm
xPoints[0] = cx + (int) (1.23*scale); yPoints[0] = cy + (int) (0.2*scale);
xPoints[1] = cx + (int) (1.38*scale); yPoints[1] = cy + (int) (0.2*scale);
xPoints[2] = cx + (int) (1.9*scale); yPoints[2] = cy + (int) (0.5*scale);
xPoints[3] = cx + (int) (1.75*scale); yPoints[3] = cy + (int) (0.5*scale);
//g.setColor(Color.red);
g.fillPolygon(xPoints, yPoints, 4);

//pedal
xPoints[0] = cx + (int) (1.65*scale); yPoints[0] = cy + (int) (0.52*scale);
xPoints[1] = cx + (int) (1.65*scale); yPoints[1] = cy + (int) (0.48*scale);
xPoints[2] = cx + (int) (1.85*scale); yPoints[2] = cy + (int) (0.48*scale);
xPoints[3] = cx + (int) (1.85*scale); yPoints[3] = cy + (int) (0.52*scale);
g.setColor(Color.white);
g.fillPolygon(xPoints, yPoints, 4);
g.setColor(Color.black);
g.drawPolygon(xPoints, yPoints, 4);
*/


unitMenu.setLocation(495, 8);

//draw text boxes
g.setFont(new Font("sansserif", Font.BOLD, 16));

g.drawString("Angle = ", 115, 30); //145
textBox_angle.setLocation(190, 14);

g.drawString("Height = ", 115, 60);
textBox_height.setLocation(190, 44);

g.drawString("Radius", 295, 30);
g.drawString("=", 356, 30);
if (rr < 10) {
g.setColor(Color.red);
g.setFont(new Font("sansserif", Font.PLAIN, 12));


g.drawString("Recommended min radius: MTB: " + twoDForm.format(10*unit) + " BMX: " + twoDForm.format(7*unit), 122, 85);



g.setFont(new Font("sansserif", Font.BOLD, 16));
}

g.drawString("" + Double.valueOf(twoDForm.format(rr*unit)), 370, 30);

g.setColor(Color.black);

g.drawString("Length", 295, 60);
g.drawString("=", 356, 60);
g.drawString("" + Double.valueOf(twoDForm.format(rl*unit)), 370, 60);

//draw ground
g.setColor(new Color(221, 221, 221));
g.fillRect(0, 350, 600, 50);
g.setColor(Color.black);
g.drawLine(0, 350, 599, 350);

//window 600x400
g.drawRect(0, 0, 599, 399);

}



public void init() {
unitMenu = new Choice();
unitMenu.add("Feet");
unitMenu.add("Inches");
unitMenu.add("Meters");
unitMenu.add("Centimeters");
add(unitMenu);
unitMenu.addItemListener(this);
unitMenu.setFocusable(false);



textBox_angle.setText(Double.toString(ra));
textBox_angle.addActionListener(this);
textBox_angle.addMouseListener(this);
add(textBox_angle);

textBox_height.setText(Double.toString(rh));
textBox_height.addActionListener(this);
textBox_height.addMouseListener(this);
add(textBox_height);

}

/*

public boolean action(Event evt, Object whatAction) {

if (evt.target == textBox_angle) {

ra = Double.valueOf(textBox_angle.getText().trim()).dou bleValue();

if (ra > 90) {
textBox_angle.setText("90");
ra = 90;
}
repaint();
return true;
}




if (evt.target == textBox_height) {

if (metric) {
rh = (Double.valueOf(textBox_height.getText().trim()).d oubleValue())/30.48;
} else {
rh = Double.valueOf(textBox_height.getText().trim()).do ubleValue();
}

repaint();
return true;
}


if (evt.target == unitMenu) {
if (unitMenu.getSelectedItem().equals("Centimeters")) {
metric = true;
repaint();
return true;

}


if (evt.target == button_american) {
button_american.setBackground(new Color(215, 215, 215));
button_american.setForeground(new Color(48, 0, 86));
button_metric.setBackground(new Color(240, 240, 240));
button_metric.setForeground(new Color(180, 180, 180));
metric = false;
ra = 50;
repaint();
return true;
}

if (evt.target == button_metric) {
button_metric.setBackground(new Color(215, 215, 215));
button_metric.setForeground(new Color(48, 0, 86));
button_american.setBackground(new Color(240, 240, 240));
button_american.setForeground(new Color(180, 180, 180));
metric = true;
repaint();
return true;
}

return false;
}
*/

public void itemStateChanged(ItemEvent objEvent) {
if (objEvent.getItemSelectable() == unitMenu) {
String s = ((Choice)objEvent.getSource()).getSelectedItem();
if (s.equals("Feet")) { unit = 1;}
if (s.equals("Inches")) { unit = 12;}
if (s.equals("Meters")) { unit = 0.3048;}
if (s.equals("Centimeters")) { unit = 30.48;}
unitMenu.setFocusable(false);
textBox_height.setText("" + Double.valueOf(twoDForm.format(rh*unit)));
repaint();
}
}



public void actionPerformed(ActionEvent e) {

if (e.getSource().equals(textBox_angle)) {
ra = Double.valueOf(textBox_angle.getText().trim()).dou bleValue();

if (ra > 90) {
textBox_angle.setText("90");
ra = 90;
}
if (ra < 0) {
textBox_angle.setText("1");
ra = 1;
}
repaint();
}


if (e.getSource().equals(textBox_height)) {
rh = (Double.valueOf(textBox_height.getText().trim()).d oubleValue())/unit;
repaint();
}
}



public void mouseClicked(MouseEvent e) {
if (e.getSource().equals(textBox_angle)) {
textBox_angle.selectAll();
return;
}

if (e.getSource().equals(textBox_height)) {
textBox_height.selectAll();
return;
}
}



@Override
public void mouseEntered(MouseEvent e) {
}



@Override
public void mouseExited(MouseEvent e) {
}



@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}



@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}




}

grats
03-07-2011, 11:00 PM
You are seeing this page because the system administrator of 000webhost.com is currently checking this website for malicious content. This redirect page will be removed once we finish manually checking all files on this account. Since we check over 100 websites per day, it can take about 2-4 hours to complete. If you are the owner of this website, you will get an email confirmation once it's done. If you are a visitor, please come back later.

Heysus
03-07-2011, 11:03 PM
You are seeing this page because the system administrator of 000webhost.com is currently checking this website for malicious content. This redirect page will be removed once we finish manually checking all files on this account. Since we check over 100 websites per day, it can take about 2-4 hours to complete. If you are the owner of this website, you will get an email confirmation once it's done. If you are a visitor, please come back later.


hmmm....ok, I guess come back later then :duh:

grats
03-07-2011, 11:26 PM
hmmm....ok, I guess come back later then :duh:

lol, bad timing

I'll check it out tonight when they probably won't be doing anything

Flight
03-08-2011, 12:36 AM
Bike ramps! I used to be into BMX alot. Now I'm more of mountain biking and downhill stuff. Nice job with all the drawing, looks like that took some time. :s

Heysus
03-08-2011, 12:58 AM
Bike ramps! I used to be into BMX alot. Now I'm more of mountain biking and downhill stuff. Nice job with all the drawing, looks like that took some time. :s

Yea the bike took like 4 hours to make :eek:. I'm actually more into downhill and freeride but there's no mountains near my house so I do a lot of dirt jumping.

Mr.Klean
03-08-2011, 03:06 AM
TBH I was expecting it to launch the bike over the ramp and see how far it goes. But if it does what it was supposed then good :P

grats
03-08-2011, 03:13 AM
TBH I was expecting it to launch the bike over the ramp and see how far it goes. But if it does what it was supposed then good :P

Lol too many flash games xD

looks like it works well