绘制2D图形图像

时间:2018-11-14 01:11:00

标签: java java-2d drawimage

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

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


`

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

2 个答案:

答案 0 :(得分:1)

我非常确定您的意思是您想在面板上绘制图像。首先,建议您在项目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);
    }
}

答案 1 :(得分:0)

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

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.ImageIO;

public class JPanelExample {

  public static void main(String[] arguments) throws IOException {

    JPanel panel = new JPanel();

    BufferedImage image = ImageIO.read(new File("./java.jpg"));
    JLabel label = new JLabel(new ImageIcon(image));
    panel.add(label);

    // main window
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("JPanel Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // add the Jpanel to the main window
    frame.add(panel); 

    frame.pack();
    frame.setVisible(true);

  }
}

但是,我假设“图像”是指在屏幕上绘制的形状(因为在示例中使用的是绘制圆形的绘画方法)。然后,使用正确的方法编写代码,但是需要实现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);
                }
            }
        }


    }

以下是Paddle类中Paint方法的一个示例:

// 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);
    }

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

enter image description here

希望这可以帮助您绘制圆圈!

相关问题