0

我制作了一个简单的塔防游戏,但由于某种原因,我在“y”上出现错误,说“componet.y 不可见”。如果您需要我,我可以包括任何其他课程然后问:)。这是代码:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Screen extends JPanel implements Runnable{

Thread thread = new Thread(this);

Frame frame;

/**Account*/
User user;

private int fps = 0;

public int scene = 0;

public boolean running = false;

public double towerWidth = 1;
public double towerHeight = 1;


public Screen(Frame frame){
    this.frame = frame;

    this.frame.addKeyListener(new KeyHandler(this));

        double width = this.frame.getWidth() / 1.309090909090909;
            towerWidth = width / 22;

        double height = this.frame.getHeight() / 1.285714285714286;
            towerHeight = height / 14;

    thread.start();
}

public void paintComponent(Graphics g){
    g.clearRect(0, 0, this.frame.getWidth(), this.frame.getHeight());



    if(scene == 0){
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, this.frame.getWidth(), this.frame.getHeight());
    }else if(scene == 1){
        //Background
        g.setColor(Color.GREEN);
        g.fillRect(0, 0, this.frame.getWidth(), this.frame.getHeight());
        //Grid
        g.setColor(Color.GRAY);
        for(int x = 0; x < 22; x++){
            for (int y = 0; y < 14; y++){
                g.drawRect(50 + (x * (int)towerWidth), 50 + (y * (int)towerHeight), (int) towerWidth, (int) towerHeight);
            }
        }
    //Health + Money things
        g.drawRect(12, (15*50) + 12, 125, (900 - (15*50) - 12 - 12) / 3);
        g.drawString("Health: " + user.player.health,12 + 25, (15*50) + 12 + 25);

        g.drawRect(12, (15*50) + 12 +  ((900 - (15*50) - 12 - 12) / 3), 125, (900 - (15*50) - 12 - 12) / 3);
        g.drawString("Money: " + user.player.money,12 + 25, (15*50) + 12 + 25 + 50);

        g.drawRect(12, (15*50) + 12 +  (((900 - (15*50) - 12 - 12) / 3)) * 2, 125, (900 - (15*50) - 12 - 12) / 3);

        //Tower Scroll list buttons
                g.drawRect(12 + 12 + 125, (15*50) + 12 , 40, 900 - (15*50) - 12 - 12);
                //Other Button on other side

        //Tower List
        for(int x = 0; x < 20; x++){
            for(int y = 0; y < 2; y++){
                if(Tower.towerList[x * 2 + y] != null){
                    g.drawImage(Tower.towerList[x * 2 + y].texture, (int) (12 + 12 + 125 + 40 + 12 + (x * towerWidth)), (15*50) + 12 + (y * ((int)towerHeight)), (int) towerWidth, (int) towerHeight,null);
                }

                    if(Tower.towerList[x * 2 + y].cost > this.user.player.money){
                        g.setColor(new Color(255, 0 , 0, 100));
                        g.fillRect((int) (12 + 12 + 125 + 40 + 12 + (x * towerWidth)), (15*50) + 12 + (y * (int) towerHeight), (int) towerWidth, (int) towerHeight);
                    }
                }   

                g.setColor(Color.GRAY);
                g.drawRect((int) (12 + 12 + 125 + 40 + 12 + (x * towerWidth)), (15*50) + 12 + (y * (int) towerHeight), (int) towerWidth, (int) towerHeight);
            } 

    }else{
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, this.frame.getWidth(), this.frame.getHeight());
    }


    //FPS AT THE BOTTOM!
    g.drawString(fps + "", 10, 10);
}


//Only first time
public void loadGame(){
    user = new User(this);

    running = true;
    }

public void startGame(User user){
    user.createPlayer();

    this.scene = 1; //Game level1
}

public void run() {
    System.out.println("[Success] Frame Created");
    System.out.println(frame.getWidth());
    System.out.println(frame.getHeight());

    long LastFrame = System.currentTimeMillis();
    int frames = 0;

    loadGame();

    while(running){
        repaint();

        frames++;

        if(System.currentTimeMillis() - 1000 >= LastFrame){
            fps = frames;
            frames = 0;
            LastFrame = System.currentTimeMillis();
        }

        try {
            Thread.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    System.exit(0);
}

public class KeyTyped{
    public void keyESC(){
        running = false;
    }

    public void keySPACE() {
        startGame(user);
    }
}
}

任何人都可以帮忙吗?

这是错误

at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 
The field Component.y is not visible

at net.marksgame.towerdefense.Screen.paintComponent(Screen.java:86)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
4

1 回答 1

0

在下面的行中y,您所指的不是来自您的班级,而是来自Component班级。

g.drawRect((int) (12 + 12 + 125 + 40 + 12 + (x * towerWidth)), (15*50) + 12 + (y * (int) towerHeight), (int) towerWidth, (int) towerHeight);

当您到达那条线时,y您内部for循环中的定义超出了范围。

于 2013-06-16T11:26:01.493 回答