Results 1 to 6 of 6
  1. #1
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    100
    Rep Power
    0

    Exclamation 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
    	
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  4. #4
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    100
    Rep Power
    0

    Default

    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();
             }

  5. #5
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    100
    Rep Power
    0

    Default 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();
             }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by natdizzle View Post
    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

  1. Flashing JFrame
    By ilyvatar in forum AWT / Swing
    Replies: 3
    Last Post: 12-01-2010, 09:52 AM
  2. MouseListener - Flashing label
    By Adomini in forum New To Java
    Replies: 4
    Last Post: 11-29-2010, 09:31 PM
  3. repaint every
    By 3xpr1ment in forum AWT / Swing
    Replies: 10
    Last Post: 03-23-2010, 05:39 PM
  4. Remove Flashing in Applet
    By Unome in forum Java Applets
    Replies: 5
    Last Post: 05-30-2009, 07:26 PM
  5. Flashing
    By Supamagier in forum Java 2D
    Replies: 6
    Last Post: 04-29-2009, 03:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •