在Java中进行2D游戏开发时,大多数教程都会创建一个缓冲区来进行渲染.这很有道理.然而,人们似乎倾斜的是将实际图形绘制到缓冲区的方法.
一些教程创建一个缓冲图像,然后创建一个整数数组来表示各个像素颜色.
private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
Graphics g = bs.getDrawGraphics();
g.setColor(new Color(0x556B2F));
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
Run Code Online (Sandbox Code Playgroud)
但是,其他一些教程不会创建缓冲图像,将像素绘制到int数组,而是使用BufferStrategy的Graphics组件将其图像直接绘制到缓冲区.
Graphics g = bs.getDrawGraphics();
g.setColor(new Color(0x556B2F));
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(testImage.image, x*128, y*128, 128, 128, null);
Run Code Online (Sandbox Code Playgroud)
我只是想知道,为什么要创建整个int数组,然后绘制它.这需要在实现矩形,拉伸,透明度等方面做更多的工作.缓冲策略的图形组件已经具有可以轻松调用的方法.使用int数组有一些巨大的性能提升吗?
我已经看了几个小时,我看到的所有网站都只是解释了他们在做什么,而不是为什么他们选择这样做.
我经常看到4096用作整个地方的默认缓冲区大小.是否有任何理由选择4096而不是另一个值?
有谁知道如何添加JTextField到图形名称bufferstrategy.getDrawGraphics?试着把它变成图形,如下所示:
private JTextField Input = new JTextField();
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
final Graphics gCommands = bs.getDrawGraphics();
Graphics gCC = bs.getDrawGraphics();
Input.requestFocus();
Input.paint(gCC);
Input.setBounds(800,250, 350,20);
Input.setBorder(BorderFactory.createLineBorder(Color.BLACK, 0));
Input.setEditable(true);
Input.setBackground(getBackground());
Input.setForeground(getForeground());
Input.addKeyListener(key);
Run Code Online (Sandbox Code Playgroud)
但是,尽管它显示,我无法编辑它.即使是Input.setBounds(800,250, 350,20)没有奏效.上面写的这个方法是在游戏循环中调用的.谁能帮我?
我正在尝试在JFrame上使用Graphics2D绘制图像.
但此代码仅显示空白背景.
怎么做?
Java版本:SE-1.6
IDE:Eclipse
我的代码看起来像这样:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import java.awt.geom.Line2D;
import java.util.TimerTask;
import javax.swing.JFrame;
public class GraphicTest extends JFrame{
public static void main(String[] args) {
GraphicTest gt = new GraphicTest();
gt.start();
}
JFrame frame;
BufferStrategy strategy;
GraphicTest(){
int width = 320;
int height = 240;
this.frame = new JFrame("test");
this.frame.setSize(width, height);
this.frame.setLocationRelativeTo(null);
this.frame.setLocation(576, 336);
this.frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
this.frame.setUndecorated(true);
this.frame.setBackground(new Color(0, 0, 0, 50));
this.frame.setVisible(true);
this.frame.setIgnoreRepaint(true);
this.frame.createBufferStrategy(2);
this.strategy = this.frame.getBufferStrategy();
}
public void onExit(){
System.exit(0);
}
void …Run Code Online (Sandbox Code Playgroud) 我正在玩文件读/写,但很难决定为“读”系统调用设置多大的读缓冲区。
特别是,我正在查看“ http://pubs.opengroup.org/onlinepubs/009695399/functions/read.html ”
除了 SSIZE_MAX 之外,它似乎没有说明我可以一次读取多少字节的任何限制。
更糟糕的是,如果我用 SSIZE_MAX 个字符创建一个数组,程序会产生一个:
sh: ./codec: Bad file number
Run Code Online (Sandbox Code Playgroud)
有没有合理的方法来决定每个读取系统调用读取多少字节?我担心这可能会因系统而异(我不能只进行尽可能多的读取,直到读取无法确定我可以读取的确切字节数,即使我这样做了,也不一定会更快比读取更少的字节)。
我的一个想法是检查我的 CPU 缓存大小并尝试使我的缓冲区不大于该大小,但由于我不知道 CPU 缓存如何工作,我不确定这是否一定正确。
提前致谢。
似乎大多数人建议只使用2或3.这只是因为超过3个占用了太多的处理能力或者其他东西(原谅我,我对此有点新鲜)?在什么样的程序中你会使用3个以上的缓冲区?
2或3对我的程序工作正常,我只是很好奇.
java ×4
graphics ×2
render ×2
buffer ×1
c ×1
c# ×1
filestream ×1
graphics2d ×1
integer ×1
io ×1
jtextfield ×1
posix ×1
streamwriter ×1
swing ×1