2013-12-07 38 views
0

我已經制作了一個包含多個類的程序,但其中有2個似乎有些奇怪。我無法從一個變到另一個。但是我無法獲得變量Public static int Screen.myWidth並在KeyHandler()上使用它。我只是得到錯誤,當我嘗試..無法從其他類中獲取變量

import java.awt.event.*; 
import java.awt.*; 

public class KeyHandler implements MouseMotionListener, MouseListener, KeyListener{ 
    public static boolean backpack = false; 
    public static boolean movebackpack = false; 
    public static int spawntox; 
    public static int spawntoy; 
    public static int SpotWidth = (Screen.myWidth)/World.ScreenWidth; 
    public static int SpotHeight = (Screen.myHeight)/World.ScreenHeight; 

public void mouseClicked(MouseEvent e) { 
    if (!backpack){ 
     Player.targetx = (int) (e.getX()-Frame.x)/SpotWidth; 
     Player.targety = (int) (e.getY()-Frame.y)/SpotHeight; 
     Player.target = false; 
     Player.MovePlayer(); 
    } 
} 

然後Screen.java

import java.awt.*; 

import javax.swing.*; 

import java.awt.image.*; 
import java.io.*; 

public class Screen extends JPanel implements Runnable{ 
public Thread thread = new Thread(this); 

public static int myWidth, myHeight; 

public static boolean isFirst = true; 

public static Point mc = new Point (0, 0); 

public static World world; 
public static Ground ground; 
public static Backpack backpack; 

public Screen(Frame frame) { 
    frame.addMouseListener(new KeyHandler()); 
    frame.addMouseMotionListener(new KeyHandler()); 
    frame.addKeyListener(new KeyHandler()); 

    thread.start(); 
} 

public void define() { 
    world = new World(); 
    ground = new Ground(); 
    backpack = new Backpack(); 
    } 

public void paintComponent(Graphics g) { 
    myWidth = getWidth(); 
    myHeight = getHeight(); 
    define(); 
    g.setColor(new Color(0, 0, 0)); 
    g.fillRect(0, 0, getWidth(), getHeight()); 
    g.setColor(new Color(255, 25, 100)); 
    g.drawString(String.valueOf(Player.x), 600, 500); 

    ground.draw(g); 
    world.draw(g); 
    if(KeyHandler.backpack == true){ 
    backpack.draw(g); 
    } 
} 

public void run() { 
    while(true) { 
     repaint(); 
     try{ 
     Thread.sleep(1); 

     } catch(Exception e) { } 
    } 
} 

}

編輯1忘了告訴我讓所有的錯誤,當我點擊某處,所以我可以運行它和一切,但它不能計算的東西,因爲它不能得到Screen.myWidth

編輯2錯誤:

Exception in thread "AWT-EventQueue-0" java.lang.ArithmeticException:/by zero 
    at KeyHandler.mouseClicked(KeyHandler.java:14) 
    at java.awt.Component.processMouseEvent(Component.java:6508) 
    at java.awt.Component.processEvent(Component.java:6270) 
    at java.awt.Container.processEvent(Container.java:2229) 
    at java.awt.Window.processEvent(Window.java:2022) 
    at java.awt.Component.dispatchEventImpl(Component.java:4861) 
    at java.awt.Container.dispatchEventImpl(Container.java:2287) 
    at java.awt.Window.dispatchEventImpl(Window.java:2719) 
    at java.awt.Component.dispatchEvent(Component.java:4687) 
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729) 
    at java.awt.EventQueue.access$200(EventQueue.java:103) 
    at java.awt.EventQueue$3.run(EventQueue.java:688) 
    at java.awt.EventQueue$3.run(EventQueue.java:686) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) 
    at java.awt.EventQueue$4.run(EventQueue.java:702) 
    at java.awt.EventQueue$4.run(EventQueue.java:700) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:699) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) 
+0

請發佈你得到的錯誤。 – iWumbo

+0

我想編譯器會抱怨未初始化的變量。即使它沒有抱怨,你的代碼也不會做你想做的事情:在第一次調用paintComponent之前,你不會初始化myWidth和myHeight。在此之前,KeyHandler中的靜態變量會被計算 - 所以SpotWidth和SpotHeight無法正確。 –

+0

@GuntramBlohm把它移動到define(){或run(){doesen't似乎幫助.. :(但好主意.. – minisurma

回答

1

靜態初始化塊試着改變KeyHandler向相關部分:

public static int SpotWidth() { return Screen.myWidth/World.ScreenWidth; } 
public static int SpotHeight() { return Screen.myHeight/World.ScreenHeight; } 

public void mouseClicked(MouseEvent e) { 
if (!backpack){ 
    Player.targetx = (int) (e.getX()-Frame.x)/SpotWidth(); 
    Player.targety = (int) (e.getY()-Frame.y)/SpotHeight(); 

這樣Spot *變量/(函數)在您點擊鼠標的時刻使用屏幕尺寸,這大概是在第一次調用paintComponent()之後。

+0

謝謝,這似乎工作:)至少目前爲止:D – minisurma

+0

你知道這個錯誤:'線程中的異常'AWT-EventQueue-0「java.lang.ArrayIndexOutOfBoundsException」的意思是?編輯完代碼之後,我得到了這個結果嗎? **編輯**看起來像我只有這個錯誤,當我在觀看類'Player.java'時運行遊戲 – minisurma

0

的問題是你的寬度和高度初始化,你可以嘗試這樣的

public static int myWidth, myHeight; 
static { //init those variables. 
    // Get the screen size 
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    myWidth = screenSize.width; // width 
    myHeight = screenSize.height; //height 
}