Skip to main content

How to fast texture paint ?

1 reply [Last post]
u5321007
Offline
Joined: 2003-08-06
Points: 0

Hi , I want to drawing the IC layout and each layer with specific texture.

I have tried using TexturePaint in Java2D, but it is really very slow. Can anybody give me a help ?

I think the solution is :

(1) create a BufferedImage with transparent support color model
(2) fill the texture to the buffered image.
(3) using "Mask" to select what region in the buffered Image I want. Much like the clip() method in graphics.
(4) draw this buffered Image to the canvas

I hope the BufferedImage using very small memory because I only use 1 color in this image. I hope it can be reuse when drawing to different layer.

Thanks lot.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
u5321007
Offline
Joined: 2003-08-06
Points: 0

Hi everybody,

Here is my solution, using biMask and alhpasComposite, it works. I am wondering anybody can provide more better solution, for example only use one BufferedImage. :~)
One more issue:
Since I have to drawing multiple layer, and each layer
has different color(but still one color). Is there anyway to change the color model in BufferedImage?

-------------------------------------------
import java.awt.*;
import java.awt.image.*;

public class Test4 extends Frame
{

BufferedImage bi;
BufferedImage biMask;
public Test4(){
Color color = Color.cyan;
IndexColorModel colorModel = new IndexColorModel(1, 2,
new byte[]{0, (byte) color.getRed()},
new byte[]{0, (byte) color.getGreen()},
new byte[]{0, (byte) color.getBlue()},0);

setBackground(Color.black);

Rectangle b = new Rectangle(640, 480);

bi = new BufferedImage(b.width, b.height
, BufferedImage.TYPE_BYTE_INDEXED, colorModel);

biMask = new BufferedImage(b.width, b.height
, BufferedImage.TYPE_BYTE_INDEXED, colorModel);

Graphics2D big = (Graphics2D)bi.getGraphics();
Graphics2D biMaskg = (Graphics2D)biMask.getGraphics();
WritableRaster wr = bi.getRaster();

int[] v = {1};
for(int i = 0; i < b.width; i+= 4){
for(int j = 0; j < b.height; j+= 4){
wr.setPixel(i, j,v);
}
}

biMaskg.fillRect(50, 50, 200, 100);
biMaskg.fillRect(50, 250, 100, 200);
big.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN));
big.drawImage(biMask, 0, 0, null);

biMaskg.dispose();
big.dispose();
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.red);
g2d.fillRect(10, 10, 100, 100);
g2d.drawImage(bi, 0, 0, null);
}

public static void main(String args[])
{
Test4 t = new Test4();
t.setBounds(new Rectangle(0, 0, 200, 200));
t.setVisible(true);
}

}