JLabelを継承したクラスを作って、paintComponent()の中でラベルに表示する画像の大きさを計算させています。
具体的な使い方は、
java
1FLabel label = new FLabel(); //JLabelを継承させたクラス 2label.alpha = 255; 3 4label.setIconAndImage(new ImageIcon("pict.jpg")); //setIcon()のかわり 5label.setIconSize(500, 500); //表示するサイズ
という感じです。
普通に使えば問題なく表示されますが、
setIconSize(int w, int h)を使ったとき、どちらかの値が1未満だとpaintComponent()が呼び出されません。
困ります。
なぜでしょうか?
下記 FLabelクラス
java
1 public class FLabel extends JLabel{ 2 3 private Image img; 4 public int alpha; //表示する画像の不透明度 5 public int iwidth; //画像のwidth 6 public int iheight; //画像のheight 7 //int widthと int heightは既に存在していたので、変数名を変えました。 8 private boolean size; 9 10 public FLabel(){} 11 public FLabel(String str){ this.setText(str); } 12 public FLabel(ImageIcon icon){ 13 14 setIconAndImage(icon); 15 } 16 17 public void setIconAndImage(ImageIcon icon){ 18 19 setIcon(icon); 20 //あ、もしかしてsetIcon()の中身に書けば良かった? 21 if(icon != null) img = icon.getImage(); 22 else img = null; 23 } 24 25 public void setIconSize(int w, int h){ 26 27 iwidth = w; 28 iheight = h; 29 size = true; 30 } 31 32 @Override 33 public void paintComponent(Graphics g){System.out.println("paintComponent"); 34 System.out.println("paintComponentが呼ばれたよ!"); 35 36 Graphics2D g2 = (Graphics2D)g; 37 if(img != null){ 38 39 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)alpha / (float)255)); 40 41 //サイズ変更がない場合 42 if(!size) g2.drawImage(img, 0, 0, getIcon().getIconWidth(), getIcon().getIconHeight(), this); 43 44 //サイズ変更がある場合 45 else if(iwidth > 0 || iheight > 0){ 46 47 //iwidthまたはiheightが0以下なら、画像の比率をそのままで拡大or縮小 48 if(iwidth <= 0) iwidth = (int)(getIcon().getIconWidth() * ((double)iheight / (double)getIcon().getIconHeight())); 49 else if(iheight <= 0) iheight = (int)(getIcon().getIconHeight() * ((double)iwidth / (double)getIcon().getIconWidth())); 50 g2.drawImage(img, 0, 0, iwidth, iheight, this); 51 } 52 } 53 } 54 }
追記:
実際に作っているプログラムの一部です。
setIconSize(w, h);
のあとにsetBounds();
を使っているのでrepaintされていると思います。
念のためsetBoundsのあとにrepaint()
を入れてみてもなにも変わりませんでした。
wとh両方が自然数ならば正常に動きますが、100%毎回ではないようです。
どちらか片方、又は両方が0以下なら100%paintComponentは呼ばれません。
java
1JTextArea text; 2ImageIcon dispImage; 3FLabel imgLabel; 4/*****************************************************/ 5if(text.getText().startsWith("image:")){ //画像 6 7 String str = text.getText().substring(6); 8 if(!str.equals("null")){ 9 10 str = "img/" + str; 11 File img = new File(str); 12 if(img.exists()){ 13 14 dispImage = new ImageIcon(str); 15 imgLabel.setIconAndImage(dispImage); 16 if(!imgLabel.size) imgLabel.setBounds(50, 50, dispImage.getIconWidth(), dispImage.getIconHeight()); 17 else imgLabel.setBounds(50, 50, imgLabel.iwidth, imgLabel.iheight); 18 } else{ 19 20 text.setText("\"" + str + "\" is not found!"); 21 } 22 } else if(imgLabel.getIcon() != null){ 23 24 imgLabel.setIconAndImage(null); 25 dispImage = null; 26 } 27} else if(text.getText().startsWith("image.")){ 28 29 String txt = text.getText().substring(6); 30 if(txt.startsWith("position:")){ 31 32 txt = txt.substring(9); 33 int w = number(txt); 34 txt = txt.substring(new String(w + "").length() + 1); 35 int h = number(txt); 36 //wとhには幅と高さの整数が入っています。 37 imgLabel.setBounds(w - imgLabel.getWidth() / 2, h - imgLabel.getHeight() / 2, imgLabel.getWidth(), imgLabel.getHeight()); 38 }else if(txt.startsWith("size:")){ //表示サイズの指定 39 40 txt = txt.substring(5); 41 int w = number(txt); 42 txt = txt.substring(new String(w + "").length() + 1); 43 int h = number(txt); 44 //wとhには幅と高さの整数が入っています。 45 46 imgLabel.setIconSize(w, h); 47 imgLabel.setBounds(w - imgLabel.getWidth() / 2, h - imgLabel.getHeight() / 2, imgLabel.iwidth, imgLabel.iheight); 48 imgLabel.setIconAndImage(dispImage); 49 } else if(txt.equals("bounds")){ 50 51 text.setText("X:" + (imgLabel.getX() + imgLabel.getWidth() / 2) + ", Y:" + (imgLabel.getY() + imgLabel.getHeight() / 2) + ", width:" + imgLabel.getWidth() + ", height:" + imgLabel.getHeight()); 52 } 53}
回答1件
あなたの回答
tips
プレビュー