I need to call a method from another class

Go To StackoverFlow.com

-2

// Here's my code:

Main Class:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Display extends Canvas implements Runnable{
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension dim = toolkit.getScreenSize();
    public static int WIDTH;
    public static int HEIGHT;
    public static final String title = "First Person Game";
    public static Thread thread;
    public static Screen screen;
    public static  BufferedImage img;
    public static boolean running = false;
    public static int[] pixels;
    public static Render render;
    public Display(){
        WIDTH = dim.width;
        HEIGHT = dim.height;
        screen = new Screen(WIDTH, HEIGHT);
        img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
    }
    private void start(){
        if(running){
            return;
        }else{
            running = true;
            thread = new Thread(this);
            thread.start();
        }
    }
    private void stop(){
        if(!running){
            return;
        }else{
            running = false;
            try{
                thread.join();
            }catch(Exception x){
                System.exit(0);
            }
        }
    }
    public void run(){
        while(running){
            render();
        }
    }
    public void render(){
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null){
            createBufferStrategy(3);
            return;
        }
        screen.render();
        for(int i = 0; i < WIDTH * HEIGHT; i++){
            pixels[i] = screen.pixels[i];
        }
        Graphics g = bs.getDrawGraphics();
        g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
        g.dispose();
        bs.show();
    }
    public static void main(String args[]){
        JFrame frame = new JFrame();
        BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
        Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");
        frame.getContentPane().setCursor(blankCursor);
        Display game = new Display();
        frame.setUndecorated(true);
        frame.add(game);
        frame.setTitle(title);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH, HEIGHT);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
        fps f = new fps();
        game.start();
    }
}



The class I need to call:

public class fps{
    public void fps(){
        System.out.println("Test 1 successful.");
        int frames = 0;
        double unprocessedSeconds = 0;
        long previousTime = System.nanoTime();
        double secondsPerTick = 1 / 60.0;
        int tickCount = 0;
        boolean ticked = false;
        Display c = new Display();
        System.out.println("Test 2 successful.");
        c.render();
        long currentTime = System.nanoTime();
        long passedTime = currentTime - previousTime;
        previousTime = currentTime;
        unprocessedSeconds += passedTime / 1000000000.0;
        while(unprocessedSeconds > secondsPerTick){
            System.out.println("Test 3 successful.");
            tick();
            unprocessedSeconds -= secondsPerTick;
            ticked = true;
            tickCount++;
            if(tickCount % 60 == 0){
                System.out.println(frames + " fps");
                previousTime += 1000;
                frames = 0;
            }
        }
        if(ticked){
            c.render();
            frames++;
        }
        c.render();
        frames++;
    }
    public void tick(){}
}

/* I don't know how to do this, I've been trying all sorts of things. I basically need *to make sure fps is printing into the console while display is running. I can't seem to so */ this. I tried in method run() but it just wouldn't call it.

2012-04-05 16:16
by Ivan Spajic
http://docs.oracle.com/javase/tutorial/java/index.htm - mre 2012-04-05 16:35
What errors have you seen Ivan - Gray 2012-04-05 17:51
There are no errors reported it's just that the second code is not working. Nothing is being printed out onto the console - Ivan Spajic 2012-04-05 19:07


0

I beleive you need to create an instance of the fps class in your main class, and then call the method through it.

fps nameOfClassInstance = new fps(//don't forget anything you need to send for constructor);
fps.run();

At least this is how it is in C# which I know is very similar to java.

if this doesn't help,then I beleive it is a threading issue, and you need to make sure you are handling the threads properly. ( I can't help with this issue, I am still green to java threading.)

2012-04-05 16:20
by kenetik


0

It is a little strange to have a method with the same name as your class. Is it meant to be a constructor, like this?

public class fps{
    //Note that the void is removed here
    public fps(){
        System.out.println("Test 1 successful.");
        int frames = 0;
        double unprocessedSeconds = 0;
        long previousTime = System.nanoTime();
        double secondsPerTick = 1 / 60.0;
        int tickCount = 0;
        boolean ticked = false;
        Display c = new Display();
        System.out.println("Test 2 successful.");
        c.render();
        long currentTime = System.nanoTime();
        long passedTime = currentTime - previousTime;
        previousTime = currentTime;
        unprocessedSeconds += passedTime / 1000000000.0;
        while(unprocessedSeconds > secondsPerTick){
            System.out.println("Test 3 successful.");
            tick();
            unprocessedSeconds -= secondsPerTick;
            ticked = true;
            tickCount++;
            if(tickCount % 60 == 0){
                System.out.println(frames + " fps");
                previousTime += 1000;
                frames = 0;
            }
        }
        if(ticked){
            c.render();
            frames++;
        }
        c.render();
        frames++;
    }
    public void tick(){}
}

I would also suggest that all that code is not really appropriate for a constructor and should be moved to its own method. Then, as mentioned by others, you can call your method after first creating an fps object.

2012-04-05 16:21
by user506069
This is what I get when I run the fixed version. C:\Users\Ivan>java Display Test 1 successful. Test 2 successful. Exception in thread "main" java.lang.IllegalStateException: Component must have a valid peer I would include more details but I run out of characters here - Ivan Spajic 2012-04-05 18:42
In my opinion that is an entirely separate issue and you would be better served to address it with a new question. After all, the title of this question, "I need to call a method from another class" along with this question's tags do not really apply to the new issue. I don't immediately know what that exception means, but I would imagine it is related to your c.render() call - user506069 2012-04-05 18:54


0

There's an error in your code:

private void start(){
    if(running){
        return;
    }else{
        running = true;
        thread = new Thread(this);
        thread.start();
    }
}

Threads are started by calling the start method. What you've done is to change the start() method. You should use the run method instead of the start method, as Thread.start() is meant for starting a thread while run() is where you are supposed to put the execution code.

As from the JavaDoc of the Java API, http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html#start%28%29

public void start()

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. 
2012-06-29 12:58
by wei2912


-1

Well, you can do one of two things. You can either declare an fps object:

fps myfps = new fps();
myfps.fps();

or you can make it all static:

public static void fps(){
  ...
}
public static void tick(){}

//elsewhere
fps.fps()
2012-04-05 16:21
by FrankieTheKneeMan
You're 1st approach is correct. You're 2nd code is absolutely wrong. You can't declare a method with same name as the class with static and void keywords both at the same time. You're code will not compile - mtk 2012-04-05 17:22
@mtk - yeah... you're not, in fact, correct. Unless you're misunderstanding me, and thinking that I'm somehow trying to declare two methods of the same name (which would cause a compile error) - Java doesn't really put any restrictions on the namespace interaction. At least Java 6 doesn't when compiled for i386 on a Debian Linux platform. Seriously. Try it - FrankieTheKneeMan 2012-04-06 06:50
I meant to say that, there can't be any method with name as that of class name. It is only the constructor that has the name same as that of the class. You can't declare static and void for a constructor. I tried and got a compile error for class fps{ public static void fps(){ } }. This is what i interpreted from your 2nd approach i.e. fps.fps(). I was commenting on the error in your 2nd code and not in your approach. : - mtk 2012-04-07 22:04
@mtk - Odd. I can compile your code just fine, exactly as you printed it here. The documentation at http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html makes no mention of any such restriction. What version of java compiler are you using, and what compile error are you getting - FrankieTheKneeMan 2012-04-13 07:07
I am using eclipse IDE and java 1.6 libraries - mtk 2012-04-13 16:00
Ads