我有一个全屏画布,上面画有3个图像.当我调整窗口大小时,这些图像会改变位置; 然而,它似乎非常小问题,在Firefox中更是如此.
我一直在读,双缓冲应该可以解决这个问题,但是我想知道当下一个位置未知时我将如何加倍缓冲.也就是说,我无法确定将来应该缓存什么,那么这怎么可能呢?
这是一个看似可行的来源,但我并不完全理解Fedor试图解释的概念.
到目前为止,我有,
$canvas = $('#myclouds')[0];
$canvas_buffer = $('canvas')[0].insertAfter($canvas).css('visibility', 'hidden');
context = $canvas.getContext('2d');
context_buffer = $canvas_buffer.getContext('2d');
clouds_arr = [$canvas, $canvas_buffer];
$(window).resize(function () {
drawCanvas();
};
function initCanvas() {
// Sources for cloud images
var cloud1 = '/js/application/home/images/cloud1.png',
cloud2 = '/js/application/home/images/cloud2.png',
cloud3 = '/js/application/home/images/cloud3.png';
// add clouds to be drawn
// parameters are as follows:
// image source, x, y, ratio, adjustment)
addCloud(cloud1, null, 125, .03);
addCloud(cloud2, null, 75, .15);
addCloud(cloud3, null, 50, .55);
addCloud(cloud1, null, 125, .97, …Run Code Online (Sandbox Code Playgroud) 我可以从谷歌搜索中看到这个问题被问到很多,但我找到的解决方案都没有为我做.你知道,我用Java制作一个带有图像的游戏,因为游戏通常都有这些东西.但整个表格不断闪烁,我无法阻止它.是的,我已经双重缓冲它并覆盖了update()方法,虽然这肯定有帮助,但闪烁仍然存在.我不知道我在做双重缓冲是否有问题,或者我是否需要完全不同的东西.
起初我觉得它可能与clearRect()行有关,但在删除它之后,游戏仍然闪烁,但当然每次都没有清除.所以这根本没有帮助.在减慢定时器时,闪烁几乎完全消失,但我需要将其减慢到100ms,即便如此,我仍然会有一些闪烁.此外,这对游戏来说太慢了.我试过让一个计时器在一个10ms的计时器上完成所有工作,一个单独的计时器在100ms进行绘画,但它看起来只是看起来很蠢.虽然闪烁仍然是一个问题,但我可以将绘画计时器减慢到大约30毫秒并保持平滑.
必须有一种方法每10-30ms这样做而不会闪烁.是否有其他方法类似于双缓冲但在这种情况下更好,或者我可以使用的东西?在此先感谢您的帮助.
public class main extends JApplet implements ActionListener {
//This Declares The Variables
Graphics2D buffer;
Image offscreen;
Timer timGame = new Timer(10, this);
//other variables
public void init(){
//This Creates The Offscreen Buffer Zone
offscreen = createImage(getSize().width, getSize().height);
buffer = (Graphics2D)offscreen.getGraphics();
//other initialization stuff irrelevant to drawing
}
public void actionPerformed(ActionEvent evt){
if (evt.getSource() == timGame)
runGame();
}
private void runGame(){
//Do stuff, move objects
repaint();
}
public void paint(Graphics g){
super.paint(g);
buffer.clearRect(0, 0, getSize().width, getSize().height);
//draw stuff …Run Code Online (Sandbox Code Playgroud) 我正在开发一款小型街机视频游戏,我正在寻找双重缓冲来改善动画效果.我有一个类应该绘制空白图像,另一个类应该绘制一个简单的线.但是,我一直在应该绘制线的行上得到NullPointerException
class Render extends JPanel {
public int dbWidth = 500, dbHeight = 400;
public Image dbImage = null;
public Graphics dbg;
public void gameRender() {
if( dbImage == null )
dbImage = createImage( dbWidth, dbHeight );
dbg = dbImage.getGraphics();
dbg.setColor( Color.white );
dbg.fillRect( 0, 0, dbWidth, dbHeight );
}
}
class MC extends Render {
public Render render = new Render();
public void draw() {
render.gameRender();
dbg.drawLine( 100, 100, 200, 200 ); // line where NullPointerException occurs
}
} …Run Code Online (Sandbox Code Playgroud) java inheritance swing nullpointerexception double-buffering
我面临着极大的担忧.我目前正参与三明治课程,我正在学习如何在嵌入式系统上开发软件 - 就像在open1788板上一样.
我计划实现双缓冲功能,因为我可以在液晶屏上看到闪烁.确实可以在绘制时查看形状!
通过双缓冲,重绘整个屏幕的速度足够快.也许我应该深入研究管理裁剪,所以我只需要重新绘制需要的部分屏幕?但那不是问题.
所以,我写了几个函数来处理双缓冲选项.如果我不希望软件使用双缓冲,那么我不会为它分配内存; 否则我会.
问题是为堆分配的默认空间最多为1024字节.我的临时缓冲区长度为261120字节!(每272像素高481像素宽,每一个16bpp).
因此,malloc返回NULL.
我采取的第一个解决方案是放置静态缓冲区,我的意思是:
static WORD s_double_buf[481*272];
Run Code Online (Sandbox Code Playgroud)
但明显的缺点是,即使您不使用双缓冲,它仍然会被分配.
第二种解决方案是编辑配置文件以使堆更大,每个替换1024个字节,例如1048576字节(0x100000).我不喜欢这个解决方案,因为我应该专注于节省内存空间.
也许我非常想念嵌入式编程技巧?根据那个,最好的解决方案是什么?我怎么能取得进步?我没有告诉你我阅读和挖掘数据表的混乱能力.
我真的很感激,如果有人能为我提供初学者的参考资料,大多数都适合我正在编程的电路板.
提前致谢!
我刚刚开始编码,我想我忘记了如何加倍缓冲.这是我现在的代码,我不确定我缺少什么.当我开始它只有一个白色的屏幕,没有椭圆形.
渲染中的错误是什么?
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JFrame;
public class Graphs extends JFrame {
private Image dbImage;
private Graphics dbg;
public static void main(String[] args) {
new Graphs();
}
public Graphs() {
setSize(1000, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setTitle("Graphs");
setLocationRelativeTo(null);
setVisible(true);
}
public void paint(Graphics g) {
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
dbg.drawImage(dbImage, 0, 0, this);
}
public void paintComponent(Graphics g) {
g.drawOval(200, 200, 200, 200);
repaint();
}
}
Run Code Online (Sandbox Code Playgroud)
@OverrideThe method paintComponent(Graphics) of type Graphs must override …Run Code Online (Sandbox Code Playgroud) 好吧我正在使用一个时间为50毫秒的计时器来动画一些移动的文本(技术上它在文本之间滚动).
问题是,如果你仔细观察,你可以看到文字闪烁,并且id不喜欢闪烁..
所以我对动画不是那么好但是我能做些什么来减少闪烁?也许更快的过去时间?或者我应该甚至使用计时器吗?
编辑:
所以我试图实现双缓冲,我显然做了一些事情.
这是没有双缓冲的代码,这个工作正常,但有点闪烁.
void PaintScrollingText(ScrollingText *Settings, WPARAM wParam)
{
HDC hdc;
PAINTSTRUCT ps;
HANDLE hOldFont;
RECT rect;
hdc = wParam ? (HDC)wParam : BeginPaint(Settings->hWnd, &ps);
hOldFont = SelectObject(hdc, Settings->hFont);
SetTextColor(hdc, Settings->crForeGnd);
SetBkColor(hdc, Settings->crBackGnd);
GetClientRect(Settings->hWnd, &rect);
rect.right -= Settings->txt1XOffset;
DrawText(hdc, Settings->szText1, -1, &rect, DT_RIGHT);
rect.right += Settings->txt1XOffset - Settings->txt2XOffset;
DrawText(hdc, Settings->szText2, -1, &rect, DT_RIGHT);
SelectObject(hdc, hOldFont);
if (!wParam) EndPaint(Settings->hWnd, &ps);
}Run Code Online (Sandbox Code Playgroud)
这是我的代码,带有双缓冲.
void PaintScrollingText(ScrollingText *Settings, WPARAM wParam) …Run Code Online (Sandbox Code Playgroud) 我无法阻止闪烁。我得到了添加双缓冲的建议。我怎么做?
#include <iostream>
#include <windows.h>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
namespace {
const int ID_NEW = 1;
const int ID_QUIT = 2;
const int ID_ABOUT = 3;
const int NORTH_BUTTON_ID = 4;
const int SOUTH_BUTTON_ID = 5;
const int WEST_BUTTON_ID = 6;
const int EAST_BUTTON_ID = 7;
const int ID_FINISHED_GAME = 8;
int x = 0;
int y = 0;
int xStart = 0;
int yStart = 0;
int windowHeight = 400;
int windowWidth = 500; …Run Code Online (Sandbox Code Playgroud) 所以我想制作一个廉价的Gyazo副本(截图工具)
问题是光标坐标是闪烁的,我该如何防止?我已经尝试过,WM_ERASEBKGND但它没有任何帮助.
我的代码还有什么问题吗?任何不良做法/技巧?
#include <Windows.h>
#include <string>
#include <gdiplus.h>
#pragma comment (lib, "Gdiplus.lib")
// Store the "screenshot" when first launching the program
HBITMAP hbm;
// This draws the cursor coordinates close to the cursor
void DrawCursorCoords(Gdiplus::Graphics &graphics, Gdiplus::Bitmap &bitmap, Gdiplus::Color c)
{
POINT cursorPos;
GetCursorPos(&cursorPos);
std::wstring x = std::to_wstring(cursorPos.x);
std::wstring y = std::to_wstring(cursorPos.y);
graphics.DrawString(x.c_str(), x.length(), &Gdiplus::Font(L"Consolas", 16), Gdiplus::PointF(cursorPos.x, cursorPos.y), &Gdiplus::SolidBrush(c));
graphics.DrawString(y.c_str(), y.length(), &Gdiplus::Font(L"Consolas", 16), Gdiplus::PointF(cursorPos.x, cursorPos.y + 16), &Gdiplus::SolidBrush(c));
}
// Paint our stuff
void Paint(HDC &hdc)
{ …Run Code Online (Sandbox Code Playgroud)