JButtonの画像をactionPerformedメソッド内で再描画したい。
以下のソースのようにして、再描画したいのです。
setIconメソッドではなく、
JButtonに対して描画したものに対して再描画したいです。
Graphics2DクラスについてJAVA APIで調べましたが、
仕組の理解に至りませんでした。
仕組みと方法を教えて頂きたいです。
よろしくお願いいたします。
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class test extends JFrame implements ActionListener{
JButton b= new JButton();
public static void main(String a[]) {
new test();
}
public test() {
super();
this.setSize(100,100);
b.addActionListener(this);
b.add(new Zoom(new ImageIcon("img1.jpg"),0,0,50,50));
this.add(b);
this.setVisible(true);
}
class Zoom extends JComponent {
private static final long serialVersionUID = 1L;
private ImageIcon icon = null;
private int x = 0;
private int y = 0;
private int h = 0;
private int w = 0;
private double scale = 1.0d;
public Zoom(ImageIcon icon, int x, int y, int w, int h) {
super();
this.icon = icon;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.scale(scale, scale);
//////////////////////////////////
//画僧を再描画したい。
//g2.clearRect(0, 0, 80, 80);//×
g2.drawImage(icon.getImage(), x, y, w, h, this);
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b){
System.out.print("ok");
//this.repaint();//×
//b.repaint();//×
b.add(new Zoom(new ImageIcon("img2.jpg"),0,0,50,50));//(再描画できない)
//b.setIcon(new ImageIcon("img2.jpg"));//ok(再描画出来る)
}
}
}
No.2ベストアンサー
- 回答日時:
#1です。
今回の問題は、再描画されていないのではなく、JButtonのデフォルトのレイアウトが OverlayLayout であるため、一つ目の Zoom コンポーネントと、二つ目の Zoom コンポーネントが重なってしまい、二つ目が見えていないだけです。
ちゃんと、一つ目を remove したあとに、add してやれば、OKです。
表示後のコンポーネントの追加と削除の後は、validate もお忘れなく。
ただし、ボタンが押されるたびに、新しいZoomのインスタンスが作られ、そこで、ImageIcon が読み込まれるなど、無駄があったりするので、改善した方がよいと思います。
public class test extends JFrame implements ActionListener {
JButton b = new JButton("test");
public static void main(String a[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new test();
}
});
}
public test() {
super();
this.setSize(100, 100);
b.addActionListener(this);
b.add(new Zoom(new ImageIcon("img1.jpg"), 0, 0, 50, 50));
this.add(b);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
class Zoom extends JComponent {
private static final long serialVersionUID = 1L;
private ImageIcon icon = null;
private int x = 0;
private int y = 0;
private int h = 0;
private int w = 0;
private double scale = 1.0d;
public Zoom(ImageIcon icon, int x, int y, int w, int h) {
super();
this.icon = icon;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
Dimension d = new Dimension(w, h);
this.setMinimumSize(d);
this.setPreferredSize(d);
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.scale(scale, scale);
//////////////////////////////////
//画僧を再描画したい。
//g2.clearRect(0, 0, 80, 80);//×
g2.drawImage(icon.getImage(), x, y, w, h, this);
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b) {
System.out.println("ok");
//this.repaint();//×
//b.repaint();//×
b.removeAll();
b.add(new Zoom(new ImageIcon("img2.jpg"), 0, 0, 50, 50));
b.validate();
//(再描画できない)
//b.setIcon(new ImageIcon("img2.jpg"));//ok(再描画出来る)
}
}
}
お返事遅くなりすみません。
とても参考になりました。
描画処理の中身を理解出来ました。
細かなソースの補足もしていただきありがとうございます。
No.1
- 回答日時:
こんにちは。
最終的に、どのようになるのがお望みですか。
1.img1が消え、img2に差し変わる。
2.img1、img2ともに表示されるようになる。
補足をお願いします。
この回答への補足
ご回答ありがとうございます。
目標は、
1.img1が消え、img2に差し変わる。
です。
描画したものを消して、書き直す作業が必要です。
setVisible(false);
してnewすると書き換えられますが。
そのようにすると、フレームのメモリが残ってしまいますので、
そうはしたくありません。
よろしくお願いいたします。
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- Java java 入力 3 4 3 出力 ABC DEFG HIJ このようなプログラムの書き方を教えてくだ 2 2022/07/15 14:18
- Ruby 【JAVA】数字をひし形に出力するプログラムについて 2 2022/07/11 23:32
- C言語・C++・C# 大量のデータを読み込んで表示する速度を改善したい 8 2023/05/07 13:29
- Java Java プログラム public class Main { public static void 3 2023/08/10 23:46
- C言語・C++・C# C# DatagridviewにExcelシートを反映するとエラーが出る 2 2023/05/06 17:12
- C言語・C++・C# C#テキストボックスの文字を配列にいれてその後表示する 4 2022/07/17 04:47
- Java java final 1 2022/06/10 22:49
- Java JavaのSingletonパターンのprivateの持つ意味が分かりません。 5 2022/06/12 10:38
- Java javaでのプログラム(配列)について質問です. 2 2022/10/14 22:27
- C言語・C++・C# 質問です 下記のコードを分かりやすく解説お願いします 初心者です #include ‹stdio.h 3 2022/05/26 22:03
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
C# 矢印キーの取得
-
正規表現
-
Visual C♯ フォーム間での値の...
-
アプレットでマウスイベントが...
-
コンボボックスを使う時の警告
-
エンターキーを押すとOKボタン...
-
C# 半角カナの文字化けについて
-
ボタンの複数割り当てについて
-
配列の受け渡し
-
大量のデータを読み込んで表示...
-
JavaプログラムのUML化
-
パスがとおらない・・・
-
C#でのWNetAddConnection3の使...
-
Thread.sleepのInterruptedExce...
-
Processingでマウスクリックで...
-
キーリピート?(Javaプログラミ...
-
フェードアウト時にぶちっと画...
-
C#で、あるクラスのメンバーす...
-
長方形をドラッグするJavaアッ...
-
Javaの課題について質問です。...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
大量のデータを読み込んで表示...
-
ボタンの複数割り当てについて
-
C#で、あるクラスのメンバーす...
-
Junitテストでvoid戻り値メッソ...
-
C#でのWNetAddConnection3の使...
-
C# 矢印キーの取得
-
Thread.sleepのInterruptedExce...
-
unityでのC++エラーの原因がわ...
-
C#で別のFormへ複数の値を返そ...
-
C# visibleプロパティをfalseに...
-
エンターキーを押すとOKボタン...
-
Processingでマウスクリックで...
-
C#でキーイベントが発生しない...
-
[C#.net]スレッド化された別フ...
-
C# DataGridView列カスタマイズ
-
【C#】ソースコードをファイル...
-
LVM_SETITEMSTATEでListViewの...
-
GetDIBits関数の使い方について
-
未割り当てのローカル変数
-
C# MouseHoverを何度も呼ぶには
おすすめ情報