0

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);
        }
    }
}
SomeSickJoke
  • 31
  • 1
  • 1
  • 7
  • Yes. You do nothing in the `actionPerformed` besides create and set the values of two ints. Do something with them like you want to? – Martin Dinov Jan 26 '14 at 20:41
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Jan 27 '14 at 02:13

1 Answers1

2

Add This.repaint() in the actionPerformed() method,

java-love
  • 516
  • 1
  • 8
  • 23