Reputation: 31
I made a small spaceship using the draw method in applets.
And I am using x and y to move the ship around the applet box.
So I thought I'd further better my self and trying adding a textfield
and a button to change the x n y on the fly, but when I click the button
the spaceship stays still. Here's my code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.applet.*;
public class SpaceshipInAJApplet extends Applet implements ActionListener {
int x = 65;
int y = 100;
TextField x1,y1;
Button enter;
public void init() {
setBackground(new Color(0xabcdef));
x1 = new TextField("X : " + x, 5);
y1 = new TextField("Y : " + y, 5);
add(x1);
add(y1);
setSize(500, 500);
enter = new Button("Enter");
add(enter);
enter.addActionListener(this);
}
// Creates Class Called paint to draw objects into the applet
public void paint(Graphics g) {
int []flameX = {x+65,x+75,x+55};
int []flameY = {y+185,y+161,y+161};
int []wingX = {x+65,x+120,x+15};
int []wingY = {y+50,y+150,y+150};
super.paint(g);
// Draw Wing
g.setColor(Color.LIGHT_GRAY);
g.fillPolygon(wingX, wingY, 3);
// Draw Wing Border
g.setColor(Color.BLACK);
g.drawPolygon(wingX, wingY, 3);
// Draw Body
g.setColor(Color.LIGHT_GRAY);
g.fillRect(x+50, y+85, 30, 65);
// Draw Border
g.setColor(Color.BLACK);
g.drawRect(x+50, y+85, 30, 65);
// Draw Rocket
g.setColor(Color.LIGHT_GRAY);
g.fillRect(x+55, y+150, 20, 10);
// Draw Border
g.setColor(Color.BLACK);
g.drawRect(x+55, y+150, 20, 10);
// Draw Rocket Flame
g.setColor(Color.ORANGE);
g.fillPolygon(flameX, flameY, 3);
}
public void actionPerformed(ActionEvent ae)
{
try {
int tempX = Integer.parseInt(x1.getText());
int tempY = Integer.parseInt(y1.getText());
}
catch(NumberFormatException ex)
{
System.out.println("Exception : "+ex);
}
}
}
Upvotes: 0
Views: 344