Java实现图像全屏显示(一)

2014-11-24 10:53:29 ? 作者: ? 浏览: 0

摘要:


本文包括一个简单的Java程序的源代码,该程序在全屏窗口的中心显示图像(本地文件或者从http://URL下载),其中使用了AWT Toolkit装载图像,Swing JFrame显示图像。


兼容性:


Sun Java 1.4或者更高版本


本程序由一个从标准Swing JFrame类扩展而来的Test1组成。代码的大部分在类的构造方法中:两个简单的事件listener(鼠标listener用于退出程序,窗口listener用于窗口在关闭时终止程序运行),全屏模式切换代码以及图像数据装载。


全屏模式切换代码很简单:


this.setUndecorated(true);


this.setVisible(true);


GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);


我们移除窗口的边框和菜单(setUndecorated method),确信窗口可见(setVisible),然后调用setFullScreenWindow。你可以在sun.com了解更多高级的全屏API


程序装载和显示由命令行参数指定的图像。如果参数由http://开头,就创建一个URL对象,否则参数就作为文件名进行处理,并直接传递给AWT ToolkitgetImage方法。


Toolkit.getDefaultToolkit().getImage()方法可以装载GIFJPEGPNG文件。它接收字符串(文件名)或者URL对象作为其参数。


Java源代码:


import java.awt.Graphics;


import java.awt.GraphicsEnvironment;


import java.awt.Image;


import java.awt.Toolkit;


import java.awt.event.MouseEvent;


import java.awt.event.MouseListener;


import java.awt.event.WindowAdapter;


import java.awt.event.WindowEvent;


import java.net.MalformedURLException;


import java.net.URL;



import javax.swing.JFrame;



public class Test1 extends JFrame


{


// this line is needed to avoid serialization warnings


private static final long serialVersionUID = 1L;



Image screenImage; // downloaded image


int w, h; // Display height and width




// Program entry


public static void main(String[] args) throws Exception


{


if (args.length < 1) // by default program will load AnyExample logo


{


new Test1("https://www.cppentry.com/upload_files/article/54/1_0g87c__.gif");


}


else


{


new Test1(args[0]); // or first command-line argument


}


}



// Class constructor


Test1(String source) throws MalformedURLException


{


// Exiting program on window close


addWindowListener(new WindowAdapter()


{


public void windowClosing(WindowEvent e)


{


System.exit(0);


}


});



// Exitig program on mouse click


addMouseListener(new MouseListener()


{


public void mouseClicked(MouseEvent e) { System.exit(0); }


public void mousePressed(MouseEvent e) {}


public void mouseReleased(MouseEvent e) {}


public void mouseEntered(MouseEvent e) {}


public void mouseExited(MouseEvent e) {}


});



// remove window frame


this.setUndecorated(true);



// window should be visible


this.setVisible(true);



// switching to fullscreen mode


GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);



// getting display resolution: width and height


w = this.getWidth();


h = this.getHeight();


System.out.println("Display resolution: " + String.valueOf(w) + "x" + String.valueOf(h));



// loading image


if (source.startsWith("http://")) // http:// URL was specified


{


screenImage = Toolkit.getDefaultToolkit().getImage(new URL(source));


}


else


{


screenImage = Toolkit.getDefaultToolkit().getImage(source); // otherwise - file


}


}



public void paint (Graphics g)


{


if (screenI

-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: