The Artima Developer Community
Sponsored Link

Java Answers Forum
What is wrong with my start method?

13 replies on 1 page. Most recent reply: Mar 15, 2002 1:28 PM by Patti

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 13 replies on 1 page
Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

What is wrong with my start method? Posted: Mar 13, 2002 1:36 PM
Reply to this message Reply
Advertisement
Can't get this to work. Please tell me how to adjust the start method.


import java.applet.Applet;
import java.awt.*;
 
// An applet to perform a simple animation.
 
public class Drive extends Applet{
	
     private Graphics page;
     private Car car;
     
   
     //sets up the animation.
     public void init() {
     	setVisible (true);
     	setSize (450,250);
       	page = getGraphics();
     	page.setXORMode (getBackground());
     	       
     }//method init
     
     
     public void start(){
             	
      car.drive();
    
          	  	
    }//method start
  }//class Drive
  
  //Represents the car that moves across the screen.
  
  class Car {
  	
  	private final int MOVE = 2;
  	private final int RIGHT_SIDE = 450;
  	private final int NUM_POINTS = 10;
  	private final int PAUSE = 100000;
  	
  	private int[] car_x = {80,70,70,90,95,140,150,175,180,170};
  	private int[] car_y = {190,190,170,170,155,155,170,170,190,190};
  	
  	private int [] car_x_update = new int [NUM_POINTS];
  	
  	//Performs the animation
  	
  	public void drive (Graphics page) {
  		
  		//Loop until car drives off the right side.
  		for (int offset=1; offset < RIGHT_SIDE; offset += MOVE) {
  			draw_car (offset,page); //draw the car
  			for (int pause=1; pause < PAUSE; pause++);
  			draw_car (offset,page); //erase the car
  		}
  		
}//method drive
 
   //Draws the car at a particular x offset.
    public void draw_car (int offset, Graphics page) {
    	
         page.setColor (Color.red);
	     page.drawPolyline (car_x, car_y,10);//car
    	 page.drawArc (80+offset,180,20,20,0,180);//rear wheel well
	     page.drawArc (150+offset,180,20,20,0,180);//front wheel well
	     page.drawLine (100+offset,190,150+offset,190);
	     page.setColor (Color.black);
	     page.drawOval (83+offset,183,14,14);//rear wheel
	     page.drawOval (153+offset,183,14,14); //front wheel
	     page.drawLine (81+offset,170,81+offset,150);//antenna
	     
       	//Update the x coordinates.    	
        for (int scan=0; scan < NUM_POINTS; scan++)
        car_x_update[scan] = car_x[scan] + offset;
        
    }//method draw_car
    
} //class Car


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: What is wrong with my start method? Posted: Mar 13, 2002 1:42 PM
Reply to this message Reply
I don't think there was anything wrong with the start method.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: What is wrong with my start method? Posted: Mar 13, 2002 1:54 PM
Reply to this message Reply
Maybe the battery is dead? ;-)

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: What is wrong with my start method? Posted: Mar 13, 2002 3:23 PM
Reply to this message Reply
VERY funny !!!

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: What is wrong with my start method? Posted: Mar 13, 2002 5:56 PM
Reply to this message Reply
Well, I got it to start by changing a lot of things to static (don't understand that) BUT now only the wheels drive off, not the car. It seems it doesn't pick up the offset part of the car_x[scan] + offset. Are you guys gonna help me?

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: What is wrong with my start method? Posted: Mar 13, 2002 6:10 PM
Reply to this message Reply
Well, changing things to static is probably not the best design practice, even if it does seem to get things working. Why don't you post your latest code and we'll take a look (well, I can say I will, at least)...

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: What is wrong with my start method? Posted: Mar 13, 2002 6:37 PM
Reply to this message Reply
Here it is. It's just not picking up the very last offset. I've tried everything-maybe static is preventing it. I don't know. I don't really understand the difference in static or not static yet. That's one of those things that hasn't sunk in.
The other is the difference in public and private-well I sort of understand that a little. Thanks for help.

import java.applet.Applet;
import java.awt.*;
 
 
// An applet to perform a simple animation.
 
public class Drive extends Applet{
	
     private Graphics page;
 
     
   
     //sets up the animation.
     public void init() {
     	setVisible (true);
     	setSize (450,250);
       	page = getGraphics();
     	page.setXORMode (getBackground());
        
     }//method init
     
     
     public void start(){
     	
     	
        Car.drive(page);
    
          	  	
    }//method start
  }//class Drive
  
  //Represents the car that moves across the screen.
  
  class Car {
  	
  	private final static int MOVE = 2;
  	private final static int RIGHT_SIDE = 450;
  	private final static int NUM_POINTS = 10;
  	private final static int PAUSE = 1000000;
  	
  	
  	private static int[] car_x = {80,70,70,90,95,140,150,175,180,170};
  	private static int[] car_y = {190,190,170,170,155,155,170,170,190,190};
  	
  	private static int [] car_x_update = new int [NUM_POINTS];
 
  	//Performs the animation
  	
  	public static void drive (Graphics page) {
  		 
  		//Loop until car drives off the right side.
  		for (int offset=1; offset < RIGHT_SIDE; offset += MOVE) {
  			draw_car (offset,page); //draw the car
  			for (int pause=1; pause < PAUSE; pause++);
  			draw_car (offset,page); //erase the car
  		}
  		
}//method drive
 
   //Draws the car at a particular x offset.
    public static void draw_car (int offset, Graphics page) {
    	
         page.setColor (Color.red);
	     page.drawPolyline (car_x, car_y,10);//car
    	 page.drawArc (80+offset,180,20,20,0,180);//rear wheel well
	     page.drawArc (150+offset,180,20,20,0,180);//front wheel well
	     page.drawLine (100+offset,190,150+offset,190);
	     page.setColor (Color.black);
	     page.drawOval (83+offset,183,14,14);//rear wheel
	     page.drawOval (153+offset,183,14,14); //front wheel
	     page.drawLine (81+offset,170,81+offset,150);//antenna
	     
       	//Update the x coordinates.    	
        for (int scan=0; scan < NUM_POINTS; scan++)
        car_x_update[scan] = car_x[scan] + offset;
        
        }//method draw_car
    
} //class Car
	

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: What is wrong with my start method? Posted: Mar 13, 2002 8:34 PM
Reply to this message Reply
Based on my cursory first glance, I see a possible typo in the drive() method:
for (int pause=1; pause < PAUSE; pause++);
   draw_car (offset,page); //erase the car

Either that semicolon at the end of the for statement should be removed, or your indentation should be fixed to show your real intention:
for (int pause=1; pause < PAUSE; pause++)
   ;
draw_car (offset,page); //erase the car

That's just a quick observation, I'll look a little more...

Vikram

Posts: 2
Nickname: vikram
Registered: Mar, 2002

Re: What is wrong with my start method? Posted: Mar 13, 2002 11:42 PM
Reply to this message Reply
> page.drawPolyline (car_x, car_y,10);//car

your car_x and car_y need to have offset
added to them also like u do for the wheels


also maybe u should be doing the animation in a separate thread instead of calling it from start()

hth

Meghana

Posts: 1
Nickname: meg
Registered: Mar, 2002

Re: What is wrong with my start method? Posted: Mar 14, 2002 12:44 AM
Reply to this message Reply
won't you need to call the constructor for the object car ? something like,

car=new Car();

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re:The Meaning of Static Posted: Mar 14, 2002 11:12 AM
Reply to this message Reply
Here is a little demo to illustrate the difference between static class variables and normal non-static instance variables. You can compile and run it, then experiment with it. Essentially it boils down to the fact that a static variable is global for all instances of a class whereas instance variables are unique for each instance of the class created with new. Let me know if this is helpful to you in better understanding static variables.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
 
/**
 * This class will be instantiated as many times as you
 * want, each time, setting the two values, classVar and
 * instanceVar; of course, classVar will be changed for
 * all instances, whereas instanceVar will only be set
 * for the current instance being instantiated.
 */
class DemoObject
{
    static String classVar = "I'm shared by all DemoObject objects.";
    String instanceVar = "I'm use by only one DemoObject object.";
 
    public DemoObject( String sharedString, String myString )
    {
        classVar = sharedString;
        instanceVar = myString;
    }
    public String toString()
    {
        return "classVar is \"" + classVar + "\", objectVar is \"" +
                instanceVar + "\"";
    }
}
 
/**
 * This class just provides all the UI and management
 * of a collection of DemoObject instances.
 */
public class StaticDemo extends JFrame
{
    JTextField editSharedString = new JTextField( 10 );
    JTextField editObjectString = new JTextField( 10 );
    JTextArea  objectDisplay    = new JTextArea( 5, 30 );
    JButton addNewObjectButton  = new JButton( "Create a new DemoObject!" );
 
    // Maybe use a JList instead, since it can store the objects...
    List demoObjectList = new ArrayList();
 
    public static void main(String[] args)
    {
        new StaticDemo("Static Demo").displayUI();
    }
 
    public StaticDemo( String title )
    {
        super(title);
    }
 
    private void initControls()
    {
        objectDisplay.setEditable(false);
        addNewObjectButton.addActionListener( new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    // Add the new object to the list:
                    demoObjectList.add( new DemoObject( editSharedString.getText(),
                                                        editObjectString.getText() ) );
                    editSharedString.setText("");
                    editObjectString.setText("");
                    objectDisplay.setText("");
 
                    // Now refill the display list with the current
                    // list of objects and thier values:
                    Iterator it = demoObjectList.iterator();
                    while( it.hasNext() )
                    {
                        DemoObject o = (DemoObject)it.next();
                        objectDisplay.append( o.toString() );
                        if( it.hasNext() )
                            objectDisplay.append("\n");
                    }
                }
            });
    }
 
    public void displayUI()
    {
        setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE );
 
        Container whatAPane = getContentPane();
 
        initControls();
 
        JPanel entryPanel = new JPanel();
 
        entryPanel.setLayout( new GridLayout( 1, 3 ) );
        entryPanel.add( editSharedString );
        entryPanel.add( editObjectString );
        entryPanel.add( addNewObjectButton );
 
        whatAPane.add( entryPanel, BorderLayout.NORTH );
        whatAPane.add( new JScrollPane(objectDisplay), BorderLayout.SOUTH );
 
        pack();
        setVisible(true);
    }
        
}

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: Re:The Meaning of Static Posted: Mar 14, 2002 1:09 PM
Reply to this message Reply
Yes !! That was extremely helpful. I see it now. Thanks for going to the trouble to send it to me.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: What is wrong with my start method? Posted: Mar 15, 2002 1:13 PM
Reply to this message Reply
I don't know what happened to my previous post, but there was a problem with the array size for car_x, not matching car_y
I wanted to suggest changing this line:
from
private int[] car_x = {80,70,70,95,140,150,175,180,170};

to

private int[] car_x = {80,70,70,95,140,150,175,180,170,170};

and adding the following to the beginning of the paint method:

public void draw_car (int offset, Graphics page) {

page.setColor (Color.white);
page.fillRect(1,1,449,249);

Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Re: What is wrong with my start method? Posted: Mar 15, 2002 1:28 PM
Reply to this message Reply
Yeah, you're right. I've got it working now. Thanks for the help.

Flat View: This topic has 13 replies on 1 page
Topic: Help to design... Previous Topic   Next Topic Topic: Java Programming

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use