【问题标题】:Graphics2D not drawing on CanvasGraphics2D 不在画布上绘图
【发布时间】:2018-12-31 05:47:00
【问题描述】:

我正在制作一个游戏,其中需要在一组程序之后更新屏幕(不是每帧都持续更新),我制作了一个较小的程序来测试 Graphics2D 绘图。在这个程序中,我想做的就是在角落里画一个小矩形。但是,矩形似乎没有绘制。我不确定我是否误解了 BufferStrategy 的使用(即有更好的方法来更新游戏),但我尝试调用 draw 方法两次以确保 graphics2D 对象存在且不为空。

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import javax.swing.*;

public class test extends Canvas
{
    private JFrame testWindow = new JFrame("Test");

    public test()
    {
        createUI();
    }

    public static void main(String[] args)
    {
        new test();
    }

    public void createUI()
    {
        testWindow.setSize(500,500);
        testWindow.setLayout(null);
        testWindow.setLocationRelativeTo(null);
        testWindow.setResizable(false);
        testWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        testWindow.add(this);

        testWindow.setVisible(true);

        draw();
        draw();
    }

    public void draw()
    {
        BufferStrategy bs = this.getBufferStrategy();
        System.out.println(bs); 
        //when the program runs this prints null once and then an identifier
        if(bs == null)
        {
            this.createBufferStrategy(3);
            return;
        }

        Graphics g = bs.getDrawGraphics();
        Graphics2D g2 = (Graphics2D) g;

        g2.setColor(Color.RED);
        g2.fillRect(10,10,100,100);

        g2.dispose();
        bs.show();
    }
}

【问题讨论】:

标签: java swing graphics2d


【解决方案1】:

好东西

大多数代码都非常接近于实现您想要完成的目标。只是组织有点不对劲,你创建的类实例有点乱。

坏人

一般来说,最好避免将 Java Swing 元素 (JFrames) 与 AWT 元素 (Canvas) 混合使用。而且我真的不知道您使用 BufferStrategy 的动机是什么。此外,处理 Java Swing 组件及其对 paintComponent 的奇怪零星调用可能会给游戏开发带来痛苦。

丑陋的

您可能不得不切换到 Swing 并使用线程,因为这将是一个更清洁的解决方案。而且,如果您制作的游戏甚至是图形密集型游戏,您将需要使用 OpenGL 或更好的 OpenGL 包装库,如 LWJGL 或我最喜欢的 LibGDX

但就目前而言,这是一个工作示例,就像您尝试使用 Oracle's Custom Painting Tutorial 制作的示例一样:

import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class Test {
    private static JFrame testWindow;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createUI(); 
            }
        });
    }

    private static void createUI() {
        testWindow = new JFrame("Test");
        testWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        testWindow.add(new MyPanel());
        testWindow.pack();
        testWindow.setVisible(true);

    }
}

class MyPanel extends JPanel {

    public MyPanel() {
        setBorder(BorderFactory.createLineBorder(Color.black));
    }

    public Dimension getPreferredSize() {
        return new Dimension(500,500);
    }

    public void paintComponent(Graphics g) {     
        super.paintComponent(g);       
        Graphics2D g2 = (Graphics2D) g;

        g2.setColor(Color.RED);
        g2.fillRect(10,10,100,100);

        g2.dispose();
    }  
}

如果您有任何问题,请告诉我。

注意:类名应始终大写。

【讨论】:

猜你喜欢
  • 2018-07-23
  • 1970-01-01
  • 2020-10-12
  • 2018-01-14
  • 1970-01-01
  • 2016-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多