Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2007, 05:42 PM
zoe zoe is offline
Member
 
Join Date: Jul 2007
Posts: 40
zoe is on a distinguished road
Error: it exspected a '.'
Well i got this code off the Internet and want to make it do some other stuff, but it doesn't work atm. here the code

Code:
// 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); }
Code:
// 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) {} } }
Code:
// 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 // // 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 '.' "
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-04-2007, 02:57 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Comment this line out or remove it.
Code:
//import StickFigure;
Compiles and runs okay.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 07:55 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org