【问题标题】:Creating a method containing image imported from files创建包含从文件导入的图像的方法
【发布时间】:2014-04-04 22:33:02
【问题描述】:

我想从 //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 中创建的,看起来很好(没有红色下划线)。

【问题讨论】:

    标签: java image import


    【解决方案1】:

    我找到的解决方案是创建一个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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-08
      • 2021-02-26
      • 2010-11-02
      • 2014-03-13
      相关资源
      最近更新 更多