Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Whats wrong with my code? I tried several things but it still won't work!

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Whats wrong with my code? I tried several things but it still won't work!

    Whats wrong with this code?

    I Tried everything but " super.paintComponent(g); " part still gives the same error

    " - The method paintComponent(Graphics) is undefinedfor the type KeyAdapter "

    import javax.swing.JPanel;
    import java.awt.*;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
     
    public class Keying extends JPanel{
     
    	public Rectangle character;
     
    	public int charW= 24;
    	public int charH = 36;
     
    	public boolean right = false;
    	public boolean left = false;
    	public Keying(Display f, Images i){ 
    		character = new Rectangle(180, 180, charW, charH);
    		f.addKeyListener(new KeyAdapter() {
    			public void keyPressed(KeyEvent e){
    				if(e.getKeyCode() == KeyEvent.VK_D){
    					right = true;
    					character.x +=1;
    			}
    			if(e.getKeyCode() == KeyEvent.VK_A){
    				left = true;
    				character.x -= 1;
    			}
    		}	
    		 public void keyReleased(KeyEvent e){
    			 if(e.getKeyCode() == KeyEvent.VK_D);
    			     right = false;
    		     if(e.getKeyCode() == KeyEvent.VK_A){
    			     right = false;
    		 }
    		};
    	public void paintComponent(Graphics g){
    		 super.paintComponent(g); 
    		this.setBackground(Color.DARK_GRAY);
    		g.setColor(Color.WHITE);
    		g.fillRect(character.x, character.y, character.width, character.height);
     
    		if(right){
    			character.x +=1;
    		}
    		if(left){
    			character.x -=1;
    		}
    		repaint();
    	}
     
    	private void setBackground(Color darkGray) {
     
    	}
     
     
    	{
    }
    });
    	}}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Whats wrong with my code? I tried several things but it still won't work!

    he method paintComponent(Graphics) is undefinedfor the type KeyAdapter
    The compiler is saying that the super class for the KeyAdapter class does not have a paintComponent() method.

    Is the paintComponent() method in the code defined in the correct class?

    To be sure that the class has a method that can be overridden, add a @Override statement on the line just before the method definition. That asks the compiler to check if the class that the method is in has a method with that name and args.


    One big problem with the code is its poor formatting. The { and }s are not properly positioned to show nested code. Fixing that will show you what the problem is.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Cannot figure out why my code won't work!
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 24th, 2013, 10:54 AM
  2. My polymorphism code won't work
    By Floomer99 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 20th, 2012, 05:31 AM
  3. Whats wrong with my code?
    By Bryan29 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 5th, 2011, 09:12 AM
  4. Scanner code won't work
    By r19ecua in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 21st, 2011, 04:49 PM
  5. my menu doesnt work can u tell me whats wrong
    By claymore in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 8th, 2010, 04:16 AM