首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >创建包含从文件中导入的图像的方法

创建包含从文件中导入的图像的方法
EN

Stack Overflow用户
提问于 2014-04-04 22:33:02
回答 1查看 52关注 0票数 0

我想从//create textures部分创建方法来缩短run(),但是如果我这样做并将来自//import部分的图像作为参数传递,那么就调用它如下:

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

drawGraphics看不到它们的方法。

drawGraphics()只是重复add(everyParameter);

下面是run()内部代码的一部分

代码语言:javascript
运行
复制
    // 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中前面创建的,看起来很好(没有用红色下划线)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-05 09:26:51

我找到的解决方案是创建一个GImage类型的方法,而不是void,然后逐个创建纹理。

代码语言:javascript
运行
复制
    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()方法中

代码语言:javascript
运行
复制
// 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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22874109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档