1

png形式のボタン画像を読み込んでいます。しかし、フォーマットが大きすぎます。その幅と高さを定義された範囲にサイズ変更するにはどうすればよいですか。ただし、画像サイズの使用は避けてください。

前:

 public JButton createButton(String name, String toolTip) {
    Image a = null;
    try {
      a = ImageIO.read((InputStream) Test.class.getResourceAsStream("/image/menu/" + name + ".png"));
    } catch (IOException ex) {
      ex.printStackTrace();
    }

    ImageIcon iconRollover = new ImageIcon(a);        
    int w = iconRollover.getIconWidth();
    int h = iconRollover.getIconHeight();

    // get the cursor for this button
    Cursor cursor =
            Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);

    // make translucent default image
    //Image image = screen.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
    Graphics2D g = (Graphics2D) a.getGraphics();
    Composite alpha = AlphaComposite.getInstance(
            AlphaComposite.SRC_OVER, .5f);
    g.setComposite(alpha);
    g.drawImage(iconRollover.getImage(), 0, 0, null);
    g.dispose();

    ImageIcon iconDefault = new ImageIcon(a);
    ImageIcon iconPressed = new ImageIcon(a);

    // create the button
    JButton button = new JButton();
    button.setIgnoreRepaint(true);
    button.setFocusable(false);
    button.setToolTipText(toolTip);
    button.setBorder(null);
    button.setContentAreaFilled(false);
    button.setCursor(cursor);
    button.setIcon(iconDefault);
    button.setRolloverIcon(iconRollover);
    button.setPressedIcon(iconPressed);
    return button;
  }

後:

ファローアップ:

  public JButton createButton(String name, String toolTip) {

    // Create image
    BufferedImage a = null;
    try {
      a = ImageIO.read((InputStream) P2P.class.getResourceAsStream("/image/menu/" + name + ".png"));
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    BufferedImage bi = new BufferedImage(70, 70, BufferedImage.TRANSLUCENT);

    Graphics2D g = (Graphics2D) bi.getGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.drawImage(a, 0, 0, 70, 70, null);
    g.dispose();   
    // get the cursor for this button
    Cursor cursor =
            Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
    ImageIcon iconRollover = new ImageIcon(bi);
    ImageIcon iconDefault = new ImageIcon(bi);
    ImageIcon iconPressed = new ImageIcon(bi);

    // create the button
    JButton button = new JButton();
    //button.addActionListener(this);
    button.setIgnoreRepaint(true);
    button.setFocusable(false);
    button.setToolTipText(toolTip);
    button.setBorder(null);
    button.setContentAreaFilled(false);
    button.setCursor(cursor);
    button.setIcon(iconDefault);
    button.setRolloverIcon(iconRollover);
    button.setPressedIcon(iconPressed);
    return button;
  }
4

3 に答える 3

1

getScaledInstance()次のように、Imageインスタンスでメソッドを使用してみませんか。

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

static int width=300;//change this to your wanted size
static int height =500;

public class Main extends JFrame implements ActionListener {
  Image img;

  JButton getPictureButton  = new JButton("Get Picture");

  public static void main(String[] args) {
    new Main();
  }

  public Main() {
    this.setSize(600, 600);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel picPanel = new PicturePanel();
    this.add(picPanel, BorderLayout.CENTER);

    JPanel buttonPanel = new JPanel();
    getPictureButton.addActionListener(this);
    buttonPanel.add(getPictureButton);
    this.add(buttonPanel, BorderLayout.SOUTH);

    this.setVisible(true);
  }

  public void actionPerformed(ActionEvent e) {
    String file = "a.png";
    if (file != null) {
      Toolkit kit = Toolkit.getDefaultToolkit();
      img = kit.getImage(file);

      img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);//scale the image to wanted size

      this.repaint();
    }
  }
  class PicturePanel extends JPanel {
    public void paintComponent(Graphics g) {
      g.drawImage(img, 0, 0, this);
    }
  }

}
于 2012-07-17T18:28:10.457 に答える
0

作品

  public JButton createButton(String name, String toolTip) {

    // Create image
    BufferedImage a = null;
    try {
      a = ImageIO.read((InputStream) P2P.class.getResourceAsStream("/image/menu/" + name + ".png"));
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    BufferedImage bi = new BufferedImage(70, 70, BufferedImage.TRANSLUCENT);

    Graphics2D g = (Graphics2D) bi.getGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.drawImage(a, 0, 0, 70, 70, null);
    g.dispose();   
    // get the cursor for this button
    Cursor cursor =
            Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
    ImageIcon iconRollover = new ImageIcon(bi);
    ImageIcon iconDefault = new ImageIcon(bi);
    ImageIcon iconPressed = new ImageIcon(bi);

    // create the button
    JButton button = new JButton();
    //button.addActionListener(this);
    button.setIgnoreRepaint(true);
    button.setFocusable(false);
    button.setToolTipText(toolTip);
    button.setBorder(null);
    button.setContentAreaFilled(false);
    button.setCursor(cursor);
    button.setIcon(iconDefault);
    button.setRolloverIcon(iconRollover);
    button.setPressedIcon(iconPressed);
    return button;
  }
于 2012-07-18T12:29:29.993 に答える