Is there any way to easily delete an ImageIcon completely from the screen? I can't find one anywhere.
ImageIcon image = new ImageIcon("candle.gif");
image.paintIcon(this, g, 150, 80)
For example, if I wanted to get rid of "image" later on when a button is pressed, what would be the appropriate code? (Not for the button, I know how to do that).
Don't reinvent the wheel. The easiest way to paint an Icon is to use a JLabel. The code would be something like:
ImageIcon icon = new ImageIcon(...);
JLabel label = new JLabel( icon );
panel.add( label );
If you don't want to display the icon anymore then in the ActionListener of your button you simple do:
label.setIcon( null );
Related
Is there a way to easily set the size of an icon for a JTabbedPanetab.
The icon is the same size as the original image and therefore makes for a very ridiculous looking tab with it taking up half the screen.
How would I easily rescale this icon to be a similar size to my text "Settings" and just have it appear normally?
URL cogIconUrl = getClass().getResource("/images/cog.png");
ImageIcon cogImg = new ImageIcon(cogIconUrl);
JTabbedPane jtp = new JTabbedPane();
getContentPane().add(jtp);
JPanel jp1 = new JPanel();
jtp.addTab("Settings", cogImg,jp1);
Furthermore, how would you go about setting the font of the tab text? I assume the two are potentially related.
Resize the Image:
BufferedImage image = ImageIO.read(...);
Image scaled = image.getScaledInstance(...);
Icon icon = new ImageIcon( scaled );
Note: the getScaledInstance(...) method may not be the best approach as it is slow, but for a single image you won't notice any problems.
You can read Perils of Image.getScaledImage for more information.
This question already has an answer here:
Not able to add image in jscrollpane inside a jframe
(1 answer)
Closed 5 years ago.
I'm making a jframe where i'm adding an image that will change as i click next.I have written a code but it doesn't work.Here is the code:
i++;
ImageIcon icon = new ImageIcon(mean.get(0));
Image image = icon.getImage(); // transform it
Image newimg = image.getScaledInstance(180, 140, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
icon = new ImageIcon(newimg);
JLabel label = new JLabel( icon );
jScrollPane1= new JScrollPane( label );
Please help
The problem lies with:
jScrollPane1= new JScrollPane( label );
The field jScrollPane whose object was placed in the JFrame is set to another JScrollPane, whose object is not added in the GUI.
Store the original label in a field, say jLabel1 and set that label.
A repaint might be needed.
You need to call:
revalidate()
repaint()
See an explanation of why here Java Swing revalidate() vs repaint()
Add this to end of your code jScrollPane1. If haven't JFrame created already create one like below then add that jScrollPane1 to JFrame.
JFrame frame = new JFrame();
frame.add(jScrollPane1, BorderLayout.CENTER);
There is also set the layout as BorderLayout but you can try other layouts or without adding layout also.
I have small problem with changing JFrame background image. First I've added background image with JLabel and application is working good. But now I need to change it dynamically.
I've tried this code :
label = new JLabel(new ImageIcon(Toolkit.getDefaultToo... // old background image
public void changeImage(){
label.setVisible(false);
label2 = new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("weatherall.gif"))));
setContentPane(label2); // new Background image
label2.setVisible(true);
repaint();
}
switch (cmb.getSelectedItem().toString()) {
case "ISTANBUL":
x = 0;
changeImage();
//some codes......vs.vs.
break;
Also I'v tried it with timer (TimerTask) every 1 sec. Refreshing frame
Anybody have an idea about this?
Now we need to create JLabel and set it size like background(stretch it) after that just add image into the JLabel and when u want to change it just change the image from same JLabel don't try adding another JLabel,it's not working!!...thats it.
JLabel label = new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource(localweather))));
setContentPane(label);//when u want to change background image just replace 'localweather' another image.
I have the following code:
try {
File file_background = new File(
"C:\\Users\\xxxx\\Desktop\\background.png");
ImageIcon icon_background = new ImageIcon(
ImageIO.read(file_background));
JLabel background = new JLabel(icon_background);
window.setContentPane(background);
File file_car = new File(
"C:\\Users\\xxxxxx\\Desktop\\car.png");
ImageIcon icon_car = new ImageIcon(ImageIO.read(file_car));
JLabel car = new JLabel(icon_car);
car.setVisible(true);
background.add(car);
// TODO Get car showing on top of the background label
} catch (IOException e) {
e.printStackTrace();
}
Where I'm attempting to have the car label show on TOP of the background label. But I'm only getting the background JLabel showing. I'm new to SWING so any suggestions to what steps I'm missing would be great.
..I want to move it a later stage. But right now I want it to show first :)
There are two ways,
1st.
Put JLabel car to JPanel, drawing an Image by using paintComponent, instead of JLabel background (advantage JPanel is container with proper notifications for LayoutManager).
Put JLabel car to JLabel background, but JLabel haven't implemented any LayoutManager, have to set desired.
Advantage all images in JLabel are static, with zero CPU and GPU inpact ad consumption in compare with paintComponent.
Disadvantage JLabel isn't container and with proper notifications for LayoutManager, required a few code lones moreover in compare with JLabel placed in JPanel, for movement (AbsoluteLayout) is quite good solution.
2nd.
Draw both Images by using BufferedImage and Graphics.
Add them both to a JPanel that uses OverlayLayout. It is not ok to add a JLabel to another JLabel.
This code has not gone through a compiler so take it for what it is :)
JPanel panel = new JPanel(new OverlayLayout());
panel.add(background);
panel.add(car);
Works.. Added the following to make it display:
car.setBounds(200, 200, 200, 200);
Apparently it's because by default a null layout manager is used. So setting the bounds of the label will enable it to display since the default size is 0.
I have created a label and added an image to it using the seticon property.
Now I want to print that image but I am unable to do so.
I tried with .print(Graphics g) method but I dont know what is that graphics??
If someone can help me in printing the label, I would be really grateful to you.
Thanks
You don't need to print anything explicitly just try following
ImageIcon ii=new ImageIcon("PATH_TO_IMAGE_FILE");
//Create a label with your image file
JLabel label=new JLabel(ii);
//Create a window using JFrame with title ( Put image on JLabel )
JFrame frame=new JFrame("Put image on JLabel");
//Add created label into JFrame
frame.add(label);
Update as per your clarification :
Follow this , and pass label.getGraphics() to printAll() method
You should implenent Printable interface
see for example here