PDA

View Full Version : [JAVA] Help with code


Lawn Gnome
10-18-2005, 02:24 AM
Well i got this code off the internet and want to make it do some other stuff but it dosent work atm. here the code, its 3 different pages.


// *******************************************************************
// FILE: MovePanel.java
//
// The display panel for a key events program -- arrow keys are used
// to move a stick figure around, the g key is used to make the figure
// grow by 50% (increase in height by 50%), the s key causes the
// figure to shrink (to half its size)
// *******************************************************************


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import StickFigure;


public class MovePanel extends JPanel
{
private final int APPLET_WIDTH = 600;
private final int APPLET_HEIGHT = 400;

private final int JUMP = 5; // number of pixels moved each step

// the following give the initial parameters for the figure
private final int START_CENTER = APPLET_WIDTH/2;
private final int START_BOTTOM = APPLET_HEIGHT - 40;
private final int SIZE = APPLET_HEIGHT / 2;

private StickFigure stickMan;

// ----------------------------------------------------
// Initialize the applet
// ----------------------------------------------------
public MovePanel (JApplet applet)
{

applet.addKeyListener(new StickListener());

stickMan = new StickFigure (START_CENTER, START_BOTTOM,
Color.yellow,SIZE);

// other initializations
setBackground (Color.black);
setPreferredSize (new Dimension (APPLET_WIDTH, APPLET_HEIGHT));

}

// ---------------------------------------
// draw the figure
// ---------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent (page);
stickMan.draw (page);
}


// **********************************************************
// Represents a listener for keyboard activity.
// **********************************************************
private class StickListener implements KeyListener
{
// --------------------------------------------------
// Handle a key-pressed event: arrow keys cause the
// figure to move horizontally or vertically; the g
// key causes the figure to "grow", the s key causes
// the figure to shrink, the u key causes arms and
// legs to go up, m puts them in the middle, and d
// down.
// --------------------------------------------------
public void keyPressed (KeyEvent event)
{
switch (event.getKeyCode())
{
case KeyEvent.VK_LEFT:
stickMan.move(-1*JUMP, 0);
break;
case KeyEvent.VK_RIGHT:
stickMan.move(JUMP, 0);
break;
case KeyEvent.VK_G:
stickMan.grow (1.5);
break;
default:
}

repaint();
}

// --------------------------------------------
// Define empty bodies for key event methods
// not used
// --------------------------------------------
public void keyTyped (KeyEvent event) {}
public void keyReleased (KeyEvent event) {}
}
}


// *****************************************************************
// MoveStickMan.java
//
// Uses key events to move a stick figure around.
// *****************************************************************

import javax.swing.*;

public class MoveStickMan extends JApplet
{
public void init()
{
getContentPane().add (new MovePanel(this));
}
}


// *******************************************************
// StickFigure.java (modified version of Listing 4.16,
// Lewis and Loftus)
//
// Represents a graphical stick figure
// *******************************************************

import java.awt.*;

public class StickFigure
{
private int baseX; // center of the figure
private int baseY; // bottom of the feet
private Color color; // color of the figure
private int height; // height of the figure
private int headW; // width of the head
private int legLength; // length of the legs
private int legPosition;// # pixels the legs are up from vertical
private int armLength; // horizontal length of the arms
private int armToFloor; // distance from base to arms
private int armPosition;// # pixels arm is above/below horizontal

// --------------------------------------------------------------
// Construct a stick figure given its four attributes
// --------------------------------------------------------------
public StickFigure (int center, int bottom, Color shade, int size)
{
baseX = center;
baseY = bottom;
color = shade;
height = size;

// define body positions proportional to height
headW = height / 5;
legLength = height / 2;
armToFloor = 2 * height / 3;
armLength = height / 3;

// set initial position of arms and legs
armPosition = -20;
legPosition = 15;
}

// ----------------------------------------------
// Draw the figure
// ----------------------------------------------
public void draw (Graphics page)
{
// compute y-coordinate of top of head
int top = baseY - height;

page.setColor (color);

// draw the head
page.drawOval(baseX-headW/2, top, headW, headW);

// draw the trunk
page.drawLine (baseX, top+headW, baseX, baseY - legLength);

// draw the legs
page.drawLine(baseX, baseY-legLength, baseX-legPosition, baseY);
page.drawLine(baseX, baseY-legLength, baseX+legPosition, baseY);

// draw the arms
int startY = baseY - armToFloor;
page.drawLine(baseX, startY, baseX-armLength, startY-armPosition);
page.drawLine(baseX, startY, baseX+armLength, startY-armPosition);
}

// -----------------------------------------------------
// Move the figure -- first parameter gives the
// number of pixels over (to right if over is positive,
// to the left if over is negative) and up or down
// (down if the parameter down is positive, up if it is
// negative)
// -----------------------------------------------------
public void move (int over, int down)
{
baseX += over;
baseY += down;
}

// ----------------------------------------------------
// Increase the height by the given factor (if the
// factor is > 1 the figure will "grow" else it will
// shrink)
// ----------------------------------------------------
public void grow (double factor)
{
height = (int) (factor * height);

// reset body parts proportional to new height
headW = height / 5;
legLength = height / 2;
armToFloor = 2 * height / 3;
armLength = height / 3;
}

// -------------------------------------------------
// set the legPosition (dist. from vertical) to
// new value
// -------------------------------------------------
public void setLegPosition (int newPosition)
{
legPosition = newPosition;
}

// ----------------------------------------
// set the arm position to the new value
// ----------------------------------------
public void setArmPosition (int newPos)
{
armPosition = newPos;
}
}


it says that the error is "line 14 of the MovePanel.java it exspected a '.' "

nikkiH
10-18-2005, 03:06 AM
Your class extends JPanel, not applet.
You need to call the applet methods on the applet object.
(I'm not into applets much, but a quick search of the javadoc API shows JPanel (http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JPanel.html) has no setPreferredSize method)

public MovePanel (JApplet applet)
{

applet.addKeyListener(new StickListener());

stickMan = new StickFigure (START_CENTER, START_BOTTOM,
Color.yellow,SIZE);

// other initializations
applet.setBackground (Color.black);
applet.setPreferredSize (new Dimension (APPLET_WIDTH, APPLET_HEIGHT));

}