显示器有问题 [英] Having issues with displays

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

问题描述

我有一个日落的自定义图像作为我的背景在Java程序。它是一个JFrame与JPanel和JPanel房子的日落背景。有没有办法,我可以绘制或绘制我的精灵表格的背景上的图像?我将不得不使用基本颜色,我可以在里面的程序,如背景(Color.BLACK);?我想使用自定义图像作为我的背景,并绘制在顶部的背景,如果它是画布或面板,我打算使用。我想让我的游戏运行。

I have a custom image of a sunset as my background in a Java program. It is a JFrame with a JPanel and the JPanel houses the sunset background. Is there no way I can draw or paint the images of my sprite sheet on top of the background? Will I have to use basic colors in which I can pick inside the program such as background(Color.BLACK);? I wanted to use the custom image as my background and draw on top o the background as if it was the Canvas or Panel I am planning to use. I want my game to run on top of that. Adding another JPanel just covers everything.

推荐答案

您可以...



创建一个 JPanel ,它绘制精灵,使其透明并将其添加到背景面板...

You could...

Make a JPanel which paints the sprites, make it transparent and add it to the background panel...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                BackgroundPane backgroundPane = new BackgroundPane();
                backgroundPane.setLayout(new BorderLayout());
                backgroundPane.add(new SpitePane());

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(backgroundPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class BackgroundPane extends JPanel {

        private BufferedImage bgImg;

        public BackgroundPane() {
            try {
                bgImg = ImageIO.read(...);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return bgImg == null ? new Dimension(200, 200) : new Dimension(bgImg.getWidth(), bgImg.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int x = (getWidth() - bgImg.getWidth()) / 2;
            int y = (getHeight() - bgImg.getHeight()) / 2;
            g2d.drawImage(bgImg, x, y, this);
            g2d.dispose();
        }

    }

    public class SpitePane extends JPanel {

        private BufferedImage sprite;

        public SpitePane() {
            try {
                sprite = ImageIO.read(...);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            setOpaque(false);
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int x = (getWidth() - sprite.getWidth()) / 2;
            int y = (getHeight() - sprite.getHeight()) / 2;
            g2d.drawImage(sprite, x, y, this);
            g2d.dispose();
        }

    }
}

问题是,你没有真正实现任何效率,当你更新 SpritePane ,它需要绘制 BackgroundPane

But, the problem is, you're not really achieving any efficiency, when you update the SpritePane, it needs to paint the BackgroundPane (because of the way the painting and transparent containers work)

你的sprite直接在背景同时...

Simply paint your sprites directly over the background at the same time...

这将产生与上面相同的结果...

This will generate the same result as above...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                GamePane backgroundPane = new GamePane();

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(backgroundPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class GamePane extends JPanel {

        private BufferedImage bgImg;
        private BufferedImage sprite;

        public GamePane() {
            try {
                bgImg = ImageIO.read(...);
                sprite = ImageIO.read(...);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return bgImg == null ? new Dimension(200, 200) : new Dimension(bgImg.getWidth(), bgImg.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int x = (getWidth() - bgImg.getWidth()) / 2;
            int y = (getHeight() - bgImg.getHeight()) / 2;
            g2d.drawImage(bgImg, x, y, this);

            x = (getWidth() - sprite.getWidth()) / 2;
            y = (getHeight() - sprite.getHeight()) / 2;
            g2d.drawImage(sprite, x, y, this);
            g2d.dispose();
        }

    }
}

好,所以你继续谈论 Canvas / 面板 JPanel ,您需要知道这些组件彼此之间操作显着不同。

Okay, so you keep talking about Canvas/Panel and JPanel, you need to know that these components operate significantly differently from each other.

AWT组件( Canvas / Panel )称为重量级组件,它们有自己的本地对等体。它们不能是透明的,并且没有z顺序的概念(当与Swing组件混合时,这是一个问题)。

AWT components (Canvas/Panel) are known as heavy weight components, they have their own native peer onto which they paint. They can't be transparent and don't have a concept of z-ordering (which is a problem when mixed with Swing components).

AWT组件允许您控制绘画过程(AKA主动绘画),但是排除了与它们一起使用任何Swing组件的可能性(因为Swing有它自己的绘画过程)。

AWT components do allow you to take control of the painting process (AKA active painting), but that precludes the possibility of ever using ANY Swing components with them (as Swing has it's own painting process)

组件称为轻量组件,它们共享它们所在的父窗口的本地对等体。它们也默认是双缓冲的(这是使用 BufferStrategy Canvas 的原因之一)是透明的。 Swing使用所谓的被动渲染算法,这意味着重绘管理器将决定什么时候绘制什么,您可以请求更新某些内容,但不能保证什么时候可以执行。

Swing components are known as light weight components, they share the native peer of the parent window which they reside. They are also double buffered by default (which is one of the reasons for using a BufferStrategy with a Canvas) and can be transparent. Swing uses what's known as a passive rendering algorithm, meaning that the repaint manager will be making decisions about what and when something will be painted, you can make requests for something to be updated, but you can't guarantee when it might be carried out.

你的问题的答案是,这取决于。你需要决定你必须具备的功能和你愿意做的工作类型(主动绘画可以采取一些工作来设置)和两个系统之间的权衡。

The answer to your question is, it depends. You need to decide which features you must have and the type of work you're willing to do (active painting can take some work to get setup) and the trade offs between the two systems.

您可以查看在AWT和Swing中绘画执行自定义绘图 BufferStrategy BufferStrategy和BufferCapabilities 了解详情

You might one to take a look at Painting in AWT and Swing, Performing Custom Painting and BufferStrategy and BufferStrategy and BufferCapabilities for more details

这篇关于显示器有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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