【问题标题】:Can anyone help fix my code [closed]任何人都可以帮助修复我的代码[关闭]
【发布时间】:2014-01-25 22:56:46
【问题描述】:

这些行上有错误,bs.showg.dispose 在此标记之后有错误语法错误标识符 expect,g.drawImage 的一部分表示删除标记和 说在这个令牌之后说语法错误 decleratorid 我做错了什么

g.drawImage(img, 0, 0, WIDTH HEIGHT, null);
g.dispose();
bs.show();

这是完整的代码

package com.mime.WorldExplorer;


public static final int WIDTH = 800;
public static final int HEIGHT = 600; 
public static final String TITLE = "World explorer(Name not final) Pre-Alpha 0.0.1";
private static final String createBufferStrategy = null;

private Thread thread;
private BufferedImage img;
private boolean running = false;
private Render render;
private Screen screen;
private int[]pixels;
private int i;
private BufferStrategy bs;


public Display() {
screen = new Screen(HEIGHT, WIDTH);
img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferedInt)img.getRaster().getDataBuffer()).getData;
}


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



    private void stop() {
        if(running)
            return; 
        running = false; 
        try {
        thread.join();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);

        }


    }


    public void run() {
        while(running);
        tick();
        render();

        }

private void render() {
    BufferStrategy bs = this.getBufferStrategy();
    if (bs == null)  {
        createBufferStrategy(3);
        return; 



    }


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();




private void tick() {


    }


public static void main(String[] args) {
    Display game = new Display();
    JFrame frame = new JFrame();
    frame.add(game);
    frame.pack();
    frame.setTitle(TITLE);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(WIDTH, HEIGHT);
    frame.setResizable(true);
    frame.setVisible(true);

    System.out.println("Running....");

    System.out.println("Working!");

    game.start();


}


}

【问题讨论】:

  • 请尝试在您的问题中输入一些正确的英语。我不知道什么是错误消息和什么问题。
  • 第一步是修复缩进。您的代码很难按原样阅读。另外,请发布您遇到的任何编译器错误的全文,并准确指出每个错误所指的行。
  • 不可能是完整的源代码,没有类声明...
  • 错误是它在 g.dispose 和 bs.show 上说需要一个标识符,而在 drawImage 上却说是 Variabledelorater 预期的
  • 再次检查这些代码行是否属于一个方法:数一下你的括号。

标签: java macos


【解决方案1】:

我不知道 OSX 代码 - 但是,看起来你有这个:

Graphics g = bs.getDrawGraphics();
g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
g.dispose();
bs.show();

.. 坐在任何方法之外 - 它似乎没有被任何东西包裹......

我建议你从顶部开始,并确保你有匹配的{} 大括号 - 也就是说,你有一个打开的{,你也有一个闭合的} -当您确定没有结束(或开始)的那个时,您会在代码中找到错误。

我认为

你需要改变这个:

private void render() {
    BufferStrategy bs = this.getBufferStrategy();
    if (bs == null)  {
        createBufferStrategy(3);
        return; 



    }


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();

到这个 - 注意它现在包裹在 { } ok :)

再说一遍 - 我不知道 OSX,虽然下面的语法看起来是正确的

private void render() {
   BufferStrategy bs = this.getBufferStrategy();
   if (bs == null)  {
       createBufferStrategy(3);
       return;    
   }        

    for (int i = 0; i<(WIDTH*HEIGHT); i++){ // changed ; for {
        pixels[i] = screen.pixels[i];    
    }

    Graphics g = bs.getDrawGraphics();
    g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
    g.dispose();
    bs.show();

} // added this here

【讨论】:

  • 是的,你修好了,非常感谢 :)
  • 不客气 :) 有机会请标记为已回答
  • 这听起来可能很愚蠢,但如何?
  • 我的答案左侧有一个空白的“勾号” - 只需单击它,它就会变成绿色:)
  • 看不到?抱歉打扰了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-15
  • 2012-11-30
  • 2014-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多