2014-03-30 45 views
2

我正在嘗試製作一個java桌面應用程序。我有一個JLabel我在洗牌圖像,但所有圖像大小不同,所以我想修復圖像的大小JLabel如何在jlabel上設置imageicon修復程序

我該怎麼做?

這裏是我的代碼:

public class ImageShuffle1 extends JPanel { 

    private List<Icon> list = new ArrayList<Icon>(); 
    private List<Icon> shuffled; 
    private JLabel label = new JLabel(); 
    private Timer timer = new Timer(1000, new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
     update(); 
     } 
    }); 

    public ImageShuffle1() { 
     this.setLayout(new GridLayout(1, 0)); 

     list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\e.jpg")); 
    list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\d.jpg")); 
    list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png")); 
     list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\f.jpg")); 
      list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\l.jpg")); 
     //label.setIcon(UIManager.getIcon("OptionPane.informationIcon")); 

    for(Icon icon: list){ 

     Image img = icon.getImage() ; 

     // put here the size properties 
     Image newimg = img.getScaledInstance(45, 34, java.awt.Image.SCALE_SMOOTH) ; 

     icon = new ImageIcon(newimg); 

    } 

shuffled = new ArrayList<Icon>(list); 
Collections.shuffle(shuffled); 
     timer.start(); 
    } 

    private void update() { 
     if (shuffled.isEmpty()) { 
    shuffled = new ArrayList<Icon>(list); 
    Collections.shuffle(shuffled); 
} 

Icon icon = shuffled.remove(0); 
label.setIcon(icon); 
} 

private void display() { 
    JFrame f = new JFrame("ImageShuffle"); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.add(this); 
    f.add(label); 
    f.pack(); 
    f.setLocationRelativeTo(null); 
    f.setVisible(true); 
} 

public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new ImageShuffle1().display(); 
      } 
     }); 
    } 
} 

我這裏得到這個錯誤。

行/變量getimage找不到圖像img = icon.getImage();

在此先感謝

+0

你已經問過這個問題,昨天得到了答案。用重複的問題退出論壇。 – camickr

回答

0

使用BufferedImage代替具有重新調整大小的功能的圖標。

下面是代碼

import java.awt.EventQueue; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.Timer; 

public class ImageShuffle1 extends JPanel { 

    private List<BufferedImage> list = new ArrayList<BufferedImage>(); 
    private List<BufferedImage> shuffled; 
    private JLabel label = new JLabel(); 

    private int width = 50; 
    private int height = 100; 

    private Timer timer = new Timer(1000, new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      update(); 
     } 
    }); 

    public ImageShuffle1() { 

     try { 
      list.add(resizeImage(ImageIO.read(new File("resources/1.png")))); 
      list.add(resizeImage(ImageIO.read(new File("resources/2.png")))); 
      list.add(resizeImage(ImageIO.read(new File("resources/6.png")))); 
      list.add(resizeImage(ImageIO.read(new File("resources/Tulips.jpg")))); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     shuffled = new ArrayList<BufferedImage>(list); 
     Collections.shuffle(shuffled); 
     timer.start(); 
    } 

    private BufferedImage resizeImage(BufferedImage originalImage) throws IOException { 
     BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
     Graphics2D g = resizedImage.createGraphics(); 
     g.drawImage(originalImage, 0, 0, width, height, null); 
     g.dispose(); 
     return resizedImage; 
    } 

    private void update() { 
     if (shuffled.isEmpty()) { 
      shuffled = new ArrayList<BufferedImage>(list); 
      Collections.shuffle(shuffled); 
     } 

     BufferedImage icon = shuffled.remove(0); 
     label.setIcon(new ImageIcon(icon)); 
    } 

    private void display() { 
     JFrame f = new JFrame("ImageShuffle"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.add(this); 
     f.add(label); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new ImageShuffle1().display(); 
      } 
     }); 
    } 
} 
+0

如何從項目文件夾中檢索圖像 – user3456343

+0

試試這個'ImageIO.read(getClass()。getResourceAsStream(「3.png」)))'從相同的包中讀取圖像文件,存在。 – Braj

+0

我不想要只有一個圖像,我想要所有圖像 – user3456343

0

首先,每幅圖像調整到一個固定的大小,它必須適合於JLabel

public static Boolean resizeImage(String sourceImage, String destinationImage, Integer Width, Integer Height) { 
    BufferedImage origImage; 
    try { 

     origImage = ImageIO.read(new File(sourceImage)); 
     int type = origImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : origImage.getType(); 

     //*Special* if the width or height is 0 use image src dimensions 
     if (Width == 0) { 
      Width = origImage.getWidth(); 
     } 
     if (Height == 0) { 
      Height = origImage.getHeight(); 
     } 

     int fHeight = Height; 
     int fWidth = Width; 

     //Work out the resized width/height 
     if (origImage.getHeight() > Height || origImage.getWidth() > Width) { 
      fHeight = Height; 
      int wid = Width; 
      float sum = (float)origImage.getWidth()/(float)origImage.getHeight(); 
      fWidth = Math.round(fHeight * sum); 

      if (fWidth > wid) { 
       //rezise again for the width this time 
       fHeight = Math.round(wid/sum); 
       fWidth = wid; 
      } 
     } 

     BufferedImage resizedImage = new BufferedImage(fWidth, fHeight, type); 
     Graphics2D g = resizedImage.createGraphics(); 
     g.setComposite(AlphaComposite.Src); 

     g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
     g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 
     g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

     g.drawImage(origImage, 0, 0, fWidth, fHeight, null); 
     g.dispose(); 

     ImageIO.write(resizedImage, "png", new File(destinationImage)); 
     ... 
+0

你能告訴我在哪裏我需要改變我的代碼 – user3456343

+0

你必須調整每個圖像的大小,然後將它添加到ArrayList。然後將調整大小的圖像的路徑添加到列表中。所有這些。 –

+0

請學習和使用[Java編程語言的代碼約定](http://www.oracle.com/technetwork/java/codeconv-138413.html) – MadProgrammer

1

讓我們與這個啓動...

for(Icon icon: list){ 

    Image img = icon.getImage() ; 

Icon沒有方法getImage,實際上沒有辦法讓「圖像」數據m沒有首先它渲染的東西(如BufferedImage

更好的解決方案可能是將圖像加載到支持BufferedImage一個ListIcon類aintained。 BufferedImage是一個更通用的起點,因爲它從Image延伸,它可以與ImageIcon一起使用。例如...

private List<BufferedImage> list = new ArrayList<BufferedImage>(); 
//... 
list.add(ImageIO.read("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\e.jpg")); 

看看Reading/Loading an Image

縮放你可能想看看