Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-05-2008, 06:23 PM
Member
 
Join Date: May 2008
Posts: 8
Panchitopro is on a distinguished road
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);
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-05-2008, 09:49 PM
Senior Member
 
Join Date: Jul 2007
Posts: 933
hardwired is on a distinguished road
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:
Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-06-2008, 03:44 PM
Member
 
Join Date: May 2008
Posts: 8
Panchitopro is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-07-2008, 07:58 AM
Senior Member
 
Join Date: Jul 2007
Posts: 933
hardwired is on a distinguished road
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.
Code:
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); } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-20-2008, 05:44 PM
Member
 
Join Date: May 2008
Posts: 8
Panchitopro is on a distinguished road
Hi,

I came back, I have had some problems with the version, it seems that I can not use the swing controls into my project because of the versions with MS VM and Sun.

Thank you for your answer and I will write later.

Panchitopro
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Funny Pictures Thread(this means a lot of pictures...) joshua Jokes and Funny Things 8 05-08-2008 07:36 AM
Scale 2 or more pictures using a JSlider Panchitopro New To Java 0 05-05-2008 06:22 PM
Small scale Java Editor Greenfrog99 AWT / Swing 0 01-27-2008 09:46 PM
How can we zoom a map using JSlider barney AWT / Swing 3 01-04-2008 09:49 AM
How To:Use a JSlider to adjust Text size in a JPanel louiebagz AWT / Swing 2 07-01-2007 08:37 AM


All times are GMT +3. The time now is 02:32 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org