Java 绘制图像二维图形

Java 绘制图像二维图形,java,java-2d,drawimage,Java,Java 2d,Drawimage,我正在用java做一个游戏,但我没有放图片,我已经试着用“drawimage”放了,但它不正常=( 我不知道哪里是保存图像的最佳位置,以及如何调用图像的最佳方法 public void paint(Graphics2D g2){ g2.setColor(getRandomColor()); g2.fillOval((int)x, (int)y, (int)diametro, (int)diametro); } ` 这段代码来自我的类小行星,它们是一个圆(因为我要测试

我正在用java做一个游戏,但我没有放图片,我已经试着用“drawimage”放了,但它不正常=( 我不知道哪里是保存图像的最佳位置,以及如何调用图像的最佳方法

    public void paint(Graphics2D g2){
    g2.setColor(getRandomColor());
    g2.fillOval((int)x, (int)y, (int)diametro, (int)diametro);
}


`
这段代码来自我的类小行星,它们是一个圆(因为我要测试与圆的碰撞)。
谢谢

为了在Java中创建图像,您首先需要创建一个JPanel(本质上是屏幕上的一个窗口)。它看起来像这样(取自javacodex.com):

然而,我假设“图像”是指绘制到屏幕上的形状(因为在示例中使用的是绘制圆的绘制方法)。然后,您的代码采用正确的方法,但需要实现JPanel(因为没有JPanel或JFrame,您无法在屏幕上以图形方式显示任何内容)

下面是我的Atari Breakout迷你游戏Board.java中的一个示例:

public class Board extends JPanel implements KeyListener {

    // Other Game Code

 public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);

        // Shows lives left and score on screen
        g2.drawString("Lives left: " + lives, 400, 600);
        g2.drawString("Score: " + score, 400, 500);

        // Paints Walls (separate paint method created in wall class)
        topWall.paint(g2);
        bottomWall.paint(g2);
        leftWall.paint(g2);
        rightWall.paint(g2);

        // Paints 2 Balls (separate paint method created in ball class)
        b.paint(g2);
        b2.paint(g2);

        // Paints Paddle (separate paint method created in paddle class)
        paddle.paint(g2);

        // Paints Bricks based on current level (separate paint method created in brick class)
        // Bricks were created/stored in 2D Array, so drawn on screen through 2D Array as well. 
        if (level == 1) {
            for (int x = 0; x < bricks.length; x++) {
                for (int i = 0; i < bricks[0].length; i++) {
                    bricks[x][i].paint(g2);
                }
            }
        }
        if (level == 2) {
            for (int x = 0; x < bricks.length; x++) {
                for (int i = 0; i < bricks[0].length; i++) {
                    bricks[x][i].paint(g2);
                }
            }
        }


    }
输出(屏幕上显示的内容):


希望这有助于您绘制圆圈!

我很确定您的意思是希望在面板上绘制图像。首先,我建议您在projects src文件夹中创建一个resources文件夹,并在其中添加所有图像。完成后,您必须使用imageIO加载图像并使用drawImage绘制图像。下面是一个简短的示例:

package asteroid;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class Nave {

    BufferedImage iconeNave;

    public Nave( ... ) {
        try{
        iconeNave = ImageIO.read(getClass().getResource("/resources/nave.png"));
        }catch(IOException e){e.printStackTrace();}
        catch(Exception e){e.printStackTrace();}
    }

    @Override
    public void paint(Graphics2D g2){
        AffineTransform at = new AffineTransform();
        at.translate((int)x + radius/2.5,(int)y + radius/2.5);
        at.rotate(Math.PI/2 + angle);
        at.translate(-iconeNave.getWidth()/2, -iconeNave.getHeight()/2);
        g2.drawImage(iconeNave, at, null);
    }
}

请讲英语。Bem-vindo ao StackOverflow!Esteéum网站somente em inglês.欧盟建议tentar pt.StackOverflow.com
// Constructor
 public Paddle(int x, int y, int w, int h){

        xpos = x;
        ypos = y;
        width = w;
        height = h;

        r = new Rectangle(xpos, ypos, width, height);
    }

public void paint(Graphics2D g2){

        g2.fill(r);
    }
package asteroid;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class Nave {

    BufferedImage iconeNave;

    public Nave( ... ) {
        try{
        iconeNave = ImageIO.read(getClass().getResource("/resources/nave.png"));
        }catch(IOException e){e.printStackTrace();}
        catch(Exception e){e.printStackTrace();}
    }

    @Override
    public void paint(Graphics2D g2){
        AffineTransform at = new AffineTransform();
        at.translate((int)x + radius/2.5,(int)y + radius/2.5);
        at.rotate(Math.PI/2 + angle);
        at.translate(-iconeNave.getWidth()/2, -iconeNave.getHeight()/2);
        g2.drawImage(iconeNave, at, null);
    }
}