Resizing-scaling the image in JTextPane - java

I want to scale and resize with mouse selected image, change selected image with corner point, and change scale picture in JTextPane as in Microsoft Word.
This code just added image without change size in text editor but not have function propertes size picture.
Please somebody help me with code:
JFileChooser jf=new JFileChooser();
// Show open dialog
int option=jf.showOpenDialog(this);
// If user chooses to insert..
if(option == JFileChooser.APPROVE_OPTION) {
File file = jf.getSelectedFile();
if(isImage(file)) {
jTextPane1.insertIcon(new ImageIcon(file.getAbsolutePath()));
}else
// Show an error message, if not an image
JOptionPane.showMessageDialog(this,"The file is not an image.",
"Not Image", JOptionPane.ERROR_MESSAGE);
}

Related

Display image on a Java button

xstButton.setIcon(new ImageIcon("D://icon-tender-check-press.png"));
I am using this line of code to display an image on a Java button.
I cannot see the desired image on the button, need help!!!
You could try it like this:
Image image = ImageIO.read(getClass().getResource("D://icon-tender-check-press.png"));
button.setIcon(new ImageIcon(image));
But i would suggest to create a Folder in your project to store images:
Image image = ImageIO.read(getClass().getResource("images/icon-tender-check-press.png"));
button.setIcon(new ImageIcon(image));
Although i am not exactly sure what your question is
Probably file "D://icon-tender-check-press.png" doesn't exists or is not a valid image.
Check it exists first.
File f = new File("D://icon-tender-check-press.png");
if(f.exists() && !f.isDirectory()) {
System.out.println("File exists");
}

Java - Custom Image in JoptionPane.ShowConfirmDialog is not working

When the User enters the password and click on Ok button the password will be encrypted and stored in JTextArea. And this is working fine. But I want to add a custom logo in the showConfirmDialog and showMessageDialog popup. I tried with the below code, but the custom Image (logo) is not displaying in the message popup
public static void main(String[] args) {
Box box = Box.createHorizontalBox();
JLabel label = new JLabel("Enter your password : ");
box.add(label);
JPasswordField passwordField = new JPasswordField(24);
box.add(passwordField);
final ImageIcon icon = new ImageIcon("C:\\Users\\Test\\Internet.png");
int button = JOptionPane.showConfirmDialog(null, box, "Enter your password", JOptionPane.OK_CANCEL_OPTION, JOptionPane.NO_OPTION, icon);
if (button == JOptionPane.OK_OPTION) {
String password = new String(passwordField.getPassword());
String encryptedPassword;
if (password != null && !password.equals("")) {
byte[] bytesEncoded = Base64.encodeBase64(password.getBytes());
JTextArea richTextField = new JTextArea(10, 10);
encryptedPassword = new String(bytesEncoded);
richTextField.setText(encryptedPassword);
richTextField.setOpaque(false);
richTextField.setEditable(false);
JOptionPane.showMessageDialog(null, richTextField);
} else {
JOptionPane.showMessageDialog(null,
"Password cannot be null. Please enter password to encrypt.");
}
}
}<br>
I'm passing ImageIcon object into the JoptionPane.showConfirmDialog as an argument. But when I run this, i don't see any Image displayed in the popup. I'm not sure what I'm doing wrong here.
Note : I need a custom Image to be displayed in both the popup's. showConfirmDialog and showMessageDialog
Any help would be greatly appreciated
Your code is perfectly fine. I just ran it in my environment and it worked fine. Which leads me to believe that your problem is the path of the image. I even tested it with a path for a image that wasn't there and the window show without showing any image.
I only changed two things, the path of the image obviously:
final ImageIcon icon = new ImageIcon("c:\\temp\\poke-ball-png-13_30x30.png");
This image I got from Free Icons PNG
And the Base64 class since there is no mention from where you are using it I use the java one:
import java.util.Base64;
....
byte[] bytesEncoded = Base64.getEncoder().encode(password.getBytes());
So be sure that your image "C:\\Users\\Test\\Internet.png" Is really there on the disk at that path

java jLabel image does not update

