Page 2 of 2 FirstFirst 12
Results 21 to 23 of 23
  1. #21
    Rolf83's Avatar
    Rolf83 is offline Member
    Join Date
    Jun 2011
    Location
    Malmö, Sweden
    Posts
    18
    Rep Power
    0

    Default

    Ok I feel mighty stupid, but now I managed to solve my problem, ofcourse it was very simple....

    Java Code:
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.JPanel;
    
    public class drawtest extends JPanel implements KeyListener {
    
    private static final long serialVersionUID = 1L;
        int width, height;
        int kx, ky;
    
    
      public drawtest() {
          kx = 10;
          ky = 10;
          addKeyListener( this );
      }
      
      public void paintComponent(Graphics g) {
    	  g.drawLine( kx, ky, kx, ky );
      }
      
    	public void keyPressed(KeyEvent e) {
    
                        if (e.getKeyChar()=='d'){kx ++;}
    	    if (e.getKeyChar()=='s'){ky ++;}
    	    if (e.getKeyChar()=='a'){kx --;}
    	    if (e.getKeyChar()=='w'){ky --;}    
    	    repaint();
    	    e.consume();
    	}
    
    	public void keyReleased(KeyEvent e) {}
    	public void keyTyped(KeyEvent e) {}
    
    }
    This does the trick for me, and I call it from my JFrame like this:

    Java Code:
    	        drawtest drawtest= new drawtest();
    	        jContentPane.add(drawtest, BorderLayout.CENTER);
    	        drawtest.setFocusable(true);
    Last edited by Rolf83; 06-04-2011 at 01:39 AM.

  2. #22
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I don't have the patience to read through the entire posting so I'll just make one comment and suggestion.

    It is obvious you copied the code from an Applet. The code you copied is used for AWT and should not be used for Swing. Mainly, you should never override of invoke the update() method.

    For standard Swing painting read the Swing tutorial Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) for working examples and explanations.

  3. #23
    Rolf83's Avatar
    Rolf83 is offline Member
    Join Date
    Jun 2011
    Location
    Malmö, Sweden
    Posts
    18
    Rep Power
    0

    Default

    I totally agree, thats why I solved it and posted the solution without using the update function, just don't know how to write solved in the headline... If you read the the last post before you posted you will see my solution... Sorry

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Draw on JPanel?
    By PhQ in forum New To Java
    Replies: 2
    Last Post: 07-17-2010, 12:19 AM
  2. Replies: 4
    Last Post: 08-28-2009, 07:50 PM
  3. Replies: 2
    Last Post: 03-22-2009, 02:53 PM
  4. how to draw x-y graph in Jpanel.--not in APPLET.
    By vincent2001@gmail.com in forum New To Java
    Replies: 2
    Last Post: 08-24-2008, 06:01 AM
  5. Draw on JPanel, Help
    By carl in forum Java 2D
    Replies: 1
    Last Post: 07-31-2007, 07:56 AM

Posting Permissions

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