在JFrame中绘画Graphics2D [英] Painting Graphics2D in a JFrame

查看:286
本文介绍了在JFrame中绘画Graphics2D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作2D垂直射击游戏,其中除图形外,所有内容均已编码(并且可以正常工作).我以前没有使用过Graphics类,所以这对我来说是全新的.以下是我用来将所有内容绘制到JFrame的代码:

I'm making a 2d vertical shooter game, in which everything is coded (and working) but the graphics. I have not used the Graphics classes before, so this is all new to me. The following is the code I use to paint everything to the JFrame:

public void paintAll()
{
    Graphics h = new Graphics2D();
    for(Bullet j : GameState.getEnBullets()){
        h.drawImage(j.getImage(),j.getX(), j.getY(), null);}
    for(Enemy j : GameState.getEnemies()){
        h.drawImage(j.getImage(),j.getX(), j.getY(), null);}
    for(Bullet j : GameState.getPlayBullets()){
        h.drawImage(j.getImage(),j.getX(), j.getY(), null);}
    this.paint(h);
}

第一行"Graphics h = new Graphics2D();"因为Graphics2d是抽象的,所以会产生错误,但是我不知道从这里到哪里.

The first line "Graphics h = new Graphics2D();" produces an error because Graphics2d is abstract, but I have no idea where to go from here.

我需要代码来获取我拥有的所有图像并将它们绘制到JFrame中的各个点.我提醒您,我以前从未做过此事,所以请告诉我这样做是否错误.

I need the code to take all the images that I have and paint them to the points in the JFrame. I remind you that I have never done this before, so please tell me if this is the wrong way to do this.

推荐答案

与Will关于同一件事的第二个线程(我的直升机视图)有关

in connections with Will's second thread (my helicopter view) about same thing Error with timer and JFrame

然后纠正安德鲁·汤普森(Andrew Thompson)的魔幻世界的直觉

and correct intuition by Andrew Thompson's magics globe then

我添加了(我希望这是正确的,因为我不熟悉paint,paintComponent或paintComponents以及自定义图形)

I added (I hope that's correctly, because I'm not familair with paint, paintComponent or paintComponents together with custom Graphics)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class MinimumSize extends JFrame {

    private static final long serialVersionUID = 1L;

    public MinimumSize() {
        setTitle("Custom Component Test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void display() {
        add(new CustomComponent());
        pack();        
        setMinimumSize(getSize());// enforces the minimum size of both frame and component
        setVisible(true);
    }

    public static void main(String[] args) {
        MinimumSize main = new MinimumSize();
        main.display();
    }
}

class CustomComponent extends JComponent {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(100, 100);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 300);
    }

    @Override
    public void paintComponent(Graphics g) {
        int margin = 10;
        Dimension dim = getSize();
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
    }
}

这篇关于在JFrame中绘画Graphics2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