2014-04-04 108 views
0

導入圖像的方法我想創造方法進行//create textures部分的縮短run(),但如果我不和//import部分作爲參數傳遞的圖像,然後調用它像:創建包含從文件

createTextures(texture, texture2,ballTextureImport, greenFlashImport, blueFlashImport);

drawGraphics方法看不到它們。

drawGraphics()就是反覆add(everyParameter);

這裏的部分代碼裏面run()

// import 
    Image texture = getImage(getCodeBase(), "texture.png"); 
    Image texture2 = getImage(getCodeBase(), "texture2.png"); 
    Image ballTextureImport = getImage(getCodeBase(), "ballTexture.png"); 
    Image greenFlashImport = getImage(getCodeBase(), "greenFlash.png"); 
    Image blueFlashImport = getImage(getCodeBase(), "blueFlash.png"); 

    // create textures 
    GImage paddleLeftTexture = new GImage(texture); 
    GImage paddleRightTexture = new GImage(texture2); 
    GImage ballTexture = new GImage(ballTextureImport); 
    GImage greenFlash = new GImage(greenFlashImport, -250, 0); 
    GImage blueFlash = new GImage(blueFlashImport, -250, 0); 
    paddleLeftTexture.setSize(WIDTH + 1, HEIGHT + 1); 
    paddleRightTexture.setSize(WIDTH + 1, HEIGHT + 1); 
    ballTexture.setSize(BALL_SIZE, BALL_SIZE); 
    greenFlash.setSize(100, 300); 
    blueFlash.setSize(100, 300); 

    // make objects 
    GOval ball = makeBall(); 
    GRect paddleLeft = makePaddle(); 
    GRect paddleRight = makePaddle(); 

    drawGraphics(ball, paddleLeftTexture, paddleRightTexture, ballTexture, 
      greenFlash, blueFlash, counter, paddleLeft, paddleRight, 
      aiScore, playerScore); 

drawGraphics()其餘參數在run()之前創建和//make objects,並期待罰款(不帶紅色下劃線)。

回答

0

我找到的解決方案是創建一個GImage類型的方法,而不是void,並逐個創建紋理。

private GImage createTexture(String importedImage, int width, int height) { 
    Image importResult = getImage(getCodeBase(), importedImage); 
    GImage textureResult = new GImage(importResult); 
    textureResult.setSize(width, height); 
    return textureResult; 
} 

,然後在run()方法我有

// make objects 
    GImage paddleLeftTexture = createTexture("texture.png", WIDTH + 1, 
      HEIGHT + 1); 
    GImage paddleRightTexture = createTexture("texture2.png", WIDTH + 1, 
      HEIGHT + 1); 
    GImage ballTexture = createTexture("ballTexture.png", (int) BALL_SIZE, 
      (int) BALL_SIZE); 
    GImage greenFlash = createTexture("greenFlash.png", 100, 300); 
    GImage blueFlash = createTexture("blueFlash.png", 100, 300); 
    GOval ball = makeBall(); 
    GRect paddleLeft = makePaddle(); 
    GRect paddleRight = makePaddle(); 
    greenFlash.setLocation(-200, 0); 
    blueFlash.setLocation(-200, 0); 

    // generate GUI 
    drawGraphics(ball, paddleLeftTexture, paddleRightTexture, ballTexture, 
      greenFlash, blueFlash, counter, paddleLeft, paddleRight, 
      aiScore, playerScore); 
相關問題