大家好, 我最近在弄个游戏。。。。(好吧我知道java不适合做游戏)
本来是用JFrame来作为窗口的,然后加个JComponent来当画布绘画,但是我随着我绘制的图片的增加,速度也变慢了。。。
于是我想获得BufferStrategy来搞,然后自己弄个线程来绘制,但是却莫名的老出现裂图,我上网搜了下双缓冲,然后试了下也不行 郁闷 希望高手帮忙看看怎么回事 下边贴下代码。。。哎 难道真要放弃java。。。。(不好意思,本人目前只会java。。喜欢swing。。喜欢java的语法。。。笑)
- Java code
import java.awt.Color;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.GraphicsDevice;import java.awt.GraphicsEnvironment;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionAdapter;import java.awt.image.BufferStrategy;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.JFrame;public class DoRenderMyself implements Runnable { private Thread loopThread; private GraphicsDevice screen; private JFrame frame; private BufferedImage tile; private BufferedImage select; private int tileW; private int tileH; private Point screenAnchor = new Point(0, 0); private Point plotP = new Point(0, 0); public static void main(String[] args) { new DoRenderMyself(); } public DoRenderMyself() { this.frame = new JFrame("DoRenderMyself"); this.frame.addMouseMotionListener(this.new Mouse()); this.frame.setBounds(150, 100, 900, 600); this.frame.setIgnoreRepaint(true); this.frame.setUndecorated(true); this.frame.setVisible(true); this.loopThread = new Thread(this); screen = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice(); this.loopThread.start(); } public void run() { init(); gameLoop(); } public void init() { try { this.tile = ImageIO.read(new File("src/res/tile0.gif")); this.select = ImageIO.read(new File("src/res/tree0.gif")); } catch (IOException e) { e.getStackTrace(); } this.tileW = tile.getWidth(); this.tileH = tile.getHeight(); this.frame.createBufferStrategy(3); } public void gameLoop() { BufferStrategy buff = this.frame.getBufferStrategy(); Graphics g = buff.getDrawGraphics(); BufferedImage iBuffer = new BufferedImage(this.frame.getWidth(), this.frame.getHeight(), BufferedImage.TYPE_INT_ARGB); while (true) { try { Thread.sleep(100); } catch (Exception e) { } Graphics2D g2 = (Graphics2D) iBuffer.getGraphics(); paint(g2); g.drawImage(iBuffer, 0, 0, this.frame); System.out.println("f"); buff.show(); } } public void paint(Graphics2D g2) { g2.clearRect(0, 0, this.frame.getWidth(), this.frame.getHeight()); g2.scale(2, 2); for (int r = 0; r < 20; r++) { for (int c = 0; c < 20; c++) { map_To_Screen(c, r, plotP); g2.drawImage(this.tile, plotP.x, plotP.y, null); g2.setColor(Color.WHITE); // g2.drawString(r + ", " + c, this.plotP.x + 10, // this.plotP.y + 10); if (c % 2 == 0) { g2.drawImage(this.select, plotP.x, plotP.y - 40, null); } } } g2.dispose(); } public void map_To_Screen(int mapCol, int mapRow, Point p) { int x = mapCol * (tileW >> 1) - mapRow * (tileW >> 1) - this.screenAnchor.x; int y = mapCol * (tileH >> 1) + mapRow * (tileH >> 1) - this.screenAnchor.y; p.x = x; p.y = y; } class Mouse extends MouseMotionAdapter { public void mouseMoved(MouseEvent e) { int mX = e.getX(); int mY = e.getY(); DoRenderMyself.this.frame.setTitle(mX + ", " + mY); if (mX < 18) { DoRenderMyself.this.screenAnchor.x -= 8; } if (mY < 18) { DoRenderMyself.this.screenAnchor.y -= 8; System.out.println("up"); } if (mX > DoRenderMyself.this.frame.getWidth() - 8) { DoRenderMyself.this.screenAnchor.x += 18; } if (mY > DoRenderMyself.this.frame.getHeight() - 8) { DoRenderMyself.this.screenAnchor.y += 18; } } }}