How to add icon into Java (NetBeans) from Mac Os X? - java

So I'm trying to add an icon into my program, but the textbook I'm reading explain how to only for Windows users. I would like to know how to add the icon. I have it on my program source folder and the code I have so far is something like this:
logo = new ImageIcon("~://resources//CherryBoom.png");
labelone = new JLabel("Fruit No.1 : ", logo, SwingConstants.LEFT);
JPanel panelone = new JPanel();
panelone.add(labelone, logo);
The icon still won't show on the windows panel, so I'm really lost here, and I don't know how can I get it to show into my program.

First of all, check the obvious solutions such as:
Have you done window.add(panelone);
Is the file in the correct spot/url is correct
Secondly, if you hate LayoutManagers like me, but still want to use javax.swing, you might try using drawString and drawImage methods in your panel's paintComponent(Graphics g) class. In detail:
You'll need to make your own JPanel:
public class MyPanel extends JPanel {
as well as override the method:
#Override
public void paintComponent(Graphics g) {
within the method, call this so the window can refresh itself and do other housekeeping things:
super.paintComponent(g);
then, use drawString and drawImage to draw these images in the place you would like them:
g.drawString("Fruit No. 1", x, y);
logo.paintIcon(this, g, x, y);
Whenever you change or draw an image, you'll also want to call in the main method:
panelone.repaint();
Hope this helps!

Java doesn't support expanding the "~" path directive.
Try this...
try {
File file = new File("~");
System.out.println(file.getAbsolutePath());
System.out.println(file.getCanonicalPath());
} catch (IOException exp) {
exp.printStackTrace();
}
I think you will find that it doesn't point to the users home folder.
Instead, you should be using System.getProperty("user.home")
logo = new ImageIcon(System.getProperty("user.home") + File.separator + "/resources/CherryBoom.png");
Now, having said that, I would strongly encourage you to use ImageIO over ImageIcon as you will get better feedback when something goes wrong.
Check out Reading/Loading an Image

Related

Loading an image from folder in project

I am currently working on a text-based RPG with a GUI and I can't seem to get the image I want to use to load.
class BackgroundPanel extends Panel
{
// The Image to store the background image in.
Image img;
public BackgroundPanel(String location)
{
// Loads the background image and stores in img object.
img = Toolkit.getDefaultToolkit().createImage(location);
}
public void paintComponent(Graphics g)
{
// Draws the img to the BackgroundPanel.
g.drawImage(img, 0, 0, null);
img.setAccelerationPriority(SCALE_FAST);
}
}
This is the code I use for the panel itself. I tried putting the image file in the root directory of my project but it doesn't seem to help. I have created a folder within the project which I intend to use for all of my images.
I'm not sure what the issue is here. I know I tried using paint() instead of paintComponent(), but then the buttons and other components won't draw until you mouse over them, for some reason.
Any ideas?
I have already posted it at below links in the same context. Please have a look:
ImageIcon does not work with me
How to retrieve image from project folder?
Read image from another directory
It's already described in Oracle Java Tutorial on Loading Images Using getResource
Note: Don't forget to call super.paintComponent() in the overridden paintComponent() method.

Matisse properly setting image to panel

I got a problem with NetBeans resource managing while setting images to pannel:
This is my not working code:
try {
BufferedImage myPicture = ImageIO.read(new File("images/3D.jpg"));
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
pnlMain.add(picLabel); //the main and only pannel made by matisse is called pnlMain
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Cannot set image");
}
The folder called "images" is in the MAIN project folder. There are several folders: build, nbproject, src and "images".
The problem I have is that the program runs but it doesnt set the image...
Someone suggested me to make another class in different package with this code:
public class PanelImage extends JPanel{
private Image imag;
public PanelImage(Image img){
if(imagen != null){
this.imagen = img;
}
}
#Override
public void paint(Graphics g){
g.drawImage(img, 0,0, getWidth(), getHeight(), this);
setOpaque(false);
super.paint(g);
}
}
But i cant find a proper way of implementing it...
For your ImagePanel class
super.paint[Component] before all the other stuff.
Don't override paint but instead paintComponent
Don't set properties in paintComponent method ie setOpaque(). Beside, JPanel is opaque by default
Override getPreferredSize() for painting on panels
For loadng images
Make a habit of not reading images from the file system, unless the application is specific to only your machine.
Instead read from the class path and make the image a resource by packaging it into the class path
Change your file structure
ProjectRoot
src
images
3D.jpg
Read from class path. Use ImageIO to make sure your path is correct. If it's invalid, an exception will be thrown
URL url = getClass().getResource("/images/3D.jpg");
Image image = ImageIO.read(url);
For Netbeans GUI Builder
You can set the label icon using the design tool
Select your label from the navigator or the design view.
Go to the properties window in the right and find the property icon
click the ellipses button to the right of the property and a dialog will appear.
Find your image and select OK (make sure your image is in a package in the src)
See related and maybe related

Making a custom icon for a JFrame

Well I was wondering if I could make an icon image for a JFrame. I do know its posible, because, let me say, I am NOT digging the java logo.
Well if I just hava to use a Frame object I will.
Can someone tell me, I know its possible!
Use an ImageIcon.
ImageIcon icon = new ImageIcon( pathToIcon );
yourFrame.setIconImage(icon.getImage());
Good Luck!
First, you have to have an image file on your computer. It can be named anything. For this example, we will call this one "pic.jpg".
Next, you need to include it in the files that your application is using. For example, if you're using NetBeans, you simply click on "Files" in the left hand side of the IDE (not File as in the menu, mind you). Drag the picture's file over to the folder that houses the main package. This will include it for available use in the code.
Inside the method where you define the JFrame, you can create an image like this:
Image frameImage = new ImageIcon("pic.jpg").getImage();
You can now set it as the IconImage for the frame like this:
JFrame frame = new JFrame("Title");
frame.setIconImage(frameImage);
Hope that's helpful.
Note: the reason that the Image object has to be created like this is because Image is abstract and cannot be instantiated by saying new Image();
Props to you, btw, kid. I wish I would have started learning programming when I was your age. Keep at it!
You can do the following.
public Test {
public static void main(String[] args) {
JFrame frame = new JFrame("My Frame");
frame.setIconImage(new ImageIcon(Test.class.getResource("image.png"));
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setVisible(true);
frame.setSize(100, 100);
//other stuffs....
}
}

Set icon in JFrame

I want to change the icon of the project instead of java icon. When the program icon is being displayed in status bar, it should be displaying the customized icon instead of default java icon.
I am using the following code. Please suggest me what's wrong in this code.
class newframe extends JFrame
{
Container cp;
newframe()
{
cp=this.getContentPane();
cp.setLayout(null);
}
public static void main(String args[])
{
newframe frm= new newframe();
frm.setbounds(0,0,1000,800);
frm.setVisible(true);
ImageIcon im1= new ImageIcon("path upto image");
frm.setIconImage(im1.getImage());
}
}
..new ImageIcon("path upto image");
A frame icon will typically be an embedded-resource, so must be accessed by URL rather than (a String representing a path to) a File.
There are a couple of things that would be keeping it from compiling. First:
frm.setbounds(0,0,1000,800);
Your "setbounds" should have a capital B. Typically, functions will be cased such that the first letter of the first word is lowercased, and subsequent words are upper-cased. See this link for the doc on setBounds: setBounds
There's a second issue in your ImageIcon path. Its hard to say if that came right from your code or if you removed the path for the sake of the example, but Andrew Thompson has addressed that adequately.
I think the problem is the decleration of the imageicon. What you should do is instead of getting the direct path, do something like this:
ImageIcon im1= new ImageIcon("Toolkit.getDefaultToolkit().
getImage(getClass().getResource("path upto image"))");
I do this with all of my applications, and it works every time.

Java repaint not displaying component correctly

im developing an GUI Application, using JSWing, i load XML file, then deserialize it, then i add all created object to the JPanel.
However, until i move the window, or click on the panel, this is how they looks like
After i move the window, they look correctly, so how to fix this issue <
I looked at this link
http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#paintComponent(java.awt.Graphics)
and it might be the answer, since in the constructor of the JComponent i use
setOpaque(true);
but im still not sure how to fix the issue since that part of documentation is very hard to understand (it somehow just does not make any sense to me :-D )
by the way, the painting itselfs goes something like this
for (NetObject o : objects) {
addNewObject(o);
}
and addNewObject (not whole code)
public void addNewObject(NetObject o) {
DraggableComponent object = new DraggableComponent(o, editorIndex); //Creates a draggableComponent
this.add(object);//Adds this component to main container
object.setOverbearing(true); //On click ,this panel gains lowest z-buffer
object.setSize(48, 48);
object.setLocation(o.x - 23, o.y - 23);
object.setBackground(Color.WHITE);
this.repaint(); //this = JPanel
}
and the overriden paintComponent code
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (isOpaque()) {
if (object.type == 2) { //tarnsition
g.drawImage(transition, 0, 0, this);
} else if (object.type == 1) {
boolean test = g.drawImage(place, 0, 0, this); //place
g.drawString(object.loadTokens(), 3, 27); // 1,2,3,4...
}
}
}
i tried to call this.revalidate - after FOR EACH LOOP - didnt help, the only way that works is to move somehow with the window, strangely, this problem exists only # Windows, my collegue is developing this exact same application under Linux, and he does not experience ani graphical issues.
I know that there been an awfully lot of topics like this, but i honestly was not able to figure out the solution.
Thanks for the answer,
OSiRiS
The setBackground() API mentions that "It is up to the look and feel to honor this property, some may choose to ignore it." Set the graphics context's color explicitly in paintComponent() and invoke fillRect().

Categories

Resources