Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default Class in simple game problem

    I've been reading "Java 6 for Dummies" and I made a pong applet by following the book, and now I'm trying to make a mario-like game with a robot icon as the character. Using the pong code as a guide. I have two errors with the code here... my error is that the PaintSurface class "isn't abstract and doesn't override abstract method 'action performed'"
    I didn't think I had an "action performed" class... I'm pretty new to this, I've been taking a computer math course this year, and we have to make a end of year project so I started this early haha.

    One more thing... how would I make the object because the shell code was for a paddle and ball. I took away the ball and just want the "paddle".. which will be my robot. The Robot object was made by my teacher, and I put that in for now, but I'm pretty sure that won't work, or I'm not doing right.


    Thanks in advance!




    Java Code:
    class PaintSurface extends JComponent implements ActionListener, KeyListener
    {//
    	int xpos = 0;
    	int ypos = 0;
    	int bulletspeed = 0;
    	int height = Karelxxx.HEIGHT;
    	int width = Karelxxx.WIDTH; 
    	int score = 0;
    	
    	Icon b = new ImageIcon(getClass().getResource("kareln.gif"));
    	
    	public PaintSurface()
    	{
    		addKeyListener(new KeyListener()
    		{
    			public void keyPressed(KeyEvent e)
    			{
    				int code = e.getKeyCode();
    				if( code == KeyEvent.VK_UP)
    				{
    					jump();	
    				}
    				if( code == KeyEvent.VK_DOWN)
    				{
    					
    				}
    				if( code == KeyEvent.VK_LEFT)
    				{
    					moveLeft();
    				}
    				if( code == KeyEvent.VK_RIGHT)
    				{
    					moveRight();
    				}
    			}
    		});
    	}
    		public void jump()
    		{
    			ypos = -1;
    			ypos = Karelxxx.GROUND;
    		}
    		public void moveLeft()
    		{
    			xpos = xpos - 1;
    		}
    		public void moveRight()
    		{
    			xpos = xpos + 1;
    		}
    
    		public void keyTyped(KeyEvent e){}
    		public void keyReleased(KeyEvent e){}		
    	
    	public void paint(Graphics g)
    	{
    		Graphics2D g2 = (Graphics2D)g;
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    		Robot Karel = new Robot(xpos, ypos, 60, 8);
    		g2.drawString("Score: " +score, 250, 20);
    	}
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your class implements ActionListener, an action listener interface demands that you have the following method
    Java Code:
    public void actionPerformed(ActionEvent e)
    Your code doesn't have this method so the compiler is complaining. You can either add the method and not do anything in the body, or you can make it so the class does not implement ActionListener.

    You probably don't need to implement keylistener either. You added a keylistener with an anonymous inner class.

    Here is an example of what you do when you implement something
    Java Code:
    public class X implements ActionListener{
      private JButton b;
      private JLabel hi;
      private int count = 1;
      public X(){
        b = new JButton("Click me");
        JFrame frame = new JFrame();
        b.addActionListener(this);
        hi = new JLabel("HI");
        JPanel p = new JPanel(new BorderLayout());
        p.add(hi, BorderLayout.CENTER);
        p.add(b, BorderLayout.NORTH);
        frame.add(p);
        frame.setSize(100, 100);
        frame.setVisible(true);
      }
      public void actionPerformed(ActionEvent e){
        if(e.getSource() == b){
          hi.setText("B CLICKED: " + count++ + " TIMES");
        }
      }
    }
    This can be done differently with an anonymous inner class
    Java Code:
    public class X{
      private JLabel hi;
      private JButton b;
      private int count = 1;
      public X(){
        JFrame frame = new JFrame();
        JPanel p = new JPanel(new BorderLayout());
        b = new JButton("Click me");
        b.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e){
            hi.setText("B CLICKED : " + count++ + " TIMES");
          }
        });
        hi = new JLabel("HI");
        p.add(b, BorderLayout.NORTH);
        p.add(hi, BorderLayout.CENTER);
        frame.add(p);
        frame.setSize(100, 100);
        frame.setVisible(true);
      }
    }
    I hope you see the difference, and realize how you sort of implemented both approaches halfway.
    Last edited by sunde887; 04-19-2011 at 02:35 AM.

Similar Threads

  1. Simple Monopoly Game
    By AtoN in forum Java Gaming
    Replies: 2
    Last Post: 01-24-2011, 02:31 PM
  2. Need help with simple game! *collision*
    By CNew in forum New To Java
    Replies: 1
    Last Post: 12-05-2010, 12:09 AM
  3. need help with a simple game im making
    By frallan in forum AWT / Swing
    Replies: 2
    Last Post: 11-30-2009, 10:36 AM
  4. Simple Game example
    By mayuresh34 in forum CLDC and MIDP
    Replies: 1
    Last Post: 10-08-2009, 01:21 PM
  5. Simple Dodging Game
    By Tb0h in forum New To Java
    Replies: 1
    Last Post: 07-18-2009, 02:30 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
  •