Trying to import my own packages

package mainClasses;
    /*
     * Frame Info and all that ****,
     * mainFrame is the actual frame itself
     * it will refer to MainC.java a lot Main class = Main Class
     */
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import JavaGame.src.resources.*; //Problem Code
    import javax.swing.JButton;
    import javax.swing.JFrame;


    public class mainFrame extends JFrame {


private static final long serialVersionUID = 1L;

public mainFrame() {
    JButton playButton = new JButton();
    JButton infoButton = new JButton();
    JButton exitButton = new JButton();
    int x = 300, y = 300;
    setSize(x, y);
    setVisible(true);
    setLayout(new FlowLayout());
    setTitle("Kingdom Raider");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    /*Buttons and Properties*/

     playButton.setBounds(x, y, 200, 100);
     playButton.setText("Play!");
    add(playButton);

     infoButton.setBounds(x, y, 200, 100);
     infoButton.setText("Information");
    add(infoButton);

     exitButton.setBounds(x, y, 200, 100);
     exitButton.setText("Exit");
    add(exitButton);
            /* Add image here */

}

public void Painting (Graphics g) {
    //Me.
}
   }

I'm creating a game and I'm having an import problem. As you can see I want to import JavaGame.src.resources, as I'm trying to import an img. Here's how my Directory stands:

enter image description here

I don't need to know the code on resourcesmanager.java its blank at the moment. So basically, this class here is in packages mainClasses, but i want to access the resources package. What gives?

Upvotes: 5

Views: 22491

Answers (6)

yuyu5
yuyu5

Reputation: 474

What you want to do is access the files stored in your resources folder. To do this, you don't import the folder as you tried to do. Instead, do what Tony the Pony advised and use

getResource("FileName.ext")

As an example, to turn a file into an icon, you'll have to use the resource path:

java.net.URL path = this.getClass().getResource("FileName.jpg");
ImageIcon icon = new ImageIcon(path);

I should also point out that if you're using NetBeans, you need to add the resource folder as a source folder; otherwise the project won't compile how you want it to, resulting in a .jar file that can't access the resource files. Eclipse might be similar. To add the folder as a source:

  1. Right click on project
  2. Select properties
  3. Click "Sources" in categories pane
  4. Click "Add Folder" and select the resource folder you made

Upvotes: 0

TechSpellBound
TechSpellBound

Reputation: 2555

I think this could be a problem with Eclipse configuration. Sometimes Eclipse considers the source folder in the project as part of the package. Just delete the project from the Eclipse workspace without deleting it from the file system and import it again.

That should help.

Deleting project from eclipse without deleting it from the file system:

http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2Ftasks%2Ftasks-42b.htm

Importing existing project into eclipse:

http://agile.csc.ncsu.edu/SEMaterials/tutorials/import_export/

Upvotes: 0

Adam Miller
Adam Miller

Reputation: 1783

Eclipse will automatically import stuff for you if you copy and paste something-maybe you could write a short clip that uses something in JavaGame.src.resources, and then copy paste that-eclipse will do the rest.

Upvotes: 1

Tony the Pony
Tony the Pony

Reputation: 41457

Resources aren't imported like packages. Have a look here: http://docs.oracle.com/javase/1.5.0/docs/guide/lang/resources.html.

Specifically, here's an example how to load images from resources: http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource.

Upvotes: 1

maksimov
maksimov

Reputation: 5811

Your source folder is src, so this is the root of your class hierarchy. You should do

import resources.*

However using * is bad form and you should try and import only classes that you need in this particular class, like you've done with javax.swing.JButton for example. So:

import resources.ResourceManager;

Upvotes: 1

Lukas Eder
Lukas Eder

Reputation: 221380

Your package name is resources, so write this:

import resources.*; // No-Problem Code

The remaining parts of the directory structure is specific to Eclipse and doesn't have anything to do with Java classpaths

Upvotes: 5

Related Questions