|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

05-05-2008, 06:23 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 6
|
|
|
Scale 2 or more pictures using a JSlider
Hi everybody,
I posted this message in general Java, I'am not sure if it is right or not.
I would like to know how I can display 2 or more images and then make scale in these images on the same time.
Thank you.
Francisco
I found this code here in Java Forums this is my reference:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
public class MapScale extends JPanel {
BufferedImage image;
double scale = 1.0;
public MapScale(BufferedImage image) {
this.image = image;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATI ON,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
double x = (getWidth() - scale*image.getWidth())/2;
double y = (getHeight() - scale*image.getHeight())/2;
AffineTransform at = AffineTransform.getTranslateInstance(x,y);
at.scale(scale, scale);
g2.drawRenderedImage(image, at);
}
public Dimension getPreferredSize() {
int w = (int)(scale*image.getWidth());
int h = (int)(scale*image.getHeight());
return new Dimension(w, h);
}
private JSlider getSlider() {
int min = 1, max = 36, inc = 5;
final JSlider slider = new JSlider(min, max, 16);
slider.setMajorTickSpacing(5);
slider.setMinorTickSpacing(1);
slider.setPaintTicks(true);
slider.setSnapToTicks(true);
slider.setLabelTable(getLabelTable(min, max, inc));
slider.setPaintLabels(true);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int value = slider.getValue();
scale = (value+4)/20.0;
revalidate();
repaint();
}
});
return slider;
}
private Hashtable getLabelTable(int min, int max, int inc) {
Hashtable<Integer,JLabel> table = new Hashtable<Integer,JLabel>();
for(int j = min; j <= max; j += inc) {
String s = String.format("%.2f", (j+4)/20.0);
table.put(Integer.valueOf(j), new JLabel(s));
}
return table;
}
public static void main(String[] args) throws IOException {
String path = "images/GIRL.JPG";
BufferedImage image = ImageIO.read(new File(path));
MapScale test = new MapScale(image);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(test));
f.getContentPane().add(test.getSlider(), "Last");
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
|
|

05-05-2008, 09:49 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 910
|
|
|
scaling multiple images
There are two general ways to do this:
1 — drawing the images in a JPanel
2 — mounting scaled images in JLabels.
Here's an example showing both ways:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
public class MultiScale implements ChangeListener {
BufferedImage[] images;
ImageScalePanel imagePanel;
JLabel[] labels;
double scale = 1.0;
public MultiScale(BufferedImage[] images) {
this.images = images;
}
public void stateChanged(ChangeEvent e) {
int value = ((JSlider)e.getSource()).getValue();
scale = (value+4)/20.0;
imagePanel.setScale(scale);
setImages();
}
private JPanel getContent() {
imagePanel = new ImageScalePanel(images);
imagePanel.setBorder(BorderFactory.createTitledBorder("graphic"));
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weighty = 1.0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(imagePanel, gbc);
panel.add(getLabelPanel(), gbc);
return panel;
}
private JPanel getLabelPanel() {
JPanel panel = new JPanel(new GridLayout(1,0));
panel.setBorder(BorderFactory.createTitledBorder("labels"));
labels = new JLabel[3];
for(int i = 0; i < labels.length; i++) {
labels[i] = new JLabel((ImageIcon)null, JLabel.CENTER);
panel.add(labels[i]);
}
setImages();
return panel;
}
private void setImages() {
for(int i = 0; i < labels.length; i++) {
labels[i].setIcon(new ImageIcon(getScaledImage(i)));
}
labels[0].getParent().validate();
}
private BufferedImage getScaledImage(int index) {
int w = (int)(scale*images[index].getWidth());
int h = (int)(scale*images[index].getHeight());
int type = images[index].getType();
BufferedImage image = new BufferedImage(w, h, type);
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2.drawImage(images[index], 0, 0, w, h, labels[index]);
g2.dispose();
return image;
}
private JSlider getSlider() {
int min = 1, max = 36, inc = 5;
JSlider slider = new JSlider(min, max, 16);
slider.setMajorTickSpacing(5);
slider.setMinorTickSpacing(1);
slider.setPaintTicks(true);
slider.setSnapToTicks(true);
slider.setLabelTable(getLabelTable(min, max, inc));
slider.setPaintLabels(true);
slider.addChangeListener(this);
return slider;
}
private Hashtable getLabelTable(int min, int max, int inc) {
Hashtable<Integer,JLabel> table = new Hashtable<Integer,JLabel>();
for(int j = min; j <= max; j += inc) {
String s = String.format("%.2f", (j+4)/20.0);
table.put(Integer.valueOf(j), new JLabel(s));
}
return table;
}
public static void main(String[] args) throws IOException {
String prefix = "images/geek/geek";
String ext = ".gif";
String[] ids = { "-c---", "--g--", "---h-" };
BufferedImage[] images = new BufferedImage[ids.length];
for(int i = 0; i < images.length; i++) {
String path = prefix + ids[i] + ext;
images[i] = ImageIO.read(new File(path));
}
MultiScale test = new MultiScale(images);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(test.getContent()));
f.getContentPane().add(test.getSlider(), "Last");
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
class ImageScalePanel extends JPanel {
BufferedImage[] images;
double scale = 1.0;
int totalWidth;
public ImageScalePanel(BufferedImage[] images) {
this.images = images;
for(int i = 0; i < images.length; i++)
totalWidth += images[i].getWidth();
}
public void setScale(double scale) {
this.scale = scale;
revalidate();
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
double x = getInsets().left;
double y = getInsets().top;
for(int i = 0; i < images.length; i++) {
AffineTransform at = AffineTransform.getTranslateInstance(x,y);
at.scale(scale, scale);
g2.drawRenderedImage(images[i], at);
x += scale*images[i].getWidth();
}
}
public Dimension getPreferredSize() {
Insets insets = getInsets();
int vi = insets.top + insets.bottom;
int hi = insets.left + insets.right;
int w = (int)(scale*totalWidth) + hi;
int h = (int)(scale*images[0].getHeight()) + vi;
return new Dimension(w, h);
}
}
Images from Using Swing Components Examples, down low.
|
|

05-06-2008, 03:44 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 6
|
|
Morning,
I checked the code and I took the images from the project CheckBoxDemo ( Using Swing Components: Examples (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components)) and it works.
Just I would like to comment that I found 3 things when I put the code in Eclipse:
1. in this part I had an error:
...
Hashtable<Integer,JLabel> table = new Hashtable<Integer,JLabel>();
...
It display a Syntax error, parameterized types are only available if source level is 5.0.
I just changed by:
Hashtable table = new Hashtable();
2. In this line:
...
String s = String.format("%.2f", (j+4)/20.0);
...
and the message in Eclipse said:
The method format(String, Object[]) in the type String is not applicable for the arguments (String, double)
Here I wrote String s = "x"; instead.
I don't understand why method "format" does not work.
3. It has appeared a warning message in Eclipse when I tryed to do a debug, something about deprecated functions, and Eclipse gave me 3 options (Continue, Cancel and Restart), I chose Restart and now it works correctly. I tryed to put the original code in a new project to see the text message but I couldn't do it, this is rare. Anyway.
Thank you very much hardwired.
Now I would like to know how can I put an image with a dimension 2400 x 1800 like a thumbnail into this project. I will check that, however if you can help me with a reference please, I will apprecite it.
Thank you again.
Panchitopro
|
|

05-07-2008, 07:58 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 910
|
|
parameterized types are only available if source level is 5.0
Generics was introduced in j2se 1.5. You can check the version of java at the prompt with >java -version
The method format(String, Object[]) in the type String is not applicable for the arguments (String, double)
The String. format method was also new in j2se 1.5
Looks like your ide might have a format method used for writing out arrays.
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Thumbnail {
private JLabel getContent(BufferedImage image) {
BufferedImage thumb = scaleImage(image);
ImageIcon icon = new ImageIcon(thumb);
JLabel label = new JLabel(icon, JLabel.CENTER);
return label;
}
private BufferedImage scaleImage(BufferedImage src) {
int w = 75; // thumbnail
int h = 100; // dimensions
int type = BufferedImage.TYPE_INT_RGB;
BufferedImage dst = new BufferedImage(w, h, type);
Graphics2D g2 = dst.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2.setBackground(UIManager.getColor("Panel.background"));
g2.clearRect(0,0,w,h);
double xScale = (double)w/src.getWidth();
double yScale = (double)h/src.getHeight();
double scale = Math.min(xScale, yScale); // fit
//Math.max(xScale, yScale); // fill
double x = (w - scale*src.getWidth())/2;
double y = (h - scale*src.getHeight())/2;
AffineTransform at = AffineTransform.getTranslateInstance(x,y);
at.scale(scale, scale);
g2.drawRenderedImage(src, at);
g2.dispose();
return dst;
}
public static void main(String[] args) throws IOException {
String path = "http://imgsrc.hubblesite.org/hu/db/" +
"1999/12/images/a/formats/full_jpg.jpg";
URL url = new URL(path);
URLConnection uc = url.openConnection();
uc.connect();
InputStream is = uc.getInputStream();
BufferedImage image = ImageIO.read(is);
System.out.printf("imageWidth = %d imageHeight = %d%n",
image.getWidth(), image.getHeight());
is.close();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Thumbnail().getContent(image));
f.setSize(300,175);
f.setLocation(200,200);
f.setVisible(true);
}
}
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|