2012-04-05 66 views
-2

调用一个方法//这里是我的代码:我需要从另一个类

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(){} 
} 

/*我不知道如何做到这一点,我一直在尝试各种各样的事情。我基本上需要*来确保fps在显示运行时正在打印到控制台中。我似乎无法如此* /这。我尝试了run()方法,但它不会调用它。

+0

http://docs.oracle.com/javase/tutorial/java/index.html – mre 2012-04-05 16:35:51

+0

什么你有没有看到过伊万? – Gray 2012-04-05 17:51:45

+0

没有错误报告,只是第二个代码无法正常工作。没有任何东西正在打印到控制台上。 – 2012-04-05 19:07:27

回答

0

我相信你需要在你的主类中创建一个fps类的实例,然后通过它调用该方法。

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

至少这是怎么在C#中,我知道是非常类似于java。

如果这没有帮助,那么我相信它是一个线程问题,并且您需要确保您正确地处理线程。 (我无法帮助解决这个问题,我对Java线程仍然很陌生。)

-1

那么,你可以做两件事之一。您可以声明FPS对象:

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

或者你可以把它所有的静态:

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

//elsewhere 
fps.fps() 
+0

你是第一种方法是正确的。你是第二代码是绝对错误的。您不能在**同时**声明一个名称与具有'static'和'void'关键字的类同名的方法**。你的代码不会编译。 – mtk 2012-04-05 17:22:34

+1

@mtk - 是的...你其实并不正确。除非你误解了我,并且认为我在某种程度上试图声明两个同名的方法(这会导致编译错误)--Java并没有对命名空间交互进行任何限制。至少Java 6在Debian Linux平台上为i386编译时没有。认真。尝试一下。 – FrankieTheKneeMan 2012-04-06 06:50:12

+0

我的意思是说,不能有任何名称与类名相同的方法。只有构造函数的名称与该类的名称相同。你不能为构造函数声明static和void。我试过并得到'class fps {public static void fps(){ } }'的编译错误。这是我从第二种方法解释的,即'fps.fps()'。 我在评论你的第二个代码中的错误,而不是你的方法。 :) – mtk 2012-04-07 22:04:29

0

这是一个有点陌生,具有相同的名称为您的类中的方法。它是否意味着像这样构造?

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(){} 
} 

我也建议所有的代码不适合构造函数,应该移到它自己的方法。然后,如其他人所述,您可以在第一个之后创建一个fps对象,然后调用您的方法。

+0

这是我运行固定版本时得到的结果。 C:\ Users \ Ivan> java Display 测试1成功。 测试2成功。 线程“main”中的异常java.lang.IllegalStateException:组件必须有 有效的对等体我将包含更多详细信息,但我在这里耗尽了字符。 – 2012-04-05 18:42:10

+0

在我看来,这是一个完全独立的问题,你最好用新的问题解决它。毕竟,这个问题的标题,“我需要调用另一个类的方法”以及这个问题的标签并不适用于新问题。我不会立即知道那个异常是什么意思,但我会想象它与你的c.render()调用有关。 – user506069 2012-04-05 18:54:03

0

有在你的代码中的错误:

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

线程是通过调用start方法开始。你所做的是改变start()方法。您应该使用run方法而不是start方法,因为Thread.start()用于启动线程,而run()则是您应该放置执行代码的位置。

由于从Java API的JavaDoc中,http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html#start%28%29

公共无效的start()

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. 
相关问题