My friend and I are in a Computer Science I class, and we are momentarily working on a project which asks you to import a method from a previously edited source, into another java source file. Here is our code.

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import java.awt.*;

public class PacerTest extends JPanel
{
  private Image leftFoot;
  private Image rightFoot;
  private Image leftshoe;
  private Image rightshoe;

  // Constructor
  public PacerTest()
  {
    leftshoe = (new ImageIcon("leftshoe.gif")).getImage();
    rightshoe = (new ImageIcon("rightshoe.gif")).getImage();
  }

  // Called automatically when the panel needs repainting
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);

    int x = 500;
    int y = 300;
    int stepLength = 100;
    int stepLength2 = 50;

    Foot rightFoot = new Foot(x, y-50, leftshoe);
    Foot leftFoot = new Foot(x, y, rightshoe);
    for (int count = 1; count <= 4; count++)
    {
      leftFoot.draw(g);
      rightFoot.draw(g);
      leftFoot.moveForward(stepLength);
      rightFoot.moveForward(stepLength);
      leftFoot.turn(90);
      rightFoot.turn(90);
      rightFoot.moveSideways(-stepLength2);
      rightFoot.moveForward(stepLength2);
      
    }
  }

  public static void main(String[] args)
  {
    JFrame window = new JFrame("Feet");
    window.setBounds(100, 100, 500, 480);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    PacerTest panel = new PacerTest();
    panel.setBackground(Color.WHITE);
    Container c = window.getContentPane();
    c.add(panel);

    window.setVisible(true);
  }
}

instead of using the move forward, turn(90), and move sideways commands, we should be able to import a method from another class that predefines these commands. Does anyone know how to import a method? I have tried and it just comes up with "illegal start of expression" I would greatly appreciate it if someone could tell us what we are doing wrong, or point us in the right direction. -From our computer to your's- zach&kody

Recommended Answers

All 5 Replies

Actually, I think, that "import a method" is just a fancy way of saying, I want to call a method of a class from somewhere else:

class MyUtils {
  public MyUtils() {

  }

  public static void method_1() {
    // ........
  }

  public void method_2() {
    // ........
  }
}
// SOME WHERE ELSE - ANYWHERE YOU CAN CALL METHODS (even in another class)

// CALL STATIC METHOD:
MyUtils.method_1();

// CALL NON STATIC METHOD:
MyUtils myUt = new MyUtils();
myUt.method_2();
myUt..........

In your case you can have a method that takes as argument a Foot instance and an int, and call the turn method.

commented: Thanks! +0

This is very helpful but in the parts where you have public static void, and public void, what is supposed to be written underneath? I'm sorry, we are very new to this. -from our computer to your's- zach&kody

What is supposed to be "written underneath" is whatever code is necessary to make the method perform as you intend. E.g. If you want the method to return the sum of 2 numbers:

public int add(int num1, int num2)
{
int sum=0;
sum = num1+ num2;
return sum;

}

You can do anything you want to do inside a method.

Also, In case you didn't know:
public int add -
public means the method can be accessed from anywhere in the class.(unless you are trying to access it from a static method)
The int is the return type of the method. Which means, when this method is run, it will return some value, which in this case will be
the result of adding 2 numbers.

The int num1,int num2 inside the brackets are parameters. This means the method can only be called if 2 parameters of type int are passed into it. So, when you call this method, you would call is like so:

add(3,4); // where 3 and 4 are 2 integers. Of course,this call will only work inside the class that the method is declared in. If you wish
//to access this method from outside the class, then use the "methods"(ways) that javaaddict showed you.

What is supposed to be "written underneath" is whatever code is necessary to make the method perform as you intend. E.g. If you want the method to return the sum of 2 numbers:

public int add(int num1, int num2)
{
int sum=0;
sum = num1+ num2;
return sum;

}

You can do anything you want to do inside a method.

Also, In case you didn't know:
public int add -
public means the method can be accessed from anywhere in the class.(unless you are trying to access it from a static method)
The int is the return type of the method. Which means, when this method is run, it will return some value, which in this case will be
the result of adding 2 numbers.

The int num1,int num2 inside the brackets are parameters. This means the method can only be called if 2 parameters of type int are passed into it. So, when you call this method, you would call is like so:

add(3,4); // where 3 and 4 are 2 integers. Of course,this call will only work inside the class that the method is declared in. If you wish
//to access this method from outside the class, then use the "methods"(ways) that javaaddict showed you.

And if I may add, I already mentioned what needs to go underneath:

In your case you can have a method that takes as argument a Foot instance and an int, and call the turn method.

And I find hard to believe that you say you are new to this when you wrote all that code that you presented.

We did not write all of the source above, we simply edited it, as an assignment in our computer science class. Thanks for the tips though guys. -From our computer to your's- zach&kody

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.