質問編集履歴

2

根本的な問題は変えずに、別の解決方法を考えたので、実現する方法を質問

2017/03/22 06:39

投稿

otftrough
otftrough

スコア476

test CHANGED
@@ -1 +1 @@
1
- paintComponentが呼び出されるタイミングがわからな
1
+ repaint()じゃダメみた
test CHANGED
@@ -1,269 +1,3 @@
1
- JSliderをクリックてい間画像て、クリックを離すと画像がま表示されるようにしたす。
1
+ JLabelに表示させた画像(透過png)を、swingのTimerで徐々に透明にたりすアニメーション作りたいんですが、前のコマで表示画像がその表示されて、半透明な画像がずっと重なっていきます。
2
2
 
3
-
4
-
5
- mousePressed()でJLabel#setIcon(null)にして画像を消し、
6
-
7
- mouseReleased()でJLabel#setIcon(icon)でもういちど表示させるようにしています。
8
-
9
-
10
-
11
- なぜか、JLabelのpaintComponent()が、mousePressed()の時に実行されず、mouseReleased()のあとにしか実行されません。
12
-
13
- ウスボタンしている間画像は消えずに表示さっぱなしになってします。
3
+ の画像してから不透明度を下げた画像を再描画するにどうすいで
14
-
15
-
16
-
17
- どうすればいいでしょうか。
18
-
19
-
20
-
21
- 一応参考に、実際書いている文です。変数名わかりにくいのは個人なのでごめんなさい。
22
-
23
- ```java
24
-
25
- private class MyLabel extends JLabel{
26
-
27
-
28
-
29
- public String command;
30
-
31
- }
32
-
33
-
34
-
35
- public class FLabel extends MyLabel{
36
-
37
-
38
-
39
- private Image img;
40
-
41
- public int alpha; //0~255 volatile?
42
-
43
- public int iwidth;
44
-
45
- public int iheight;
46
-
47
- private boolean size;
48
-
49
-
50
-
51
- public FLabel(){}
52
-
53
- public FLabel(String str){ this.setText(str); }
54
-
55
- public FLabel(ImageIcon icon){
56
-
57
-
58
-
59
- setIconAndImage(icon);
60
-
61
- }
62
-
63
-
64
-
65
- public void setIconAndImage(ImageIcon icon){System.out.println("setIconAndImage(" + icon + ")");
66
-
67
-
68
-
69
- setIcon(icon);
70
-
71
- if(icon != null) img = icon.getImage();
72
-
73
- else img = null;
74
-
75
- }
76
-
77
-
78
-
79
- public void setIconSize(int w, int h){
80
-
81
-
82
-
83
- iwidth = w;
84
-
85
- iheight = h;
86
-
87
- size = true;
88
-
89
- }
90
-
91
-
92
-
93
- @Override
94
-
95
- public void paintComponent(Graphics g){System.out.println("paintComponent");
96
-
97
-
98
-
99
- Graphics2D g2 = (Graphics2D)g;
100
-
101
- if(img != null){
102
-
103
-
104
-
105
- g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)alpha / (float)255));
106
-
107
-
108
-
109
- if(!size) g2.drawImage(img, 0, 0, getIcon().getIconWidth(), getIcon().getIconHeight(), this);
110
-
111
- else if(iwidth > 0 || iheight > 0){
112
-
113
-
114
-
115
- if(iwidth <= 0) iwidth = (int)(getIcon().getIconWidth() * ((double)iheight / (double)getIcon().getIconHeight()));
116
-
117
- else if(iheight <= 0) iheight = (int)(getIcon().getIconHeight() * ((double)iwidth / (double)getIcon().getIconWidth()));
118
-
119
- if(iwidth < getIcon().getIconWidth() || iheight < getIcon().getIconHeight())
120
-
121
- img = reSize(img, iwidth, iheight);
122
-
123
- else img = reSize(dispImage.getImage(), iwidth, iheight);
124
-
125
- g2.drawImage(img, 0, 0, iwidth, iheight, this);
126
-
127
- setBounds(getX(), getY(), iwidth, iheight);
128
-
129
- }
130
-
131
- text.requestFocus();
132
-
133
- text.moveCaretPosition(text.getText().length());
134
-
135
- }
136
-
137
- }
138
-
139
- }
140
-
141
-
142
-
143
- public class MySlider extends JSlider{
144
-
145
-
146
-
147
- public FLabel sbtn = null;
148
-
149
- public JPanel span = null;
150
-
151
- }
152
-
153
- public void mouseReleased(MouseEvent e){
154
-
155
-
156
-
157
- try{
158
-
159
-
160
-
161
- MySlider sl = (MySlider)e.getSource();
162
-
163
- sl.sbtn.alpha = sl.getValue();
164
-
165
- sl.sbtn.setIconAndImage(ii);
166
-
167
- System.out.println("alpha=" + sl.getValue());
168
-
169
- } catch(Exception ex){}
170
-
171
- }
172
-
173
- ImageIcon ii = null;
174
-
175
- public void mousePressed(MouseEvent e){
176
-
177
-
178
-
179
- try{
180
-
181
-
182
-
183
- MySlider sl = (MySlider)e.getSource();
184
-
185
- ii = (ImageIcon)sl.sbtn.getIcon();
186
-
187
- sl.sbtn.setIconAndImage(null);
188
-
189
- sl.repaint();
190
-
191
- } catch(Exception ex){}
192
-
193
- }
194
-
195
- /**********************************************************************************/
196
-
197
- JFrame f = new JFrame();
198
-
199
- //ImageFrame.add(f);
200
-
201
- //f.setTitle(act.getUser().getScreenName());
202
-
203
- //f.setIconImage(icon.getImage());
204
-
205
- f.setUndecorated(true);
206
-
207
- f.setBackground(new Color(0, 0, 0, 0f));
208
-
209
- //f.setAlwaysOnTop(true);
210
-
211
- //f.addWindowListener(new WL(saving.size() - 1));
212
-
213
-
214
-
215
- JPanel imgp = new JPanel();
216
-
217
- imgp.setLayout(new BoxLayout(imgp, BoxLayout.Y_AXIS));
218
-
219
- imgp.setBackground(new Color(0, 0, 0, 0f));
220
-
221
-
222
-
223
- MySlider sl = new MySlider();
224
-
225
- sl.setMinimum(1);
226
-
227
- sl.setMaximum(255);
228
-
229
- sl.setOrientation(SwingConstants.HORIZONTAL);
230
-
231
- sl.setValue(1);
232
-
233
- sl.addMouseListener(this);
234
-
235
- sl.span = imgp;
236
-
237
- imgp.add(sl);
238
-
239
-
240
-
241
- double a = (double)height / (double)width;
242
-
243
- double b = (double)icon.getIconHeight() / (double)icon.getIconWidth();
244
-
245
- if(a >= b) icon = new ImageIcon(reSize(icon.getImage(), width, (int)((double)icon.getIconHeight() * ((double)width / (double)icon.getIconWidth()))));
246
-
247
- else icon = new ImageIcon(reSize(icon.getImage(), (int)((double)icon.getIconWidth() * ((double)height / (double)icon.getIconHeight())), height));
248
-
249
- FLabel btn = new FLabel(icon); //前はボタンを使っていたのでその名残で変数名はbtnです
250
-
251
- btn.alpha = 1;
252
-
253
- btn.addMouseListener(this);
254
-
255
- //btn.command = "image" + (saving.size() - 1) + "@" + act.getUser().getScreenName();
256
-
257
- sl.sbtn = btn;
258
-
259
- imgp.add(btn);
260
-
261
-
262
-
263
- f.add(imgp);
264
-
265
- f.setBounds(0, 0, width, height);//画面いっぱいの大きさ
266
-
267
- f.setVisible(true);
268
-
269
- ```

1

文章を簡単にした

2017/03/22 06:39

投稿

otftrough
otftrough

スコア476

test CHANGED
File without changes
test CHANGED
@@ -1,8 +1,10 @@
1
- JSliderにmouseListener追加して、mousePressed()mouseReleased()そぞれ、JLabelに対する処理を書ています。
1
+ JSliderをクリックしている間画像を消してクリックを離す画像がまた表示さるようしたす。
2
+
3
+
2
4
 
3
5
  mousePressed()でJLabel#setIcon(null)にして画像を消し、
4
6
 
5
- mouseReleased()でJLabel#setIcon(icon)でもういちど表示させることが目的です。
7
+ mouseReleased()でJLabel#setIcon(icon)でもういちど表示させるようにしています。
6
8
 
7
9
 
8
10