Results 1 to 3 of 3
- 03-30-2011, 10:47 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 1
- Rep Power
- 0
first time GUI'r... trying to reference jpg
this is my first time trying to get an image onto a JFrame... not sure where exactly to save my jpg to in my source folder, and how exactly to reference it in my ImageIcon constructor in order to get to it... any help?
the package7/arnold-deadlift.jpg is my attempt to get to a picture called "arnold... .jpg" which is in package 7, in my src folder, in my java project... if that helps at all on what that folder hierarchy looks like
import javax.swing.*;
import java.awt.*;
public class Gui{
private Image icon;
public static void main(String[]args){
Gui gui = new Gui();
gui.run();
}
public void run(){
JFrame f = new JFrame();
f.setSize(600,600);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
NewPanel panel = new NewPanel();
f.add(panel);
}
public class NewPanel extends JPanel{
public void paintComponent(Graphics g){
icon = new ImageIcon("package7/arnold-deadlift.jpg").getImage();
g.drawImage(icon,0,0,this);
}
}
}
- 03-31-2011, 12:32 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
- 04-08-2011, 05:33 PM #3
First: use code-tags to make your code readable.
Supposing you use Eclipse and ant:
Where you save your jpg isn't that important.
- If you place it under your src-directory it is copied by Eclipse to your bin-tree when building the project.
- You can also place it in a parallel directory to src and bin, recources for example. Then you must remember to reference it in your ant-script when making a jar.
You can use a small diagnostic method like
(Remove the "static" if you don't use it from a lib but from the class itself)Java Code:public static void testFile( String filePathAndName){ File file =new File( filePathAndName); String path = file.getAbsolutePath(); System.out.println( "File: " + filePathAndName + "\n" + "AbsPath: " + path ); }
If you make an ImageIcon (or some other resource) you can call this with the same path to see where the program is looking for it.
Similar Threads
-
JAVA Programmers Wanted - Full-Time and Part-Time Positions open
By javatrek2020 in forum Jobs OfferedReplies: 3Last Post: 08-23-2011, 12:46 PM -
reference issue
By Stephen Douglas in forum New To JavaReplies: 29Last Post: 07-04-2010, 05:12 PM -
calculate time diff for particular time period
By baktha.thalapathy in forum New To JavaReplies: 2Last Post: 05-24-2010, 04:10 PM -
Class Time - represents time of day
By verbazon in forum New To JavaReplies: 1Last Post: 04-13-2009, 01:06 AM -
creating a reference to a value in a map
By kellykellykelly in forum Advanced JavaReplies: 1Last Post: 02-28-2009, 07:59 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks