# can someone fix this?

• 05-14-2008, 11:01 PM
sharkbait11
can someone fix this?
hey so me and a freind are working on a game
but both of us are confused about whats wrong...
we have everything needed but it wont work

the blue triangle is supposed to move across a horizantal axis on the screen under the commands of the left and right arrow keys

Quote:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.*;
import java.net.*;
import java.util.*;
import java.applet.Applet;
import java.applet.AudioClip;
import javax.swing.*;

class GalaxyWars2
{
static int height;
static int width;

Polygon shape;
boolean active;
double angle;
double deltaAngle;
double currentX, currentY;
double deltaX, deltaY;
Polygon sprite;

public GalaxyWars2()
{
this.shape = new Polygon();
this.active = false;
this.angle = 0.0;
this.deltaAngle = 0.0;
this.currentX = 0.0;
this.currentY = 0.0;
this.deltaX = 0.0;
this.deltaY = 0.0;
this.sprite = new Polygon();
}

this.angle += this.deltaAngle;
if (this.angle < 0)
this.angle += 2 * Math.PI;
if (this.angle > 2 * Math.PI)
this.angle -= 2 * Math.PI;
this.currentX += this.deltaX;
if (this.currentX < -width / 2)
this.currentX += width;
if (this.currentX > width / 2)
this.currentX -= width;
this.currentY -= this.deltaY;
if (this.currentY < -height / 2)
this.currentY += height;
if (this.currentY > height / 2)
this.currentY -= height;
}

public void render() {

int i;

this.sprite = new Polygon();
for (i = 0; i < this.shape.npoints; i++)
this.sprite.addPoint((int) Math.round(this.shape.xpoints[i] * Math.cos(this.angle) + this.shape.ypoints[i] * Math.sin(this.angle)) + (int) Math.round(this.currentX) + width / 2,
(int) Math.round(this.shape.ypoints[i] * Math.cos(this.angle) - this.shape.xpoints[i] * Math.sin(this.angle)) + (int) Math.round(this.currentY) + height / 2);
}

}

public class GWars2 extends Applet implements Runnable, ActionListener
{

int Score;
int shipsLeft=5;
int shipCounter;

GalaxyWars2 ship;
GalaxyWars2 enemy;

boolean playing;

boolean left = false;
boolean right = false;
boolean up = false;

Dimension offDimension;
Image offImage;
Graphics offGraphics;

Font font = new Font("Earwig Factory", Font.BOLD, 25);
FontMetrics fm;
int fontWidth;
int fontHeight;

public String getAppletInfo() {

return("Galaxy Wars 2, Copyright 2007 by Aaron , Tyler and Justin");
}
public void init()
{
Graphics g;
Dimension d;
int i;

g = getGraphics();
d = size();
GalaxyWars2.width = d.width;
GalaxyWars2.height = d.height;

//Makes our ships
ship=new GalaxyWars2();

enemy=new GalaxyWars2();

initGame();
endGame();
}

public void initGame()
{
Score = 0;
initShip();
playing = true;
}
//ends game
public void endGame()
{
playing = false;
stopShip();
}
//stops our ship
public void stopShip()
{
ship.active = false;
}

public void initShip()
{
ship.active = true;
ship.angle = 0.0;
ship.deltaAngle = 0.0;
ship.currentX = 0.0;
ship.currentY = 0.0;
ship.deltaX = 0.0;
ship.deltaY = 0.0;
ship.render();
}

double dx, dy;

if (!playing)
return;

if (left) {
ship.deltaX +=20;

ship.render();

}
if (right) {
ship.angle -= Math.PI / 16.0;
if (ship.angle < 0)
ship.angle += 2 * Math.PI;

dx = -Math.sin(ship.angle);
dy = Math.cos(ship.angle);

if (ship.active) {
ship.render();
}

else
if (--shipCounter <= 0 || Score < 0)
if (shipsLeft > 0) {
initShip();
}

else
endGame();
}
}
public void run() {
int DELAY = 5;

int i, j;
long startTime;

startTime = System.currentTimeMillis();

if (1 <0) {

int Score =1;

if (Score <= 0)
{
endGame();
}

}

repaint();
try {
startTime += DELAY;
}
catch (InterruptedException e) {
break;
}
}

}

public void start()
{
{
}
}

public void stop()
{
{
}
}
public void actionPerformed(ActionEvent e) {
int ShipCC=0;
if ("Ship".equals(e.getActionCommand()))
{
System.out.println("Confirm");
} }
public boolean keyUp(Event e, int key)
{

if (key==1005) {
up=true;
}

if (key==1007) {
left=true;
System.out.println("Left key up");
}
if (key==1006) {
right=false;
}

return true;
}

public boolean keyDown(Event e, int key) {

// Check if any cursor keys have been pressed and set flags.
if (key==1005) {
up=false;
}
if (key==1007) {
left=false;
System.out.println("Left key pressed");
}
if (key==1006) {
right=true;
}

return true;
}
public void paint(Graphics g) {

update(g);
}

public void update(Graphics g)
{
Dimension d = size();
int i;

if (offGraphics == null || d.width != offDimension.width || d.height != offDimension.height) {
offDimension = d;
offImage = createImage(d.width, d.height);
offGraphics = offImage.getGraphics();
}
offGraphics.setColor(Color.black);
offGraphics.fillRect(0, 0, d.width, d.height);

if (shipsLeft >= 5)
{
offGraphics.setColor(Color.blue);
offGraphics.fillPolygon(ship.sprite);
}
if(shipsLeft == 4)
{
offGraphics.setColor(Color.yellow);
offGraphics.fillPolygon(ship.sprite);
}
if(shipsLeft == 3)
{
offGraphics.setColor(Color.orange);
offGraphics.fillPolygon(ship.sprite);
}
if(shipsLeft == 2)
{
offGraphics.setColor(Color.red);
offGraphics.fillPolygon(ship.sprite);
}
if(shipsLeft == 1)
{
offGraphics.setColor(Color.white);
offGraphics.fillPolygon(ship.sprite);
}

offGraphics.setColor(new Color(0, 0, 0));
offGraphics.drawPolygon(ship.sprite);
offGraphics.drawLine(ship.sprite.xpoints[ship.sprite.npoints - 1], ship.sprite.ypoints[ship.sprite.npoints - 1],
ship.sprite.xpoints[0], ship.sprite.ypoints[0]);

g.drawImage(offImage, 0, 0, this);
}
}
• 05-16-2008, 04:07 AM
hardwired
How to handle deprecated warnings:
Code:

```C:\jexp>javac gw2.java Note: gw2.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. C:\jexp>javac -Xlint:deprecation gw2.java gw2.java:48: warning: [deprecation] size() in java.awt.Component has been deprecated         d = size();             ^ gw2.java:178: warning: [deprecation] stop() in java.lang.Thread has been deprecated             thethread.stop();                     ^ gw2.java:191: warning: [deprecation] keyUp(java.awt.Event,int) in java.awt.Component has b een deprecated     public boolean keyUp(Event e, int key)                   ^ gw2.java:208: warning: [deprecation] keyDown(java.awt.Event,int) in java.awt.Component has  been deprecated     public boolean keyDown(Event e, int key) {                   ^ gw2.java:230: warning: [deprecation] size() in java.awt.Component has been deprecated         Dimension d = size();                       ^ 5 warnings```
Try right arrow key now...
Code:

