如何获得Java输出屏幕? [英] How to get the Java Output screen?

查看:479
本文介绍了如何获得Java输出屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用图形"在屏幕上显示图像,但屏幕无法加载

I am trying to display images on the screen using Graphics but the screen doesn't load

出现输出屏幕,但仅显示黑屏,而不显示图像

The output screen appears but only show The black screen and not the images

代码已正确编译,所以为什么我没有得到输出

The code gets compiled properly so why am i not getting the output

package game;

import java.awt.*;
import javax.swing.JFrame;

public class Screen {
    private GraphicsDevice vc;

    public Screen(){
       GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
       vc=env.getDefaultScreenDevice();
    }

    public void setFullScreen(DisplayMode dm, JFrame window){
        window.setUndecorated(true);
        window.setResizable(false);
        vc.setFullScreenWindow(window);

        if(dm !=null && vc.isDisplayChangeSupported()){
            try{
                vc.setDisplayMode(dm);
            }catch(Exception ex){}
        }
    }

    public Window getFullSCreenWindow(){
        return vc.getFullScreenWindow();
    }

    public void resotreScreen(){
        Window w= vc.getFullScreenWindow();
        if(w!=null){
            w.dispose();
        }
        vc.setFullScreenWindow(null );
    }


}



package game;

import java.awt.*;
import javax.swing.ImageIcon;

import javax.swing.JFrame;

class Images extends JFrame{
    public static void main(String[] args){
       DisplayMode dm = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN);

       Images i = new Images();
       i.run(dm);

    }

    private Screen s;
    private Image bg;
    private Image pic;
    private boolean loaded;
    public void run(DisplayMode dm){
        setBackground(Color.BLUE);
        setForeground(Color.WHITE);
        setFont(new Font("Arial",Font.PLAIN,24));

        loaded =false;

        s = new Screen();

        try{
            s.setFullScreen(dm, this);
            loadpics();
            try{
                Thread.sleep(10000);
            }catch(Exception ex){}
        }finally{
               s.resotreScreen();
        }

    }

    public void loadpics(){
        bg = new ImageIcon("C:\\Users\\Dhruv\\Downloads\\Ronaldo.jpg").getImage();
        pic =new ImageIcon("C:\\Users\\Dhruv\\Downloads\\Messi.jpg").getImage();

        loaded= true;
        repaint();
    }
    public void paint(Graphics g){
        if(g instanceof Graphics2D){
            Graphics2D g2 =(Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        }

        if(loaded){
            g.drawImage(bg,0,0,null);
            g.drawImage(pic,170,180,null);
        }

    }
}

推荐答案

让我们从背景信息开始...

Let's start with background information...

JFrameJRootPane的容器,其中包含contentPaneJMenuBarJGlassPane

A JFrame is a container for a JRootPane, which contains the contentPane, JMenuBar and JGlassPane

覆盖诸如JFrame之类的顶级容器的paint时,您仅绘制了最底部的组件,然后将JRootPane及其内容绘制在顶部,使其毫无意义.

When you override paint of top level container like JFrame, you are only painting the bottom most component, the JRootPane and it's contents are then painted over the top, making it kind of pointless.

有关更多详细信息,请参见如何使用根窗格

See How to Use Root Panes for more details

绘画也是一个复杂的操作,不能调用super.paint不会引起任何问题,除非您真正了解它的工作原理并准备手动进行工作,否则请确保在绘画之前始终调用super paint方法.

Painting is also a complex operation, failing to call super.paint will cause no end of issues, make sure you always call the super paint method before painting unless you really understand how it works and are prepared to do it's job manually.

在Swing中,建议您从基于JComponent的类(首选JPanel)扩展并覆盖其paintComponent方法并在那里执行自定义绘制

In Swing you are instead encouraged to extend from a JComponent based class (JPanel been the preferred) and override its paintComponent method and perform your custom paint there

根据您的需要,可以将该组件添加到窗口中或设置为contentPane或添加到其他容器中.

This component can either be added to the window or set as the contentPane or added to some other container depending on your needs.

请参见 AWT和Swing中的绘画执行自定义绘画以获取更多详细信息.

See Painting in AWT and Swing and Performing Custom Painting for more details.

ImageIcon使用后台线程加载其图像,因此即使返回,该图像也可能无法实现(或完全加载).当您使用g.drawImage(bg,0,0,null);并将null作为ImageObserver传递时,它将阻止容器知道图像何时更改,并允许其自动重新绘制自身.

ImageIcon uses background thread to load it's images, so even though it returns, the image might not be realised (or fully loaded). When you use g.drawImage(bg,0,0,null);, passing null as the ImageObserver, it prevents the container from knowing when the image changes and allowing it to automatically repaint itself.

很棒的是,所有基于Component的类都实现了ImageObserver,因此几乎所有可以绘制的东西都可以充当ImageObserver.

What's cool is, all Component based classes implement ImageObserver, so pretty much anything which can paint can act as an ImageObserver.

公约鼓励使用this作为ImageObserver.

通常,更好的解决方案是使用ImageIO,它在加载图像时直到图像完全实现后才会返回.

Generally, a better solution is to use ImageIO, which when it loads images, won't return until the image is fully realised.

看看读取/加载图像有关更多详细信息.

Have a look at Reading/Loading an Image for more details.

Thread.sleep(10000);是在Swing中使用的危险物品.它有可能阻止UI的更新或响应其他输入和事件,从而使您的程序看起来好像已挂起,因为它已经挂了.但就您而言,这意味着您违反了Swing的单线程规则.

Thread.sleep(10000); is a dangerous thing to use in Swing. It has the potential to stop the UI from been updated or respond to other input and events, making your program appear as if it's hung, because it has. But in your case, it means you're violating the single thread rules of Swing.

Swing是一个单线程环境,永远不要执行任何可能会阻塞事件调度线程的操作,并且永远不要从EDT上下文之外更新UI.

Swing is a single threaded environment, you should never perform any action which might block the Event Dispatching Thread and you should never update the UI from outside the context of the EDT.

有一些解决方案可以帮助您,Swing Timer用于生成在EDT中调度的定期事件,而SwingWorker用于执行长时间运行的操作,这些操作支持更新UI.

There are solutions available to help you, Swing Timer for generating periodical events which are dispatched within the EDT and SwingWorker for performing long running operations which have support for updating the UI.

有关更多详细信息,请参见事件调度线程.

See The Event Dispatch Thread for more details.

我的建议是不要担心全屏支持,集中精力使用普通窗口绘制图像并添加全屏支持.这样一来,您就可以在一组孤立的功能中解决问题,从而更容易解决.

My recommendation is to not worry about the full screen support, focus on getting the images painting using a normal window and the add the full screen support. This allows you to solve problems within an isolated set of functionality, making it much easier to solve.

这篇关于如何获得Java输出屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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