Results 1 to 4 of 4
Thread: Finally done :D
- 12-22-2008, 07:49 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
Finally done :D
I finally finished my pong program. However, as it seems to apply to every applet I make, what the heck is up with the constant flickering on the screen? It gets worse and worse as the game (or program) continues. I tried changing the refresh rate, but that only makes the flickering a little harder to see but in the long run doesn't matter because the blinking becomes so excessive you can hardly even see the graphics I've coded onto the screen. Are applets just like this or can I get an opinion on how to fix it? If you need me source code, ask and I'll be happy to post it.
Thanks,
Shadaw
- 12-23-2008, 08:03 AM #2
Ways to eliminate flicker in AWT animations:
1 — override the update method to call paint
2 — use limited repaints, viz, the repaint method with four parametersJava Code:public void update(Graphics g) { paint(g); }
3 — use an offscreen image, aka, double buffering.
Here's a demonstration of option 3:
Java Code:// <applet code="Anima" width="600" height="300"></applet> import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Anima extends Applet { SimpleAnimation simple = new SimpleAnimation(); BufferedAnimation buffered = new BufferedAnimation(); AnimaEngine engine; public void init() { Panel animaPanel = new Panel(new GridLayout(1,0,10,0)); animaPanel.setBackground(Color.black); animaPanel.add(simple); animaPanel.add(buffered); Animable[] animables = { simple, buffered }; engine = new AnimaEngine(animables); setLayout(new BorderLayout()); add(animaPanel); add(getLabelPanel(), "Last"); addListeners(); } private Panel getLabelPanel() { Panel panel = new Panel(new GridLayout(1,0,10,0)); panel.setBackground(Color.black); panel.add(getLabel("simple", Color.white)); panel.add(getLabel("buffered", Color.white)); return panel; } private Label getLabel(String s, Color color) { Label label = new Label(s, Label.CENTER); label.setBackground(color); label.setFont(new Font("dialog", Font.PLAIN, 20)); return label; } private void addListeners() { new Thread(new Runnable() { public void run() { // Wait for gui to stabilize to avoid generating // events before components are fully initialized. try { Thread.sleep(250); } catch(InterruptedException e) { System.out.println("addComponentListeners interrupted"); } simple.addComponentListener(resizer); buffered.addComponentListener(resizer); } }).start(); } public void start() { engine.start(); } public void stop() { engine.stop(); } private ComponentListener resizer = new ComponentAdapter() { public void componentResized(ComponentEvent e) { simple.reset(); buffered.reset(); } }; } class SimpleAnimation extends Panel implements Animable { Point center = new Point(100, 150); int R = 24; int dx = 3; int dy = 2; public void paint(Graphics g) { g.setColor(Color.white); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.red); g.fillOval(center.x-R, center.y-R, 2*R, 2*R); } public void advance() { if(center.x - R + dx < 0 || center.x + R + dx > getWidth()) { dx *= -1; } if(center.y - R + dy < 0 || center.y + R + dy > getHeight()) { dy *= -1; } center.x += dx; center.y += dy; repaint(); } public void reset() { center.setLocation(100, 150); } } class BufferedAnimation extends Panel implements Animable { Point center = new Point(225, 200); int R = 24; int dx = -3; int dy = 2; Image image; Graphics ig; public void paint(Graphics g) { g.setColor(Color.blue); g.fillOval(center.x-R, center.y-R, 2*R, 2*R); } public void update(Graphics g) { if(image == null) { image = createImage(getWidth(), getHeight()); ig = image.getGraphics(); } ig.setColor(Color.white); ig.fillRect(0, 0, getWidth(), getHeight()); paint(ig); g.drawImage(image, 0, 0, this); } public void advance() { if(center.x - R + dx < 0 || center.x + R + dx > getWidth()) { dx *= -1; } if(center.y - R + dy < 0 || center.y + R + dy > getHeight()) { dy *= -1; } center.x += dx; center.y += dy; repaint(); } public void reset() { center.setLocation(225, 200); image = null; ig.dispose(); } } interface Animable { public void advance(); } class AnimaEngine implements Runnable { Animable[] animables; Thread thread; long delay = 40; boolean animating = false; public AnimaEngine(Animable[] animables) { this.animables = animables; } public void run() { while(animating) { try { Thread.sleep(delay); } catch(InterruptedException e) { animating = false; } for(int i = 0; i < animables.length; i++) { animables[i].advance(); } } } public void start() { if(!animating) { animating = true; thread = new Thread(this); thread.start(); } } public void stop() { animating = false; if(thread != null) { thread.interrupt(); thread = null; } } }
- 12-23-2008, 06:37 PM #3
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
how does public void update work?
I see the way it's intended to work, but what change must i make to all of my other painted graphics in order to compensate for the constant lines that public void update leaves? and thanks, the fix certainly did work, i just have this little problem now.
- 12-24-2008, 04:45 AM #4
how does public void update work?
This is best explained in the Method Detail section for the update method in the Component api. At the end of the description is a link to a helpful article with more discussion about AWT drawing.
but what change must i make to all of my other painted graphics in order to compensate for the constant lines that public void update leaves?
Not sure what you mean by "the constant lines that public void update leaves?"
Similar Threads
-
How to use Finally
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:40 PM -
Try---finally
By javarishi in forum New To JavaReplies: 14Last Post: 04-09-2008, 10:34 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks