Results 1 to 6 of 6
- 03-12-2009, 04:21 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 45
- Rep Power
- 0
[new] need help on GUI adding images
hi all,
well im just confused on how i would create an object from one class and display it on another class.
For example on my main class i have created a frame which just has a blue background.
I have an crab class where i want to create like crab objects.
What im stuck on is how to i give a image of a crab ive got to the objects that i made? Would i somehow in the crab constructors give the image? or does the image not go in the constructors?
Also how when the crab objects have an image would i display it on the frame i create on the main class?
thanks everyone for your time
- 03-16-2009, 01:39 PM #2
I hate to respond this way, but how much Java experience do you have?
Here's the short answer.
Create an ImageIcon, add it to a JLabel, and set the properties of the JLabel appropriately to cause the image to display the way you want.
In your JFrame, start by adding a JPanel that fills the entire ContentPane (use GridLayout with one cell). This is a good practice for *all* GUI's. Then add the JLabel to the JPanel.
- 03-16-2009, 08:25 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 45
- Rep Power
- 0
Thank you for your reply. I have started learning java for about 6 months, so i guess im pretty much a begginner.
Ive looked on the interent aswell for different ways but some just seeem comfusing, ill have a look on the internet for methods to implement your way.
- 03-16-2009, 10:16 PM #4
Sun's Java Tutorial is the best place to start...
GUI programming in Java is powerful, but it is also complicated at first. There is a right way to do things, and not following it will lead to all kinds of problems. You want to read the "Swing" section.
- 03-19-2009, 07:22 AM #5
Member
- Join Date
- Mar 2009
- Posts
- 45
- Rep Power
- 0
I have looked at turtorials regarding swing.
I have now created a class which extends JFrame which has a constructor which creates an JFrame and creates a JPanel
(This is what creats the panel) // JPanel panel1 = new JPanel();
adds the panel to the frame, // add(panel1);
creates a imageicon of the crab image1 // ImageIcon image1 = new ImageIcon("crab.jpg");
creates the JLabel and it holds the crab image // JLabel label1 = new JLabel(image1);
and then adds the label to the panel //panel1.add(label1);
then sets it to show // setVisible(true);
in the main method of the program i will call this constructor and it willl create the frame and the image of one crab.
This works and shows the picture, however what im confused on is what is the way to create more crabs and show them on the JPanel? For example say i want to create four crabs from a crab class i have, i want to create four crab objects which show on the JPanel so when the program is run four crab images are shown.Last edited by counterfox; 03-19-2009 at 07:24 AM.
- 03-19-2009, 10:29 AM #6
Similar Threads
-
XML Images
By JavaWizz in forum XMLReplies: 1Last Post: 10-17-2008, 10:19 AM -
images
By amith in forum AWT / SwingReplies: 3Last Post: 06-27-2008, 08:38 PM -
images
By amith in forum AWT / SwingReplies: 1Last Post: 05-20-2008, 10:54 AM -
Help with images...
By toby in forum Java AppletsReplies: 1Last Post: 08-04-2007, 05:25 AM -
Images in JSP
By Daniel in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06-05-2007, 06:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks