Link to home
Start Free TrialLog in
Avatar of Chad_T
Chad_T

asked on

Java GUI, cannot load image- I am very confused!

To whom this may concern:

For several weeks, I have been attempting to write a Java program that uses an image as the GUI, with 6 spots on the image going to different pieces of code.  4 of the spots are Java coded calculators, and 2 of them go to pdf manuals.

I started using a text editor, and had success with the calculator functionality.  The GUI was really difficult for me, so I moved to NetBeans to try to implement this.  

At this point, NetBeans "cannot find symbol" getCodeBase(), and I have imported about every library I can think of.  Point being, I have code everywhere, and am totally confused as to how to get the GUI working, and to get my different calculators and manuals to all "talk" to each other.

Should I go back to my text editor?  Should I continue working NetBeans?  I am completely confused, and my deadline is very, very soon.  

Can anybody offer advise, or possibly even what the base code would look like?  I thought that I knew how to program before I took this on, and boy have I been schooled!

Any help is very, very much appreciated.

Thank you,
Chad T
Avatar of suprapto45
suprapto45
Flag of Singapore image

Hi Chad T,

It is true that it is a little frustated learning Java GUI for the first time but when you are used to do that, you should have no problem anymore.

What you need to know regarding the GUI is that the positioning. Please search yahoo based on Layout. There are many Layout in Java such as FlowLayout which default your GUI from left to right, BorderLayout which divides your screen into NORTH, WEST, CENTER and etc. BoxLayou, GridLayout and etc.

You may also search yahoo or google on the Panel or JPanel if you use Swing. The panel can contain many components of your GUI such as JBUtton, JTextField and etc.

Then you need to know more about Listener. For the JButton, JTextField, they all have the listeners to configure what they need to execute if they are pressed or invoked.

By learning these three components, I believe that you should have some clear and obvious idea. Please search on JavaAlmanac.com and etc.

My suggestion...just stick to NetBeans or you may post your prob here.

Regards
Dave
Avatar of Chad_T
Chad_T

ASKER

Hello Dave,
 
Thank you for your fast reply.  I actually have already schooled myself on the items that you mention above.  As for the Centering, North, South, JButton, and JTextFields, I have a pretty decent understanding.  Those are actually items that I am using in the calculators that I have already completed.  

I actually have a functional test GUI, where I am using the TabbedPanes.  My biggest point of confusion is how use an actual JPG image as my GUI, and to specify 4 sections of my JPG to execute my calculator code blocks, and to specify the other 2 sections to open a pdf file.

Does this help, in terms of specifics?  I can post what I have if it would help.  I figured my code was garbled to the point where it was pretty unhelpful, though.  Let me know....

Thank you,
Chad T
Avatar of CEHJ
getCodeBase() is an applet method. Do you have an Applet  (or JApplet)? If not, you should probably do something like

Image img = new ImageIcon(getClass().getResource("/x.jpg")).getImage();
>> If not,

and you can call that if it *is* too
Avatar of Chad_T

ASKER

Hello CEHJ,
  You are correct, I am not doing an Applet.  This must be the issue.  I tried your line of code, and it does compile.... and works very well, I might add.  Now that I am able to read the image, how can I display it as my GUI?  I need to convert to a different type than TabbedPane I am pretty sure, but do not know what that would be.... below is the current "main" that I am using:

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

public class Calculators extends JFrame {
  private JLabel        statusBar;
  private JTabbedPane   tabbedPane;

  public static void main(String[] args) {
    Calculators that = new Calculators();
    that.setVisible(true);
  }

  public Calculators() {
    super("Money");
    setSize(700, 550);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    statusBar = new JLabel(" ");
    getContentPane().add(statusBar, BorderLayout.SOUTH);

    tabbedPane = new JTabbedPane();

    populateTabbedPane();
    getContentPane().add(tabbedPane, BorderLayout.CENTER);
  }

  private void populateTabbedPane() {

    Image img = new ImageIcon(getClass().getResource("money.JPG")).getImage();
   
 }
----

Thank you very much,
Chad T
ASKER CERTIFIED SOLUTION
Avatar of Javatm
Javatm
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Chad_T

ASKER

By the way, not sure when it is appropriate to click "accept," and when to provide "feedback."  First time using the forum, so please let me know what is customary, so that I can make sure grading and points are allocated properly.

Thanks,
Chad T
Feed back : is if you want to say thank you for the things an expert did.
Accept : means the expert gets all the points.
Split Points : at the buttom of the page splits the points on all expert.
You can use Feedback and Accept or Feedback and Split Just remember to do
Feedback 1st since if you pick Accept or Split you will not be able to do a Feedback :)
>>.  Now that I am able to read the image, how can I display it as my GUI?  

Since you're putting the image in a GUI, you may as well use the ImageIcon i mentioned as Javatm says
Avatar of Chad_T

ASKER

Javatm,
  Your code both solves the read and viewing problem that I am having!  Thank you, thank you, thank you, thank you!  Also, thank you to CEHJ for assisting, as well as Dave from the beginning of this thread.
  I do not want to push my luck here.... but now that I have my image as my GUI, is there some kind of mouse clicking procedure, or do I need to establish some coordinates so that a particular section of my image takes the user to a particular java calculator / pdf file?

Very warm regards,
Chad T
 
 
You can add a MouseListener to the ImageIcon
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well you got to set your components 1st, align them in yout frame and add some listeners to your components.
http://java.sun.com/docs/books/tutorial/uiswing/events/

If you have another question that should be in another thread okey, I could use some feedback =) since I
only got one, haha. Thanks for your time dude.
Here are the list of all the listeners to all components :
http://java.sun.com/docs/books/tutorial/uiswing/events/eventsandcomponents.html
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
8-)
http://freewebs.com/cube-j

Always glad to help . . .
Friend : Javatm
Thanks for the feedback bro =)