java 0x2d_Java-Java2D性能问题

好吧,这是旧文章,但我想分享我在不使用BufferedImage的情况下使用Swing / AWT直接绘图的发现。

直接绘制到int []缓冲区时,更好地完成某种绘图(例如3D)。 完成图像后,您可以使用ImageProducer实例(如MemoryImageSource)来生成图像。 我假设您知道如何在不使用Graphics / Graphics2的情况下直接执行绘图。

/**

* How to use MemoryImageSource to render images on JPanel

* Example by A.Borges (2015)

*/

public class MyCanvas extends JPanel implements Runnable {

public int pixel[];

public int width;

public int height;

private Image imageBuffer;

private MemoryImageSource mImageProducer;

private ColorModel cm;

private Thread thread;

public MyCanvas() {

super(true);

thread = new Thread(this, "MyCanvas Thread");

}

/**

* Call it after been visible and after resizes.

*/

public void init(){

cm = getCompatibleColorModel();

width = getWidth();

height = getHeight();

int screenSize = width * height;

if(pixel == null || pixel.length < screenSize){

pixel = new int[screenSize];

}

mImageProducer = new MemoryImageSource(width, height, cm, pixel,0, width);

mImageProducer.setAnimated(true);

mImageProducer.setFullBufferUpdates(true);

imageBuffer = Toolkit.getDefaultToolkit().createImage(mImageProducer);

if(thread.isInterrupted() || !thread.isAlive()){

thread.start();

}

}

/**

* Do your draws in here !!

* pixel is your canvas!

*/

public /* abstract */ void render(){

// rubisch draw

int[] p = pixel; // this avoid crash when resizing

if(p.length != width * height) return;

for(int x=0; x < width; x++){

for(int y=0; y

int color = (((x + i) % 255) & 0xFF) << 16; //red

color |= (((y + j) % 255) & 0xFF) << 8; //green

color |= (((y/2 + x/2 - j) % 255) & 0xFF) ; //blue

p[ x + y * width] = color;

}

}

i += 1;

j += 1;

}

private int i=1,j=256;

@Override

public void run() {

while (true) {

// request a JPanel re-drawing

repaint();

try {Thread.sleep(5);} catch (InterruptedException e) {}

}

}

@Override

public void paintComponent(Graphics g) {

super.paintComponent(g);

// perform draws on pixels

render();

// ask ImageProducer to update image

mImageProducer.newPixels();

// draw it on panel

g.drawImage(this.imageBuffer, 0, 0, this);

}

/**

* Overrides ImageObserver.imageUpdate.

* Always return true, assuming that imageBuffer is ready to go when called

*/

@Override

public boolean imageUpdate(Image image, int a, int b, int c, int d, int e) {

return true;

}

}// end class

请注意,我们需要MemoryImageSource和Image的唯一实例。 除非已调整JPanel的大小,否则不要为每个帧创建新的Image或新的ImageProducer。 参见上面的init()方法。

在渲染线程中,询问repaint()。 在Swing上,repaint()将调用重写的paintComponent(),并在其中调用render()方法,然后要求imageProducer更新图像。完成图像后,使用Graphics.drawImage()进行绘制。

要获得兼容的图像,请在创建图像时使用正确的ColorModel。 我使用GraphicsConfiguration.getColorModel():

/**

* Get Best Color model available for current screen.

* @return color model

*/

protected static ColorModel getCompatibleColorModel(){

GraphicsConfiguration gfx_config = GraphicsEnvironment.

getLocalGraphicsEnvironment().getDefaultScreenDevice().

getDefaultConfiguration();

return gfx_config.getColorModel();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值