浏览 3379 次
锁定老帖子 主题:java 游戏编程 (一)
精华帖 (0) :: 良好帖 (2) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-06-16
首先说说java游戏编程的基础。 这次总结是基于awt swing java2D这些知识。废话不多说,先来第一部分。 1.全屏图形。 显示的硬件分 显卡和显示器。显卡的内存将存储屏幕上的内容,并且将内存中的内容在显示器上显示。显示器的屏幕用像素表示。一般是800*600/1280*1024之类。 另外还有位深度的概念。比如8,16,24,32位。 8是2的八次方 =216中颜色,以此类推。越小越快。 将显示转换为全屏 Jframe,模拟屏幕或说画布等 DisplayMode,指定显示切换到什么样的分辨率,位深度刷新率。 GraphicsDevice,改变现实方式和检查显示属性靠它。 GraphicsEnvironment,获得GraphicsDevice。 首先来一个屏幕管理类 package com.jsheng.game.util; import java.awt.DisplayMode; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Window; import javax.swing.JFrame; /** function: * company: jsheng * @author wanghn wanghaining9999@sina.com * describe: */ public class MyScreenMgr { private GraphicsDevice device; public MyScreenMgr() { GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment(); device = environment.getDefaultScreenDevice(); } public void setFullScreen(DisplayMode displayMode,JFrame window){ window.setUndecorated(true); window.setResizable(false); device.setFullScreenWindow(window); if (displayMode != null && device.isDisplayChangeSupported()) { try { device.setDisplayMode(displayMode); } catch (IllegalArgumentException ex) { //处理 } } } public Window getFullScreenWindow() { return device.getFullScreenWindow(); } public void reScreen() { Window window = device.getFullScreenWindow(); if (window != null) { window.dispose(); } device.setFullScreenWindow(null); } } 接下来是测试 package com.jsheng.game.test1; import java.awt.*; import javax.swing.JFrame; import com.jsheng.game.util.MyScreenMgr; /** function: * company: jsheng * @author wanghn wanghaining9999@sina.com */ public class FullScreenTest extends JFrame { private static final long serialVersionUID = 1L; public static void main(String[] args) { DisplayMode displayMode = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN); FullScreenTest test = new FullScreenTest(); test.run(displayMode); } public void run(DisplayMode displayMode) { setBackground(Color.black); setForeground(Color.white); setFont(new Font("Dialog", 0, 24)); MyScreenMgr screen = new MyScreenMgr(); try { screen.setFullScreen(displayMode, this); Thread.sleep(4000); }catch (InterruptedException ex) { } finally { screen.reScreen(); } } public void paint(Graphics g) { if(g instanceof Graphics2D){ Graphics2D g1 = (Graphics2D) g; g1.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); } g.drawString("欢迎访问J盛科技!我的qq6668220", 20, 50); } } window.setUndecorated(true); 删除边框和标题栏。 g1.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 这两句抗齿边文本,是文本的边缘模糊化,没有齿边。 这样一个全屏幕的demo就出来了。 2.添加图像。 package com.jsheng.game.test1; import java.awt.*; import javax.swing.ImageIcon; import javax.swing.JFrame; import com.jsheng.game.util.MyScreenMgr; /** function: * company: jsheng * @author wanghn wanghaining9999@sina.com */ public class FullScreenTest2 extends JFrame { private static final long serialVersionUID = 1L; private Image bgImage; private Image myImage; private Image myImage2; private boolean flag; public static void main(String[] args) { DisplayMode displayMode = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN); FullScreenTest2 test = new FullScreenTest2(); test.run(displayMode); } public void loadImages() { bgImage = loadImage("images/Blue hills.jpg"); myImage = loadImage("images/a1.jpg"); myImage2 = loadImage("images/a2.jpg"); flag = true; repaint(); } private Image loadImage(String fileName) { return new ImageIcon(fileName).getImage(); } public void run(DisplayMode displayMode) { setBackground(Color.black); setForeground(Color.white); setFont(new Font("Dialog", 0, 24)); MyScreenMgr screen = new MyScreenMgr(); try { screen.setFullScreen(displayMode, this); loadImages(); Thread.sleep(4000); }catch (InterruptedException ex) { } finally { screen.reScreen(); } } public void paint(Graphics g) { if(g instanceof Graphics2D){ Graphics2D g1 = (Graphics2D) g; g1.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); if (flag) { g1.drawImage(bgImage, 0, 0, null); g1.drawImage(myImage, 100, 0, null); g1.drawImage(myImage2, 305, 0, null); g1.drawString("欢迎访问J盛科技!我的qq6668220", 200, 500); } else { g.drawString("正在读取图片", 5, 24); } } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |