JFrameをフェードイン・フェードアウトさせたい
JFrameを弄って、フェードしている間にJPanelを入れ替えたいと考えています。
newした時だけなぜかフェードイン後になります。
字数制限上省略しているので必要があれば補足などに追記します。
******
public class MyFrame extends JFrame {
public static final int NON_FADE = 0;
public static final int IN_FADE = 1;
public static final int FADE_OUTING = 2;
public static final int FADE_INING = 3;
private FadePane pane;
private JPanel nowPanel;
private Color fadeColor = Color.BLACK;
private int fadeTime = 500;
private int fadeStatus = NON_FADE;
private boolean fadable = true;
public MyFrame() {
pane = new FadePane();
setGlassPane(pane);
fadeOut(0, fadeColor);
}
public void fadeOut(int time, Color color) {
setFadeColor(color);
if (fadeStatus != NON_FADE) {
return;
}
fadeStatus = FADE_OUTING;
pane.fadeOut(time, color);
fadeStatus = IN_FADE;
}
public void fadeIn(int time) {
if (fadeStatus != IN_FADE) {
return;
}
fadeStatus = FADE_INING;
pane.fadeIn(time, getFadeColor());
fadeStatus = NON_FADE;
repaint();
}
public void setFadeColor(Color color) {
if (color == null) {
return;
}
if (fadeColor.equals(color)) {
return;
}
fadeColor = color;
this.getContentPane().setBackground(fadeColor);
this.getContentPane().repaint();
}
public void setPanel(JPanel panel) {
if (fadable && !(nowPanel == null)) {
fadeOut(fadeTime, fadeColor);
}
if(nowPanel!=null)this.getContentPane().remove(nowPanel);
nowPanel = panel;
if(nowPanel!=null)this.getContentPane().add(nowPanel);
nowPanel.repaint();
if (fadable && !(nowPanel == null)) {
fadeIn(fadeTime);
}
}
private class FadePane extends JComponent {
private JComponent com = this;
private Color color = Color.BLACK;
private final int renewalTime = 10;
public void fadeOut(int time, Color color) {
this.color = color;
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int alpha = color.getAlpha();
FadeListener listener = new FadeListener(true, red, green, blue, alpha, time, renewalTime, this);
Timer timer = new Timer(renewalTime, listener);
listener.setTimer(timer);
timer.start();
synchronized (this) {
try {
wait();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public void fadeIn(int time, Color color) {
this.color = color;
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int alpha = color.getAlpha();
FadeListener listener = new FadeListener(false, red, green, blue, alpha, time, renewalTime, this);
Timer timer = new Timer(renewalTime, listener);
listener.setTimer(timer);
timer.start();
synchronized (this) {
try {
wait();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public synchronized void wakeup() {
notifyAll();
}
@Override
protected void paintComponent(final Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(color);
g2.fillRect(0, 0, this.getWidth(), this.getHeight());
}
private class FadeListener implements ActionListener {
//true:フェードアウト
//false:フェードイン
final boolean mode;
final int red;
final int green;
final int blue;
final int alpha;
Timer timer;
final int allTime;
int time = 0;
final int dTime;
FadePane pane;
public FadeListener(boolean mode, int red, int green, int blue, int alpha, int time, int dTime, FadePane pane) {
this.mode = mode;
this.red = red;
this.green = green;
this.blue = blue;
this.alpha = alpha;
this.allTime = (time / dTime) + 1;
this.dTime = dTime;
this.pane = pane;
}
public void setTimer(Timer timer) {
this.timer = timer;
}
@Override
public void actionPerformed(ActionEvent e) {
if (time == 0) {
com.setVisible(!mode);
}
time++;
if (time > allTime) {
com.setVisible(mode);
timer.stop();
pane.wakeup();
return;
}
com.setVisible(true);
int nowAlpha = mode ? alpha * time / allTime : alpha - (alpha * time / allTime);
color = new Color(red, green, blue, nowAlpha);
com.repaint();
}
}
}
}