Passive rendering method - java

I am building a simple game rendering method. I have a static images like map background that isn't really changing at all . I am just using like 30% of the whole screen for changing graphics. And I don't feel like it is necessary to render all that stuff over and over again if it isn't changing..
on internet I found something about passive rendering - You draw what you need and wait... until something updates .. after that you update it and again wait.
Looks like a great method for this situation . But now ...
I have a render method like that :
public class X extends Canvas{
//some method
this.createBufferStrategy(3);
//....
public void render(){
BufferStrategy buffer = this.getBufferStrategy();
Graphics g = buffer.getDrawGraphics();
//Draws black background...
g.setColor(Color.BLACK);
g.fillRect(....
for(... //cycles every object and calls their render methods...
}
This is unable to maintain draw and wait method...becouse the background is redrawn every 1/30 of second so If you don't have anything to draw, you get black canvas with nothing on it.. so you have to always redraw everything each update...If I don't draw the background , buffer start to blink like hell... so I have to have something to cover that stuff up.
I searched on internet and found nothing much about other solutions for this rendering type.. Tuns of stuff for active rendering but nothing for that Draw and wait method..
I think it's impossible to do that with BufferStrategy. Is there something else to serve for this ? Also is there other source for Graphics g ? On internet was something about calling it directyl from Canvas but I didn't get it running for this. Or am I thinking about it from wrong angle?

You're confusing passive rendering with partial updates. In "passive rendering" you can redraw everything, but you don't do it 30fps. you only do it when something moves. In "partial rendering", you only draw on the part of the canvas that changed.
The two can be put together, so that you only draw what changes, and only when it changes. That's the ideal case.
But passive rendering doesn't work if something is always moving, and partial updates don't work if the whole background is updating constantly.

Related

Stop clearing graphics on repaint();?

I'm creating a simple Breakout game and I've encountered a couple of problems.
First off, I need to render the bricks, the paddle, etc... I need to generally render everything on the screen that's needed. However, I don't want, for example, the bricks to continuously render; you can see where the render is occurring as it happens. There's a flash of white.
So, my question is: is it possible to render only certain objects, or more specifically, only render what needs to be rendered?
Every time I call repaint(); I don't want the whole screen to clear and repaint.

Simple painting

I have an seemingly very simple task at hand. I have a grid (500x500 right now) I want to visualize as it is populated and I want to write a class in Java that makes this easy for me to do. I'm thinking something along the line of:
public class Screen {
...
public void plot(x,y) {
// change the color of pixel x,y to black
}
public void clear() {
// fill the screen with white
}
}
I have been looking around and quickly found Canvas in awt, however from what I have been able to figure out so far, this widget will only allow me to draw on to it by overriding its paint method. This is far from optimal in my case as this will require me to draw the entire grid every time I wish to do just plot one single pixel.
Is there any way to get canvas to just draw a single pixel rather than the entire canvas? Or some other way to accomplish what I look for here?
I would prefer to avoid having to use any external libraries.
You will need to override the paint method to display the entire grid.
However, what you can do is create a BufferedImage that flips the one pixel, and draw that entire image to the component in the paint method, using Graphics.drawImage().
Unfortunately you have to override paint() and render the entire grid every time paint() is called. That's how graphical components work - the windows system/OS may request to repaint the component at any time (eg. when the window is re-shown/resized/moved)

Overiding AWT and storing graphics

-Hi all! I'm making a Java applet that simulates wave interference, which I have almost finished (will license under GPL). However, I have two questions regarding the AWT paint cycle that I am having difficulty finding answers to.
I want to make an 'about' overlay that appears when I press a button. The way I want to do this is to draw over the entire applet window with my static message and legend objects. The problem is stopping the AWT components from drawing themselves in the foreground without using remove(). Is there a way I can stop AWT from drawing itself temporarily?
For my standing waves mode I want to have node and anti-node markers calculated and drawn to a secondary graphics every time the standing wave reaches a maximum amplitude (all of which I can do myself), but drawn to the primary graphics (and thus displayed) every paint cycle. Could someone explain the steps to do so? I imagine it would involve creating a second graphics object, drawing to it once, then drawing it to the primary graphics every cycle.
If you are able to answer either of my questions I would be very grateful!
Cheers, Jack Allison
Responding to your first question:
You can't disable the paint()/paintComponent() method if you've inlcluded it in your code. If its there, it runs. However, you can create a flag so that only if the flag is true, the stuff gets drawn. Let me show you what I mean:
boolean flag;
...
public void paintComponent(Graphics comp) {
if (flag) {
Graphics2D comp2D = (Graphics2D) comp;
//drawing statements
}
}
public void actionPerformed(ActionEvent event) {
flag = true;
repaint();
}

Java: Painting multiple objects to one frame

I've been working on a java game recently, and I have a lot of it figured out. One thing still plagues me, however. The way it's set up, a player moves across a background (the game board). Currently, every time the player moves, it repaints the whole frame, including the background. This causes a brief, yet annoying screen flicker whenever the player moves.
I've separated out my code to draw the background separately from the things that need to be repainted:
public void drawMap(Graphics pane) {...}
public void drawPlayer(Graphics pane) {...}
The problem is that I can't find a way to make the board stay on the screen when I use repaint(); , a necessity for the player to move. Any suggestions?
You should look into double buffering, basically you paint an image to the buffer, then paint the buffer. It should remove the flickering effect you are talking about. Below are a few helpful links:
http://docs.oracle.com/javase/tutorial/extra/fullscreen/doublebuf.html
http://content.gpwiki.org/index.php/Java:Tutorials:Double_Buffering
http://www.ecst.csuchico.edu/~amk/classes/csciOOP/double-buffering.html
Just comment if your having trouble understanding it.
UPDATE: I would also suggest you look in 'Killer game programming in java'. You can get a free ebook of one of the older versions. Some of it is a bit out dated, but the first few chapters about setting up a game loop and drawing to the screen etc are still very much relevant.
UPDATE 2: From the second link, try something like this:
private void drawStuff() {
BufferStrategy bf = this.getBufferStrategy();
Graphics g = null;
try {
g = bf.getDrawGraphics();
drawMap(g);
drawPlayer(g);
} finally {
// It is best to dispose() a Graphics object when done with it.
g.dispose();
}
// Shows the contents of the backbuffer on the screen.
bf.show();
//Tell the System to do the Drawing now, otherwise it can take a few extra ms until
//Drawing is done which looks very jerky
Toolkit.getDefaultToolkit().sync();
}
UPDATE 3: This post here gives a nice code sample that you can play with and adapt, that should give you the best idea on how to do double buffering
I suggest to avoid redrawing everything with every change. Instead draw the whole frame at a fixed interval, e.g. every 50ms. Just keep the status of every element in a class and if something changes just change the data value. Due to the fixed redrawing interval the display will pick up any changes at the next redraw.

Java setClip seems to redraw

I'm having some troubles with setClip in Java. I have a class that extends JPanel. Within that class I have overridden the paintComponent method. My paintComponent method looks something like this:
paintComponent {
//draw some lines here
Rectangle whole = g2.getClipBounds();//g2 is my Graphics2D object
Rectangle part = <some rectangle that is a part of the whole paintable area>;
g2.setClip(part);
//draw some more stuff here
g2.setClip(whole);
}
The problem that I'm seeing is that the area in the clipped region seems to be painted repeatedly. For example, if I tell it to paint, it paints just fine. But then, if I switch windows or somehow else cause it to paint the same thing again, the clipped region isn't cleared while the rest is. This results in the painting on the clipped region to appear bolder than the rest of the paintable area.
I imagine that I'm missing something in how setClip works.
Any suggestions would be much appreciated. Thanks in advance for any help.
Creating a new Graphics object from the old one did the trick for me, as adviced by Tom.

Categories

Resources