I'm creating a feedback label that displays the picture that the user has chosen in a file dialog.
The moment when a picture file is selected, the label will update itself into that image of which the user has clicked.
The first time when the picture is chosen it works fine, however when another picture is chosen for the 2nd time onwards, it remains as the first picture.
Codes:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
//browse button
FileDialog fd = new FileDialog(this, "Choose a file", FileDialog.LOAD);
fd.setDirectory("C:\\");
fd.setFile("*.jpg"); // jpg files only
fd.setVisible(true);
String filename = fd.getFile();
if (filename == null) {
System.out.println("You cancelled the choice");
} else {
savePicture("temp"); // save it in temp.jpg. This overwrites any existing picture.
ImageIcon imgThisImg = null;
imgThisImg = new ImageIcon(absfilePath+ "/temp.jpg");
jLabel7.setIcon(null);
jLabel7.setIcon(imgThisImg);
jLabel7.revalidate();
jLabel7.repaint();
}
During debugging, the moment after savePicture() function is executed, the directory picture is updated. Therefore it's not an issue with overwritting the file. The file is overwritten correctly, why does it still display the previous image? Is there a cache or something that i need to clear?
Thanks.
Using ImageIO to the read file works best. Can be achieved using the following line.
jLabel7.setIcon(new JLabel(new ImageIcon(ImageIO.read(new File("C:\\Users\\Cameron Gillespie\\Documents\\NetBeansProjects\\OnlineCabsClient\\src\\images\\taxiBackground.png")))));
You are taking the label then setting the icon. Creating a new label and ImageIcon. Then using ImageIO to read the file. Reading the image and printing it to the label.

abnormal resizing of java.awt.Image on using getImage() and getScaledInstance()

I m making a java rmi based application where i pass and receive an ImageIcon object from the server...
(the image is stored in a separate URL in server)
The function involves the following....
1. Getting the image from the server at first....(on button press A)
2. Replacing it with a image file in the client[optional]....(on button press B)
3. Remove the image with a default image[optional]....(on button press C)
4. Sending it back to the Server....................(On button press D).....
Here the image is displayed in ajlabel calledimg_label
The codes i've used are as follows.....
Variables used
java.awt.Image img;
javax.swing.ImageIcon CurrentImageIcon;
javax.swing.ImageIcon DefaultImageIcon;
// CurrentImageIcon contains the image to be displayed in the img_label....
// img is used for copying as well for scaling......
// DefaultImageIcon holds the default Image......
On Button Press A
img = temp.getImage();
CurrentImageIcon = new ImageIcon(img);
// Assuming temp holds the ImageIcon taken from the server.......
img=img.getScaledInstance(83,85 , Image.SCALE_DEFAULT);
img_label.setIcon(new ImageIcon(img));
img_label.revalidate();
img_label.repaint();
On Button Press B
String url_text = jTextField.getText(); // taking the url frm the field.....
CurrentImageIcon = new ImageIcon(url_text);
img=CurrentImageIcon.getImage();
img=img.getScaledInstance(83,85 , Image.SCALE_DEFAULT);
img_label.setIcon(new ImageIcon(img));
img_label.revalidate();
img_label.repaint();
On Button Press C
img = DefaultImageIcon.getImage();
CurrentImageIcon = new ImageIcon(img);
img=img.getScaledInstance(83,85 , Image.SCALE_DEFAULT);
img_label.setIcon(new ImageIcon(img));
img_label.revalidate();
img_label.repaint();
On Button Press D
// ImagetoSend is an ImageIcon to be sent to the Server.....
ImagetoSend = CurrentImageIcon;
CurrentImageIcon = null;
Now the problem i m getting is a weird one......
The image is getting embedded as i wanted on repainting when i click this button........
But when i download the recently uploaded image next time on Button press A....it is displayed either magnified or reduced to size even though i included the getScaledInstance method....
like this...
The image i am handling is a jpg image....
I even checked the image at Server directory.....No size change has occured on that file which was uploaded from client to server. But the change is observed when it is downloaded and embedded to the jlabel...
Can Anyone help me to sort out this issue...??
Somehow it is getting scaled down twice. Either you scale it down then send it to the server where it is scaled down, or the both button clicks scale it. Eliminate the scale down code on the server side and see what happens.

Java Swing: popup : display small icon in menu

I have a right click popup menu that works fine.
I now want to add a little image next to the text.
I managed to display the image in the popupmenu but its the full size version.
what I want to know is:
is there a way to automatically reize the icon to be the right size ( same as the text ) or do I have to resize the image?
Jason
You have to resize the Icon to be whatever size you want it to be. Something like:
ImageIcon icon = new ImageIcon( ... );
Image image = icon.getImage().getScaledInstance(64, 64, Image.SCALE_SMOOTH);
JLabel label = new JLabel( new ImageIcon( image ) );

Categories