How to print content of a label in java? - java

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

Related

Unable to change image in jScrollPane upon clicking next [duplicate]

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.

How to dynamically change JFrame background?

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.

Display a set of images inside Netbeans JFrame

I know how to display the images in separate JFrames.
public static void displayImage(BufferedImage img) {
JFrame f = new JFrame();
f.add(new JLabel(new ImageIcon(img)));
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
But I want a method to display all the images in a single JFrame(or in a suitable element), So I can shift between the images using two buttons("Next" and "Previous").
actually you don't need a multiple frame for this.you don't need even multiple jpanels .just a single jlable is sufficient .
all you need to do is change image of jlable when button press .
1) put your images in a directory and give a name .names should have pattern
example
C:\Users\Madhawa.se\Desktop\images\i1.jpg
C:\Users\Madhawa.se\Desktop\images\i2.jpg
C:\Users\Madhawa.se\Desktop\images\i3.jpg
C:\Users\Madhawa.se\Desktop\images\i4.jpg
C:\Users\Madhawa.se\Desktop\images\i5.jpg
2) declare a int variable as global
int i=0;
3) increase i by one when click next button and add appropriate image to label
i++;
ImageIcon imgThisImg = new ImageIcon("C:\\Users\\Madhawa.se\\Desktop\\images\\i"+i+".jpg");
jLabel1.setIcon(imgThisImg);
4) decrease i by one when click previous button and add appropriate image to label
i--;
ImageIcon imgThisImg = new ImageIcon("C:\\Users\\Madhawa.se\\Desktop\\images\\i"+i+".jpg");
jLabel1.setIcon(imgThisImg);
you can declare also String array which holds image paths .if your image names want to be arbitrary names .and then you can get image same way.
and also you should have a logic to correctly cycle images.you should restrict i to image quantity .otherwise image doesn't exist for i after i exceeded
output>>

displaying images from an array in a gui window using swing in java

I am developing a program that will populate an array with 52 images of cards from a file. I would like to display these images in a gui window. This is for a program that will select five random card images and display them in a gui window. So, right now, i am trying to develop the part of the code which will display images from an array in a window and i am at a loss as to how to display png images in a jframe. This is the code i have so far. I used a system.out.println statement so i know that the array of 52 card images is populating correctly, however, i do not know how to display them properly in a window.
String[] cardsArray = new String[52];
for (int i = 0; i < 52; i++)
{
cardsArray[i] = "C:\\Users\\mike\\Documents\\NetBeansProjects\\card shuffler\\cards\\\"+String.valueOf(i+1)+".png";
}
System.out.println(Arrays.toString(cardsArray));
additional note. I have to use a jframe to display the results in a side by side layout. I thought to use flowLayout to accomplish this, but, i a not sure how to pass in an array of images. I have no problem doing it with a single image from a file. I am using the code below as a guide.
JFrame myJFrame = new JFrame();
// create and assign a FlowLayout for myFrame
myJFrame.setLayout(new FlowLayout());
// Create a label with an image icon
JLabel jlCSCI = new JLabel(new ImageIcon("CSCI.jpg"));
// add the Label to the frame
myJFrame.add(jlCSCI); // Add thelabel to MyGridLayout
// set the title, size, location and exit behavior for the frame
myJFrame.setTitle("ImageIcon Demo");
myJFrame.setSize(240, 200);
myJFrame.setLocation(200, 100);
myJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// make the frame visible (activate the GUI frame)
myJFrame.setVisible(true);
I am not sure how to develop the statement that would utilize an array that i created within the program.
Maybe like this?
for (String imgName : cardsArray)
{
myJFrame.add(new JLabel(new ImageIcon(imgName)));
}
Hope this helps.
EDIT:
I simply added a JLabel with an Icon to the frame. The ImageIcon class is just an implementation of the Icon interface and it creates an icon by reading an image from file. Creating a JLabel with an Icon will display the icon instead of the text. You can also combine the text and the icon. For more info, check the documentation.

Displaying a JLabel on top of another JLabel

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.

Categories