Results 1 to 6 of 6
Thread: repaint without flashing
- 01-15-2011, 03:17 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 33
- Rep Power
- 0
repaint without flashing
Hi I'm trying to get this program to paint a square then be able to move it around the screen without flashing.. but it keeps flashing. I would rather have this be swing and not an applet. where should i put the repaint code?
Java Code:import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.geom.QuadCurve2D; import javax.swing.JApplet; public class DrawingStuff extends JApplet implements KeyListener { int x = 100; int y = 100; public void paint(Graphics g) { super.paint(g); Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.green); g2.fill3DRect(x, y, 55, 55, true); repaint(); } public void init(){ setSize(600,600); addKeyListener(this); } @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_LEFT: x = x - 1; break; case KeyEvent.VK_RIGHT: x = x + 1; break; case KeyEvent.VK_UP: y = y - 1; break; case KeyEvent.VK_DOWN: y = y + 1; break; } } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } }
-
Don't paint directly in the applet's paint methd but rather in the paintComponent method of a JPanel that is held in the applet. This will allow your app to use Swing's double buffering. Also, it makes little sense to call repaint() inside of a paint or paintComponent method. If Java weren't smart, this could cause a recursive nightmare.
Edit: and you'll want to use a Swing Timer for your animation loop.Last edited by Fubarable; 01-15-2011 at 03:33 PM.
-
Or perhaps you don't need the Timer, depending on what you will be doing with this animation. For e.g.,
Java Code:import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JApplet; import javax.swing.JPanel; @SuppressWarnings("serial") public class DrawingStuff2 extends JApplet { public void init() { try { javax.swing.SwingUtilities.invokeAndWait(new Runnable() { public void run() { createGUI(); } }); } catch (Exception e) { System.err.println("createGUI didn't successfully complete"); } } private void createGUI() { getContentPane().add(new DrawingStuff2Panel()); setSize(600, 600); } } @SuppressWarnings("serial") class DrawingStuff2Panel extends JPanel { protected static final int DELTA = 4; private int x = 100; private int y = 100; public DrawingStuff2Panel() { // if using a key listener, then the component, here the JPanel must have focus setFocusable(true); requestFocusInWindow(); addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_LEFT: x = x - DELTA; break; case KeyEvent.VK_RIGHT: x = x + DELTA; break; case KeyEvent.VK_UP: y = y - DELTA; break; case KeyEvent.VK_DOWN: y = y + DELTA; break; } repaint(); } }); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.green); g2.fill3DRect(x, y, 55, 55, true); } }
Better still would be to use key binding instead of the key listener
- 01-15-2011, 04:59 PM #4
Member
- Join Date
- Jan 2009
- Posts
- 33
- Rep Power
- 0
so here i can make the square go up and to the left... but is their any way to have it go up and left at the same time? diagonal?
Java Code:public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_LEFT: x = x - DELTA; break; case KeyEvent.VK_RIGHT: x = x + DELTA; break; case KeyEvent.VK_UP: y = y - DELTA; break; case KeyEvent.VK_DOWN: y = y + DELTA; break; } repaint(); }
- 01-15-2011, 05:01 PM #5
Member
- Join Date
- Jan 2009
- Posts
- 33
- Rep Power
- 0
d
so here i can make the square go up and to the left... but is their any way to have it go up and left at the same time? diagonal?
Java Code:public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_LEFT: x = x - DELTA; break; case KeyEvent.VK_RIGHT: x = x + DELTA; break; case KeyEvent.VK_UP: y = y - DELTA; break; case KeyEvent.VK_DOWN: y = y + DELTA; break; } repaint(); }
-
For this, I think that you'll have to use key binding and a Swing Timer. Check out the Swing tutorial on this subject. You'll need to use the more advanced case where you check when the key is pressed and released. There are also examples of doing this on this forum (I know because I wrote some of them) and on other fora which Google can show you.
Similar Threads
-
Flashing JFrame
By ilyvatar in forum AWT / SwingReplies: 3Last Post: 12-01-2010, 09:52 AM -
MouseListener - Flashing label
By Adomini in forum New To JavaReplies: 4Last Post: 11-29-2010, 09:31 PM -
repaint every
By 3xpr1ment in forum AWT / SwingReplies: 10Last Post: 03-23-2010, 05:39 PM -
Remove Flashing in Applet
By Unome in forum Java AppletsReplies: 5Last Post: 05-30-2009, 07:26 PM -
Flashing
By Supamagier in forum Java 2DReplies: 6Last Post: 04-29-2009, 03:43 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks