View Single Post
  #4 (permalink)  
Old 05-07-2008, 07:58 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,029
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); } }
Reply With Quote