6 Replies - 583 Views - Last Post: 09 December 2016 - 12:54 PM Rate Topic: -----

#1 King Hebes   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 29-January 16

Images won't appear in program

Posted 08 December 2016 - 08:47 PM

So I've copied this program directly from my textbook as one of my tasks is to modify it with certain conditions. However, the program will not work properly even when copied directly from the textbook. None of the images are loading into the program. I've tried copying them to various locations and nothing. I had a friend run some tests also and the images are being loaded, but aren't displaying. It appears to him the paint component method is not being called. He doesn't know about GUI's though so couldn't tell me what is wrong. Perhaps you guys can help here?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DirectionPanel extends JPanel
{
   private final int WIDTH = 1000, HEIGHT = 1000;
   private final int JUMP = 10; //increment for image movement
   
   private final int IMAGE_SIZE = 31;
   
   private ImageIcon up, down, left, right, currentImage;
   private int x, y;
   
   //Constructor: Sets up this panel and loads the images.
   
   public DirectionPanel()
   {
      addKeyListener (new DirectionListener());
      
      x = WIDTH / 2;
      y = HEIGHT /2;
      
      up = new ImageIcon("up.png");
      down = new ImageIcon("down.png");
      left = new ImageIcon("left.png");
      right = new ImageIcon("right.png");
      
      currentImage = right;
      
      setBackground(Color.black);
      setPreferredSize(new Dimension(WIDTH, HEIGHT));
      setFocusable(true);
   }
   
   //Draws the image in the current location.
   
   public void paintcomponent(Graphics page)
   {
      super.paintComponent(page);
      currentImage.paintIcon(this, page, x, y);
   }
   
   //Represents the listener for keyboard activity.
   
   private class DirectionListener implements KeyListener
   {
      //responds to the user pressing arrow keys by adjusting the image and image location accordingly.
      
      public void keyPressed(KeyEvent event)
      {
         switch (event.getKeyCode())
         {
            case KeyEvent.VK_UP:
               currentImage = up;
               y -= JUMP;
               break;
            case KeyEvent.VK_DOWN:
               currentImage = down;
               y += JUMP;
               break;
            case KeyEvent.VK_LEFT:
               currentImage = left;
               x -= JUMP;
               break;
            case KeyEvent.VK_RIGHT:
               currentImage = right;
               x += JUMP;
               break;
         }
         
         repaint();
      }
      
      //Provide empty definitions for the unused event methods.
      public void keyTyped(KeyEvent event) {}
      public void keyReleased(KeyEvent event) {}
   }
}



Here is the code for the program to launch.

import javax.swing.JFrame;
import java.awt.*;

public class Direction
{
   //Creates and displays the application frame.
   
   public static void main(String[] args)
   {
      JFrame frame = new JFrame("Direction");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
      frame.getContentPane().add(new DirectionPanel());
      
      frame.pack();
      frame.setVisible(true);
   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Images won't appear in program

#2 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3541
  • View blog
  • Posts: 16,143
  • Joined: 20-September 08

Re: Images won't appear in program

Posted 09 December 2016 - 04:02 AM

Always use

@Override


before overridden methods. Tell me what you see when you do that
Was This Post Helpful? 0
  • +
  • -

#3 King Hebes   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 29-January 16

Re: Images won't appear in program

Posted 09 December 2016 - 11:36 AM

I have not been taught about overridden methods yet. Where would that go?
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3541
  • View blog
  • Posts: 16,143
  • Joined: 20-September 08

Re: Images won't appear in program

Posted 09 December 2016 - 12:07 PM

Just the line before the method declaration
Was This Post Helpful? 0
  • +
  • -

#5 King Hebes   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 29-January 16

Re: Images won't appear in program

Posted 09 December 2016 - 12:40 PM

View Postg00se, on 09 December 2016 - 12:07 PM, said:

Just the line before the method declaration


I'm guessing you mean the line before the paintComponent method?

Here's the error:

DirectionPanel.java:38: error: method does not override or implement a method from a supertype
@Override
^
1 error

@Override
   public void paintcomponent(Graphics page)
   {
      super.paintComponent(page);
      currentImage.paintIcon(this, page, x, y);
   }


Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3541
  • View blog
  • Posts: 16,143
  • Joined: 20-September 08

Re: Images won't appear in program

Posted 09 December 2016 - 12:46 PM

That's right. So know you need to ask yourself the reason for the error message. Hint: Java is case-sensitive

This post has been edited by g00se: 09 December 2016 - 12:46 PM
Reason for edit:: typo

Was This Post Helpful? 1
  • +
  • -

#7 King Hebes   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 29-January 16

Re: Images won't appear in program

Posted 09 December 2016 - 12:54 PM

Oh I feel stupid now lol... I looked over this code for so long double checking to see if I missed anything.... paintComponent..... Well thank you! My friend will probably slap himself as well.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1