I want to initialize an ImageIcon with an offset. The resizing is already working properly, but how can I tell the resized image icon to map to a certain x and y coordinate?
I have a map in my project and the attached snippet is the initialization of the image icon.
The image icon is then printed to a JLabel with a static size and position.
ImageIcon imageIcon = createImageIcon("/mapEnd1.png", "Map");
Image image = imageIcon.getImage();
Image newimg = image.getScaledInstance(600, 800, java.awt.Image.SCALE_SMOOTH);
imageIcon = new ImageIcon(newimg);
Related
I am trying to attach different images into a image. This is happening but after attaching the color of the image changes. Here is my existing code
BufferedImage baseImg = getBaseImage(src);
Graphics2D g = baseImg.createGraphics();
BufferedImage bmg = getBufferedImageFromSrc(path);
String[] coords = coord.split(",");
g.drawImage((Image)bmg,(int) Double.parseDouble(coords[0]),(int) Double.parseDouble(coords[1]), 60,60,Color.white,null);
g.dispose();
String path = "C://resources/temp.jpeg";
ImageIO.write(bimg, "jpeg", new File(path));
i am able to add the image to the base image at the specified co-ordinates but the base image colour is getting changed.
Here is a sample image
Thanks in advance
I'm working on swing project.
I've jErrorMsgLabel to show error message.
I want to use L&F icons for jErrorMsgLabel same as below snap showing information message "Please specify a value for Database:"
So I did
jErrorMsgLabel.setIcon(UIManager.getIcon("OptionPane.errorIcon"));`
But icon size is same as JOptionPane
How can I change icon size?
Or is there any other way around to show error messages?
You need to scale the image yourself:
ImageIcon icon = (ImageIcon)UIManager.getIcon("OptionPane.errorIcon");
Image image = icon.getImage();
Image scaledImage = image.getScaledInstance(80, 80, Image.SCALE_DEFAULT);
Icon scaledIcon = new ImageIcon( scaledImage );
yourLabel.setIcon( scaledIcon );
Of course whenever you scale an image larger you will get pixilation.
Edit:
Following is the code that paints the Icon to a BufferedImage which can then be scaled:
Icon icon = UIManager.getIcon("OptionPane.errorIcon");
BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bufferedImage.createGraphics();
icon.paintIcon(null, g, 0, 0);
g.dispose();
ImageIcon errorIcon = new ImageIcon(bufferedImage.getScaledInstance(15, 15, Image.SCALE_SMOOTH));
I am trying to set a background image for a ListViewer.
listViewer = new ListViewer(parent);
listViewer.setContentProvider(this);
listViewer.setLabelProvider(this);
listViewer.getList().setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
Image image = new Image(Display.getDefault(), ListPart.class.getResourceAsStream("/icons/blurred.jpg"));
Rectangle imageBounds = image.getBounds();
Rectangle rectangle = listViewer.getList().getBounds();
GC gc = new GC(listViewer.getList());
gc.drawImage(image, imageBounds.x, imageBounds.y,imageBounds.width,imageBounds.height,rectangle.x,rectangle.y,rectangle.width,rectangle.height);
listViewer.getList().setBackgroundImage(image);
I tried to draw the image with GC but it doesn't work. If I leave the image as it is, it doesn't stretch after the List.
Is there an easier way to do this?
I am trying to give my interface a new function, but I have encountered some obstacles. I want to enlarge image on JLabel when mouseEnters.
Here is how my JLabels looks:
int sacle = 50 //Size of my JLabel Icon
int zoom = 10 // How much the icon should enlarge
imageIcon = new ImageIcon(new ImageIcon(myClass.class.getResource(Picture))
.getImage().getScaledInstance(scale, scale, Image.SCALE_SMOOTH));
JLabel stackIsGreat = new JLabel();
stackIsGreat.setIcon(imageIcon);
//and I add multiple of such JLabels`
And the code goes on and on. I wanted to creat a function and add it to mouseListener, so all will behave the same. I wanted to achive that with:
//inside external method
activeLabel = (javax.swing.JLabel)(e.getSource());
ImageIcon temp = (ImageIcon) activeLabel.getIcon();
But there is no way I know I could get use of this, because java says I need Image to create my enlarged ImageIcon
ImageIcon enlarged = new ImageIcon((Image).getScaledInstance(scale + zoom, scale + zoom, Image.SCALE_SMOOTH))
How can I retrive the image used to crate the JLabel from code.
Any help would be appreciated.
I want to enlarge image on JLabel when mouseEnters.
Instead of creating your own MouseListener you could use a JButton to give you the rollover effect:
Something like:
JButton button = new JButton(...);
button.setBorderPainted( false );
ImageIcon icon = (ImageIcon)button.getIcon();
Image image = icon.getImage();
Image scaled = image.getScaledImage(...);
button.setRolloverIcon( new ImageIcon( scaled ) );
I've been wondering how can I set an ImageIcon to a button using the Image path and set it to a JButton?
I can resize the image's size according to the buttons size:
frontViewImageFile = fc.getSelectedFile();
MainFrame.btnFrontView.setIcon(new ImageIcon(ImageIO.read(
frontViewImageFile).getScaledInstance(150, 150, Image.SCALE_SMOOTH)));
But the image came from a file chooser and I can use getScaledInstance method to resize the picture.
How can i do this with image path since method getScaledInstance is undefined for the type String?
ImageIcon icon = ...;
JButton b = ...;
Image im = icon.getImage();
Image im2 = im.getScaledInstance(b.getWidth(), b.getHeight(), ...);
b.setIcon(new ImageIcon(im2));