```//  <applet code="GW2" width="400" height="400"></applet> import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.*; import java.net.*; import java.util.*; import java.applet.Applet; import java.applet.AudioClip; import javax.swing.*; public class GW2 extends Applet implements Runnable, ActionListener {     int Score;     int shipsLeft=5;     int shipCounter;     GalaxyWars2 ship;     GalaxyWars2 enemy;     boolean playing;     boolean left = false;     boolean right = false;     boolean up = false;     Dimension offDimension;     Image offImage;     Graphics offGraphics;     Thread thethread;     Font font = new Font("Earwig Factory", Font.BOLD, 25);     FontMetrics fm;     int fontWidth;     int fontHeight;     public String getAppletInfo() {         return("Galaxy Wars 2, Copyright 2007 by Aaron , Tyler and Justin");     }     public void init()     {         Graphics g;         Dimension d;         int i;         g = getGraphics();         d = size();         GalaxyWars2.width = d.width;         GalaxyWars2.height = d.height;         //Makes our ships         ship=new GalaxyWars2();         ship.shape.addPoint(0, 20);         ship.shape.addPoint(20,10);         ship.shape.addPoint(0,0 );         ship.shape.addPoint(0,20 );         enemy=new GalaxyWars2();         enemy.shape.addPoint(250, 250);         enemy.shape.addPoint(233,342 );         enemy.shape.addPoint(234,84 );         enemy.shape.addPoint(340,230 );         enemy.shape.addPoint(250,250 );         initGame();         endGame();         requestFocusInWindow();     }     public void initGame()     {         Score = 0;         initShip();         playing = true;     }     public void endGame()     {         playing = false;         stopShip();     }     public void stopShip()     {         ship.active = false;     }     public void initShip()     {         ship.active = true;         ship.angle = 0.0;         ship.deltaAngle = 0.0;         ship.currentX = 0.0;         ship.currentY = 0.0;         ship.deltaX = 0.0;         ship.deltaY = 0.0;         ship.render();     }     //updates ships cordinates     public void updateShip() {         double dx, dy;         if (!playing)             return;         if (left) {             ship.deltaX += 20;             ship.advance();             System.out.println("ship.deltaX = " + ship.deltaX);             ship.render();         }         if (right) {             ship.angle -= Math.PI / 16.0;             if (ship.angle < 0)                 ship.angle += 2 * Math.PI;             dx = -Math.sin(ship.angle);             dy =  Math.cos(ship.angle);             if (ship.active) {                 ship.advance();                 ship.render();             }             else if (--shipCounter <= 0 || Score < 0)                 if (shipsLeft > 0) {                     initShip();                 }             else                 endGame();         }     }     public void run() {         int DELAY = 5;         int i, j;         long startTime;         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);         startTime = System.currentTimeMillis();         while (Thread.currentThread() == thethread) {             if (1 < 0) {                 updateShip();                 int Score = 1;                 if (Score <= 0)                 {                     endGame();                 }             }             updateShip();             repaint();             try {                 startTime += DELAY;                 Thread.sleep(//Math.max(0,                             //startTime - System.currentTimeMillis()));                             250);             } catch (InterruptedException e) {                 break;             }         }     }     public void start()     {         if (thethread == null)         {             playing = true;             thethread = new Thread(this);             thethread.start();         }     }     public void stop()     {         if (thethread != null)         { //            thethread.stop();  // deprecated method             playing = false;             thethread.interrupt();             thethread = null;         }     }     public void actionPerformed(ActionEvent e) {         int ShipCC=0;         if ("Ship".equals(e.getActionCommand()))         {             System.out.println("Confirm");         }     }     public boolean keyUp(Event e, int key)     {         if (key==1005) {             up=true;         }         if (key==1007) { //            left=true;             left=false;             System.out.println("Left key up");         }         if (key==1006) {             right=false;         }         return true;     }     public boolean keyDown(Event e, int key) {         // Check if any cursor keys have been pressed and set flags.         if (key==1005) {             up=false;         }         if (key==1007) { //            left=false;             left=true;             System.out.println("Left key pressed");         }         if (key==1006) {             right=true;         }         return true;     }     public void paint(Graphics g) {         update(g);     }     public void update(Graphics g)     {         Dimension d = //size();  // deprecated method                       getSize();         int i;         if (offGraphics == null || d.width != offDimension.width ||                   d.height != offDimension.height) {             offDimension = d;             offImage = createImage(d.width, d.height);             offGraphics = offImage.getGraphics();         }         offGraphics.setColor(Color.black);         offGraphics.fillRect(0, 0, d.width, d.height);         if (shipsLeft >= 5)         {             offGraphics.setColor(Color.blue);             offGraphics.fillPolygon(ship.sprite);         }         if(shipsLeft == 4)         {             offGraphics.setColor(Color.yellow);             offGraphics.fillPolygon(ship.sprite);         }         if(shipsLeft == 3)         {             offGraphics.setColor(Color.orange);             offGraphics.fillPolygon(ship.sprite);         }         if(shipsLeft == 2)         {             offGraphics.setColor(Color.red);             offGraphics.fillPolygon(ship.sprite);         }         if(shipsLeft == 1)         {             offGraphics.setColor(Color.white);             offGraphics.fillPolygon(ship.sprite);         }         offGraphics.setColor(//new Color(0, 0, 0));                             Color.red);         offGraphics.drawPolygon(ship.sprite);         offGraphics.drawLine(ship.sprite.xpoints[ship.sprite.npoints - 1],                                    ship.sprite.ypoints[ship.sprite.npoints - 1],                             ship.sprite.xpoints[0], ship.sprite.ypoints[0]);         g.drawImage(offImage, 0, 0, this);     } } class GalaxyWars2 {     static int height;     static int width;     Polygon shape;     boolean active;     double angle;     double deltaAngle;     double currentX, currentY;     double deltaX, deltaY;     Polygon sprite;     public GalaxyWars2()     {         this.shape = new Polygon();         this.active = //false;                       true;         this.angle = 0.0;         this.deltaAngle = 0.0;         this.currentX = 0.0;         this.currentY = 0.0;         this.deltaX = 0.0;         this.deltaY = 0.0;         this.sprite = new Polygon();     }     public void advance() {         this.angle += this.deltaAngle;         if (this.angle < 0)             this.angle += 2 * Math.PI;         if (this.angle > 2 * Math.PI)             this.angle -= 2 * Math.PI;         this.currentX += this.deltaX;         if (this.currentX < -width / 2)             this.currentX += width;         if (this.currentX > width / 2)             this.currentX -= width;         this.currentY -= this.deltaY;         if (this.currentY < -height / 2)             this.currentY += height;         if (this.currentY > height / 2)             this.currentY -= height;     }     public void render() {         int i;         this.sprite = new Polygon();         for (i = 0; i < this.shape.npoints; i++)             this.sprite.addPoint(                 (int) Math.round(this.shape.xpoints[i] * Math.cos(this.angle) +                     this.shape.ypoints[i] * Math.sin(this.angle)) +                     (int) Math.round(this.currentX) + width / 2,                 (int) Math.round(this.shape.ypoints[i] * Math.cos(this.angle) -                    this.shape.xpoints[i] * Math.sin(this.angle)) +                     (int) Math.round(this.currentY) + height / 2);     } }```