绘制图像 2D 图形

我正在用java做一个游戏,但我没有放图像,我已经尝试通过“drawimage”放它,但它不能正常工作=(我不知道哪里是保存图像的最佳位置以及如何保存成为调用图像的最佳方法。


    public void paint(Graphics2D g2){

    g2.setColor(getRandomColor());

    g2.fillOval((int)x, (int)y, (int)diametro, (int)diametro);

}



`

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


慕婉清6462132
浏览 157回答 2
2回答

慕虎7371278

我很确定您的意思是要在面板上绘制图像。首先,我建议您在项目 src 文件夹中创建一个资源文件夹,并在那里添加所有图像。完成后,您必须使用 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);    }}

慕雪6442864

为了在 Java 中创建图像,您首先需要创建一个 JPanel(本质上是屏幕上的一个窗口)。它看起来像这样(取自 javacodex.com):import java.awt.*;import java.awt.image.*;import java.io.*;import javax.swing.*;import javax.imageio.ImageIO;public class JPanelExample {&nbsp; public static void main(String[] arguments) throws IOException {&nbsp; &nbsp; JPanel panel = new JPanel();&nbsp; &nbsp; BufferedImage image = ImageIO.read(new File("./java.jpg"));&nbsp; &nbsp; JLabel label = new JLabel(new ImageIcon(image));&nbsp; &nbsp; panel.add(label);&nbsp; &nbsp; // main window&nbsp; &nbsp; JFrame.setDefaultLookAndFeelDecorated(true);&nbsp; &nbsp; JFrame frame = new JFrame("JPanel Example");&nbsp; &nbsp; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; // add the Jpanel to the main window&nbsp; &nbsp; frame.add(panel);&nbsp;&nbsp; &nbsp; frame.pack();&nbsp; &nbsp; frame.setVisible(true);&nbsp; }}但是,我假设“图像”是指在屏幕上绘制的形状(因为您在示例中使用了绘制圆的绘制方法)。然后,您的代码采用正确的方法,但需要实现 JPanel(因为没有 JPanel 或 JFrame,您实际上无法在屏幕上以图形方式显示任何内容)。下面是取自我的 Atari Breakout 迷你游戏的 Board.java 的示例:public class Board extends JPanel implements KeyListener {&nbsp; &nbsp; // Other Game Code&nbsp;public void paint(Graphics g) {&nbsp; &nbsp; &nbsp; &nbsp; super.paint(g);&nbsp; &nbsp; &nbsp; &nbsp; Graphics2D g2 = (Graphics2D) g;&nbsp; &nbsp; &nbsp; &nbsp; g2.setColor(Color.BLACK);&nbsp; &nbsp; &nbsp; &nbsp; // Shows lives left and score on screen&nbsp; &nbsp; &nbsp; &nbsp; g2.drawString("Lives left: " + lives, 400, 600);&nbsp; &nbsp; &nbsp; &nbsp; g2.drawString("Score: " + score, 400, 500);&nbsp; &nbsp; &nbsp; &nbsp; // Paints Walls (separate paint method created in wall class)&nbsp; &nbsp; &nbsp; &nbsp; topWall.paint(g2);&nbsp; &nbsp; &nbsp; &nbsp; bottomWall.paint(g2);&nbsp; &nbsp; &nbsp; &nbsp; leftWall.paint(g2);&nbsp; &nbsp; &nbsp; &nbsp; rightWall.paint(g2);&nbsp; &nbsp; &nbsp; &nbsp; // Paints 2 Balls (separate paint method created in ball class)&nbsp; &nbsp; &nbsp; &nbsp; b.paint(g2);&nbsp; &nbsp; &nbsp; &nbsp; b2.paint(g2);&nbsp; &nbsp; &nbsp; &nbsp; // Paints Paddle (separate paint method created in paddle class)&nbsp; &nbsp; &nbsp; &nbsp; paddle.paint(g2);&nbsp; &nbsp; &nbsp; &nbsp; // Paints Bricks based on current level (separate paint method created in brick class)&nbsp; &nbsp; &nbsp; &nbsp; // Bricks were created/stored in 2D Array, so drawn on screen through 2D Array as well.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (level == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int x = 0; x < bricks.length; x++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < bricks[0].length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bricks[x][i].paint(g2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (level == 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int x = 0; x < bricks.length; x++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < bricks[0].length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bricks[x][i].paint(g2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }这是 Paddle 类中的 Paint 方法的示例:// Constructor&nbsp;public Paddle(int x, int y, int w, int h){&nbsp; &nbsp; &nbsp; &nbsp; xpos = x;&nbsp; &nbsp; &nbsp; &nbsp; ypos = y;&nbsp; &nbsp; &nbsp; &nbsp; width = w;&nbsp; &nbsp; &nbsp; &nbsp; height = h;&nbsp; &nbsp; &nbsp; &nbsp; r = new Rectangle(xpos, ypos, width, height);&nbsp; &nbsp; }public void paint(Graphics2D g2){&nbsp; &nbsp; &nbsp; &nbsp; g2.fill(r);&nbsp; &nbsp; }输出(屏幕上显示的内容):
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java