New to Java and trying to make a game

I'm really new to Java so please try not to roll over laughing at the following code. At the moment, as you can hopefully pick up from the code, i'm trying to get a shape(Polygon) to respond to keyboard events and i'm trying to make the object primarily respond to the cursor keys and to move the Polygon in the corresponding directions of the cursors. I'm stuck and i think i have initialised everything but i can't get the shape to move. I'm pretty sure i'm missing required code in the paint method.
<code>
import java.awt.*;
import java.awt.Event.*;
import java.applet.*;
import java.io.*;
public class PlodInvaders extends Applet implements Runnable {
Point start, end, fire, shield, target;
int co_x, x, y;
int co_y;
int current_x, current_y;
char current_key;
Font theFont;
Thread th;
Image ship;
Polygon p;
public void init() {
setBackground(Color.black);
// mouse co-oridnates
co_x = this.size().width + 5;
co_y = this.size().height + 5;
theFont= new Font("Courier", Font.BOLD, 24);
current_x = 150;
current_y = 100;
public void start() {
th = new Thread(this);
th.start();
public boolean KeyDown(Event evt, int key) {
switch(key) {
case Event.LEFT: current_x = current_x - 10; break;
case Event.RIGHT: current_x = current_x +10; break;
case Event.UP: current_y = current_y + 10; break;
case Event.DOWN: current_y = current_y - 10; break;
default: current_key = (char) key;
start = new Point(x, y);
repaint();
return true;
public boolean MouseDown(Event evt, int x, int y) {
fire = new Point(x, y);
return true;
public boolean MouseUp(Event evt, int x, int y) {
target = new Point(x, y);
repaint();
return true;
public void run() {
public void paint(Graphics g){
Polygon p = new Polygon();
g.setColor(Color.white);
p.addPoint(20,20);
p.addPoint(50,25);
p.addPoint(50,50);
p.addPoint(25,50);
p.addPoint(20,20);
g.fillPolygon(p);
</code>

this is not prefect and it is not a applet, but it will make you start. you can email if you have any qeustions
import java.awt.*;
import java.awt.event.*;
public class MoveP extends Frame implements KeyListener
     Polygon po = new Polygon();
        int ix=0;
     int iy=0;
public MoveP()  
     addWindowListener(new WindowAdapter()
    {     public void windowClosing(WindowEvent ev)
          {     dispose();
               System.exit(0);
     addKeyListener(this);
     setSize(300,372);
     po.addPoint(20,20);
     po.addPoint(50,25);
     po.addPoint(50,50);
     po.addPoint(25,50);
     po.addPoint(20,20);
     setVisible(true); 
public void paint(Graphics g)
     g.setColor(Color.black);
     for (int i=0; i < po.npoints; i++)
          po.xpoints[i]+=ix;
          po.ypoints[i]+=iy;
     g.fillPolygon(po);
public void keyTyped(KeyEvent e)
public void keyPressed(KeyEvent e)
     ix = 0;
     iy = 0;
     int k = e.getKeyCode();
     if (k == 40) iy++;
     if (k == 38) iy--;
     if (k == 39) ix++;
     if (k == 37) ix--;
     repaint();
public void keyReleased(KeyEvent e)
public static void main (String[] args)
     new MoveP();
      Noah

Similar Messages

  • I got a new credit card and tried to make an in-app purchase but forgot to update my info. It still says transaction is pending on the app

    I got a new credit card and tried to make an in-app purchase but forgot to update my info. It still says transaction is pending on the app

    Hi Danny724,
    Welcome to the Support Communities!
    I would suggest updating your billing information in the iTunes application, signing out of your account and signing back in; and then trying to complete the purchase.  If the issue persists, the iTunes Store Support Team can review your account with you.
    iTunes Store: Changing your payment information
    http://support.apple.com/kb/ht1918
    How to report an issue with your iTunes Store, App Store, Mac App Store, or iBookstore purchase
    http://support.apple.com/kb/HT1933?viewlocale=en_US
    Cheers,
    - Judy

  • Hello, I am new to java and I am trying to something cool

    Hello I am new to Java and I am trying to do something cool. I have written some code and it compiles nicely. Basically what I am trying to do is use ActionListeners with JTextArea to change the size of a rectangle thing.
    Here is my code. The problem is that when I push the submit button nothing happens. Any help you could give would be greatly appreciated.
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    class SecondGate extends JFrame implements ActionListener {
         float sxd = 190f;
         float dps = 190f;
         JTextArea Long = new JTextArea(1,3);
         JTextArea Short = new JTextArea(1,3);
         JLabel one = new JLabel("width");
         JLabel two = new JLabel ("Height");
         JButton Submit = new JButton("Submit");
    public SecondGate() {
    super("Draw a Square");
    setSize(500, 500);
         Submit.addActionListener(this);
         setLayout(new BorderLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         String Width = Float.toString(sxd);
         String Height = Float.toString(dps);
         Rect sf = new Rect(Width, Height);
         JPanel pane = new JPanel();
         pane.add(one);
         pane.add(Long);
         pane.add(two);
         pane.add(Short);
         pane.add(Submit);
         add(pane, BorderLayout.EAST);
         add(sf,BorderLayout.CENTER);
    setVisible(true);
         public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();
    if (source == Submit) {
              sxd = Float.parseFloat(Long.getText());
              dps = Float.parseFloat(Short.getText());
         repaint();
         public static void main(String[] arguments) {
    SecondGate frame = new SecondGate();
    class Rect extends JPanel {
         String Width;
         String Height;
    public Rect(String Width, String Height) {
    super();
    this.Width = Width;
    this.Height = Height;
    public void paintComponent(Graphics comp) {
    super.paintComponent(comp);
    Graphics2D comp2D = (Graphics2D)comp;
    comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
         BasicStroke pen = new BasicStroke(5F, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND);
    comp2D.setStroke(pen);
         comp2D.setColor(Color.blue);
         BasicStroke pen2 = new BasicStroke();
    comp2D.setStroke(pen2);
    Ellipse2D.Float e1 = new Ellipse2D.Float(235, 140, Float.valueOf(Width), Float.valueOf(Height));
    comp2D.fill(e1);
         GeneralPath fl = new GeneralPath();
         fl.moveTo(100F, 90F);
         fl.lineTo((Float.valueOf(Width) + 100F),90F);
         fl.lineTo((Float.valueOf(Width) + 100F),(Float.valueOf(Height) + 90F));
         fl.lineTo(100F,(Float.valueOf(Height) + 90F));
         fl.closePath();
         comp2D.fill(fl);
         }

    I got it to work. You were right about the method and references. Thank you
    Here is the working code for anyone who is interested in how to do this.
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    class SecondGate extends JFrame implements ActionListener {
    float sxd = 190f;
    float dps = 190f;
    JTextArea Long = new JTextArea(1,3);
    JTextArea Short = new JTextArea(1,3);
    JLabel one = new JLabel("width");
    JLabel two = new JLabel ("Height");
    JButton Submit = new JButton("Submit");
    String Width = Float.toString(sxd);
    String Height = Float.toString(dps);
    Rect sf = new Rect(Width, Height);
         public SecondGate() {
              super("Draw a Square");
              setSize(500, 500);
              Submit.addActionListener(this);
              setLayout(new BorderLayout());
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              JPanel pane = new JPanel();
              pane.add(one);
              pane.add(Long);
              pane.add(two);
              pane.add(Short);
              pane.add(Submit);
              add(pane, BorderLayout.EAST);
              add(sf,BorderLayout.CENTER);
              setVisible(true);
         public void actionPerformed(ActionEvent evt) {
              Object source = evt.getSource();
              if (source == Submit) {
              String Width = Long.getText();
              String Height = Short.getText();          
              sf.Width(Width);
              sf.Height(Height);
                   repaint();
         public static void main(String[] arguments) {
              SecondGate frame = new SecondGate();
         class Rect extends JPanel {
              String Width;
              String Height;
         public Rect(String Width, String Height) {
              super();
              this.Width = Width;
              this.Height = Height;
         String Width (String Width){
              this.Width = Width;
              return this.Width;
         String Height (String Height) {
              this.Height = Height;
              return Height;
         public void paintComponent(Graphics comp) {
              super.paintComponent(comp);
              Graphics2D comp2D = (Graphics2D)comp;
              comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
              BasicStroke pen = new BasicStroke(5F, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND);
              comp2D.setStroke(pen);
              comp2D.setColor(Color.blue);
              BasicStroke pen2 = new BasicStroke();
              comp2D.setStroke(pen2);
              Ellipse2D.Float e1 = new Ellipse2D.Float(235, 140, Float.valueOf(Width), Float.valueOf(Height));
              comp2D.fill(e1);
              GeneralPath fl = new GeneralPath();
              fl.moveTo(100F, 90F);
              fl.lineTo((Float.valueOf(Width) + 100F),90F);
              fl.lineTo((Float.valueOf(Width) + 100F),(Float.valueOf(Height) + 90F));
              fl.lineTo(100F,(Float.valueOf(Height) + 90F));
              fl.closePath();
              comp2D.fill(fl);
              }

  • TS3297 Does anyone know how to reset your security questions?? I loaded an itunes gift card on new ipod but when trying to make a purchase, itunes is asking us the incorrect security questions?!(for 1st time purchase)

    Does anyone know how to reset your security questions?? I loaded an itunes gift card on new ipod but when trying to make a purchase, itunes is asking us the incorrect security questions?! (for 1st time purchase)  I know the questions are not what I chose because I wrote the questions & answers down when setting up the ipod. Any ideas??!!!

    Reset Security Questions
    Frequently asked questions about Apple ID
    Manage My Apple ID
    Or you can email iTunes Support at iTunes Store Support.
    If all else fails:
      1. Go to: Apple Express Lane;
      2. Under Product Categories choose iTunes;
      3. Then choose iTunes Store;
      4. Then choose Account Management;
      5. Now choose iTunes Store Security and answer the bullet questions, then click
          Continue;
      6. Sign in with your Apple ID and press Continue;
      7. Under Contact Options fill out the information and advise iTunes that you would
          like your security/challenge questions reset;
      8. Click Send/Continue.
    You should get a response within 24 hours by email.
    In the event you are unsuccessful then contact AppleCare - Contacting Apple for support and service.
    Another user had success doing the following:
    I got some help from an apple assistant on the phone. It is kind of round about way to get in.
    Here is what he said to do and it is working for me...
      a. on the device that is asking you for the security questions go to "settings", > "store" >
          tap the Apple ID and choose view"Apple ID" and sign in.
      b. Tap on payment information and add a credit/debit card of your preference then select
          "done", in the upper right corner
      c. sign out and back into iTunes on the device by going to "settings"> "store" > tap the
          Apple ID and choose "sign-out" > Tap "sign -in" > "use existing Apple ID" and you
          should be asked to verify your security code for the credit /debit card and NOT the
          security questions.
      d. At this time you can remove the card by going back in to edit the payment info and
          selecting "none" as the card type then saving the changes by selecting "done". You
          should now be able to use your iTunes store credit without answering the security
          questions.
    It's working for me ...I just have to put in my 3 digit security pin from the credit card I am using.
    Good Luck friends!

  • I am a new mac user and tried to update my firefox but now i have multiple foxes and a half fox fix something? how do i get it all together in one and be sure i have current version on only?

    i am a new mac user and tried to update my firefox but now i have multiple foxes and a half fox fix something? how do i get it all together in one and be sure i have current version on only?

    Hi there
    I think in this case, you may be best to speak with Microsoft about this, as it does seem like an issue with Office, and not your Mac.
    Follow this link, to find your local Microsoft Support contact number.
    Or perhaps create a new topic in the Microsoft Support Communities
    Good luck

  • Got a new iPhone today and tried to restore from my old phone using icloud but kept coming up with a message that said problem with my icloud back up please help

    Got a new iPhone today and tried to restore from my old phone using icloud but kept coming up with a message that said problem with my icloud back up please help

    Hi there Clairemcaula,
    You may find the troubleshooting steps in the article below helpful.
    iCloud: Troubleshooting restoring an iCloud backup
    http://support.apple.com/kb/ts4036
    -Griff W. 

  • Im new to java and hope somebody can help me with my question

    Hi!
    Im quite new to java and I just have some simple questions..
    can someone please tell what kinds of error are considered as language violation for java? where can I find more info about these errors?
    can someone give me a simple example on a kind of error that cannot be caught in both compilation and runtime? I hope someone can help me out. Thanks in advance!!

    knightz211 wrote:
    Im just asking about errors that might go against the language definition but cant be detected.. If it "goes against the language defintion," it will be detected by the compiler. That's half of the compiler's job is to tell you what you've done that violates the language spec.
    because it confuses me when they say that such errors might occur so I just want to know what might these errors be.. sorry for that..Who's "they"? What exactly* did "they" say about "such errors"? It sounds to me like you're just confused, and you think there's something mysterious and inexplicable going on but you don't know what and don't even know what you're asking.
    I would suggest not worrying about hypothetical problems that you can't even put into words and focussing on learning Java. Along the way, if you encounter real, specific, actual problems, ask about them, and you'll probably get answers about the problems themselves and the language or theory behind them.

  • I just got a new iPhone 4S and tried to restore but it didn't finished downloading all the apps, tried to reset and restore again, but it wouldn't start over. What can I do?

    I just got a new iPhone 4S and tried to restore but it didn't finished downloading all the apps, tried to reset and restore again, but it wouldn't start over. What can I do?

    http://support.apple.com/kb/ts3694
    Check USB connections
    If there’s an issue with the USB port, cable, dock, or hub, or if the device becomes disconnected during restore, try troubleshooting the USB connection, then troubleshooting your security software.
    Common errors: 13, 14, 1600-1629, 1643-1650, 2000-2009, 4000, 4005, 4013, 4014, 4016, “invalid response”, and being prompted to restore again after a restore completes.
    To narrow down the issue, you can also change up your hardware:
    Use another USB cable.
    Plug your cable into a different USB port on your computer.
    Try a different dock connector (or no dock).
    Add (or remove) a USB hub between your device and computer.
    Connect your computer directly to your Internet source, with no routers, hubs, or switches.
    If you see your error after changing your USB connections, continue to the next section to troubleshoot your hardware.

  • I installed a new hard drive and trying to reinstall FCP2 and on the install disc i get the following message - "You can't open the application FC Studio.mpkg because power PC applications are no longer supported"

    I installed a new hard drive and trying to reinstall FCP2 and on the install disc i get the following message - "You can't open the application FC Studio.mpkg because power PC applications are no longer supported"

    Because you are running lion.. FCS 6 is a Power PC app and Lion doesn't support Power PC anymore.. So you can't install it.. Three options..
    1. Revert back to Snow Leopard.
    2. Purchase FCP X... 
    if you can't stand FCP X
    3. Call apple directly, 1800-MYAPPLE and ask to purachse FCS 3 for $999... (its FCP 7)
    I just got FCS 3 today through apple phone sales..

  • Just bought a new IPad mini and trying to transfer game center files from Ipod.  How do I do this?

    Just bought a new IPad mini and trying to transfer game center files from Ipod.  How do I do this without losing level progress?

    Restore from your backup of your iPod on your computer.

  • I just set an administrators name (my full name) and password for parental controls (using the one I always use) and now after restarting and trying to make changes to aprental controls it does not recognize my password

    I just set an administrators name (my full name) and password for parental controls (using the one I always use) and now after restarting and trying to make changes to aprental controls it does not recognize my password

    Hello, do you have another admin account to log into for a test?

  • I just bought my new iphone 4s and when I make a call or receive a call i can barely hear them. I have gone to settings and to sounds and turn it all the way up, but it doesn't help. What else can I do?

    I just bought my new iphone 4s and when I make a call or receive a call I can barely hear them.

    Plastic packaging film?

  • I am creating webpages with frontpage and trying to make collapsible lists. The lists collapse and work correctly in "preview" mode, but do not work when accessed through firefox or explorer 8. Does anyone know what I am doing wrong?

    I am creating webpages with frontpage and trying to make collapsible lists. The lists collapse and work correctly in "preview" mode, but do not work when accessed through firefox or explorer 8. Does anyone know what I am doing wrong?

    Welcome to the 'wonderful' world of HTML5 e-learning.
    In my view, from the issues I am seeing everywhere, HTML5 output from rapid e-learning tools such as Captivate is not ready for the kind of mobile device e-learning you want to develop.
    Sorry to be pessimistic, but you may need to consider going back to the drawing board and coding it from the ground up.

  • I have an iphone 4s and have installed the new ios download and can no longer find games and other apps that i have downloaded. Where would they be or have they been wiped?

    I have an iphone 4s and have installed the new ios download and can no longer find games and other apps that i have downloaded. Where would they be or have they been wiped?

    Hi,
    don´t be worry, this would not be a problem.
    You have to take the same Aplle ID on both devices, then you can sync them via iTunes by using all universal apps on each device, create playlists, albums and all the things you need and sync it, no problem...
    cu
    dienoppe

  • I synced my iPod to a new music folder and now all of my games (Klondike, Vortex, etc) are gone - how do I get them back?

    I synced my iPod to a new music folder and now all of my games (Klondike, Vortex, etc) are gone - how do I get them back? Please help! Thanks!

    Aren't those the default games that come with the iPod Classic?  If so, they won't be in your iTunes library, but only on your iPod Classic itself as they are built in the iPod's software.
    B-rock

Maybe you are looking for