Results 1 to 2 of 2
  1. #1
    bobCallahan24 is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Default java graphics question

    Java Code:
    import java.awt.Color;
    import java.awt.event.KeyListener;
    import java.awt.event.KeyEvent;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import java.awt.Dimension;
    
    public class Tanks extends JPanel
    {
        private static int x = 150, y = 150;
        private static int c_x = 0, c_y = 0;
        private static boolean cannon = false;
    
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.setColor(Color.BLACK);
            g.fillRect(x, y, 50, 50);
    
            if(cannon)
            {
    			g.setColor(Color.RED);
    			g.fillRect(c_x, c_y, 5, 5);
    		}
        }
    
        public static void main(String[] args)
        {
            JFrame jf=new JFrame();
            jf.setResizable(false);
            jf.setDefaultCloseOperation
               (JFrame.EXIT_ON_CLOSE);
            final Tanks Tanks = new Tanks();
            jf.setPreferredSize(new Dimension(495, 605));
            jf.add(Tanks);
            jf.addKeyListener(new KeyListener()
            {
                public void keyPressed(KeyEvent event)
                {
                    if(event.getKeyCode() == KeyEvent.VK_RIGHT)
                    {
    					cannon = false;
    
    					if(Tanks.x <= 430)
    					{
                            Tanks.x+=10;
                            Tanks.repaint();
    					}
                    }
                    else if(event.getKeyCode() == KeyEvent.VK_LEFT)
                    {
    					cannon = false;
    
    					if(Tanks.x >= 10)
    					{
    					    Tanks.x -= 10;
    					    Tanks.repaint();
    			        }
    				}
    	            else if(event.getKeyCode() == KeyEvent.VK_UP)
                    {
    					cannon = false;
    
    					if(Tanks.y >= 10)
    					{
    					    Tanks.y -= 10;
    					    Tanks.repaint();
    			        }
    				}
                    else if(event.getKeyCode() == KeyEvent.VK_DOWN)
                    {
    					cannon = false;
    
    					if(Tanks.y <= 520)
    					{
    					    Tanks.y += 10;
    					    Tanks.repaint();
    			        }
    				}
                    else if(event.getKeyCode() == KeyEvent.VK_SPACE)
                    {
    					cannon = true;
    					c_x = x + 25;
    			        c_y = y - 10;
    
    					while(Tanks.c_y >= 0)
    					{
    						Tanks.c_y -= 1;
    					    Tanks.repaint();
    					}
    				}
                }
    
                public void keyReleased(KeyEvent event)
                {}
    
                public void keyTyped(KeyEvent event)
                {}
            });
    
            jf.pack();
            jf.setVisible(true);
        }
    }
    In this program, I want the cannon to appear to fire out of the tank. Right now, it instantly appears at the top of the window after hitting the space bar. How can I fix this problem?
    Last edited by Fubarable; 01-14-2010 at 11:06 PM. Reason: code tags added

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    Java executes in multiple threads. One of these threads is the event dispatch thread.
    It is the event dispatch thread that executes for mouse hits and kestrokes.
    It is also the thread for that executes paint().
    A call to repaint() does not cause paint() to be done immediately.
    Instead repaint() leaves a note. Later, after all user input events have been processed,
    the note tells the main dispatcher to run the paint() method on the event dispatch thread.

    Java Code:
                  else if(event.getKeyCode() == KeyEvent.VK_SPACE)
                    {
    					cannon = true;
    					c_x = x + 25;
    			        c_y = y - 10;
    
    					while(Tanks.c_y >= 0)
    					{
    						Tanks.c_y -= 1;
    					    Tanks.repaint();
    					}
    				}
    This code seems to want to animate the cannon. It changes y, calls reapint() and repeats.
    But nowhere in the while loop does the thread stop executing its response
    to the SPACE key event.
    In other words, what is happening is that repaint() is trying to leave a bunch of notes.
    (All but the first is actually ignored.)
    So when paint is finally called, y has been set to the top.

    To do animation, it has to be broken into time-separated events on the event dispatch thread.
    A Timer object can do this.

    For an overview and an example of using Timer, see Threads and Swing


    {Please try to do a better job of indentation. And remember:
    right curly braces go to the left and left curly braces go to the right.
    Like this:
    Java Code:
        else if(event.getKeyCode() == KeyEvent.VK_SPACE) {
            cannon = true;
            c_x = x + 25;
            c_y = y - 10;
            while(Tanks.c_y >= 0) {
                Tanks.c_y -= 1;
                Tanks.repaint();
            }
        }
    Last edited by zweibieren; 01-15-2010 at 06:09 AM.

Similar Threads

  1. Replies: 2
    Last Post: 01-16-2010, 04:50 PM
  2. Java Graphics
    By svaidya in forum New To Java
    Replies: 8
    Last Post: 12-07-2009, 05:11 PM
  3. Java 2d Graphics Drag and Drop
    By shawleigh17 in forum AWT / Swing
    Replies: 9
    Last Post: 07-15-2009, 09:27 PM
  4. Want to learn Java Graphics
    By loggen in forum New To Java
    Replies: 7
    Last Post: 01-03-2009, 04:15 PM
  5. Problem in printing java graphics
    By Mahendra in forum Java 2D
    Replies: 0
    Last Post: 01-23-2008, 12:45 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
  •