Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Drawing to an image to later be used as background

$
0
0
I've been working on this problem for a while, and am not even sure how to
phrase it exactly... What I'm trying to do is print a grid onto an image
one time so that it can later be used as a background and I don't have to
'redraw' it with every paint refresh.

Basically, how do I draw to an image, and then use it for the rest of a
program's lifetime?

Here is the code I've been working with:

public void paint(Graphics g){
		BufferStrategy bf = this.getBufferStrategy();
		if(drawGrid == null) {
			drawGrid = this.getBufferStrategy();
			// Draw Grid
			g = drawGrid.getDrawGraphics();
			g.setColor(Color.white);
			g.fillRect(0, 0, WIDTH, HEIGHT);
			for(int i=0;i<numberOfTilesH;i++){
				for(int j=0;j<numberOfTilesW;j++){
				//	g.drawRect((int)gridCell[j][i].getPosition().getX(), (int)gridCell[j][i].getPosition().getY(), 5, 5);
				}
			}
			g.dispose();
		}
		try {

			if (character != null){
				character.draw((Graphics2D) g);
			}
			g.dispose();
		}
		drawGrid.show();
		bf.show();

		Toolkit.getDefaultToolkit().sync();
	}




My codes is trying to draw the grid in that first part, if it doesn't
exist already. The second part is drawing a character to the screen.

I hope this makes sense, please let me know if there's a need for more
clarity.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>