Skip to main content

Porting TabbedPaneAnimationDemo from 2.0 to 3.0

2 replies [Last post]
davide71
Offline
Joined: 2007-09-28
Points: 0

Hi,
few months ago I used JXLayer 2.0 and now I'm trying to make myself acquainted with new JXLayer 3.0 api.
I'm porting TabbedPaneAnimationDemo from 2.0 to 3.0 but I have some problems: modified code compiles correcly now but its behaviour is a bit strange.
Here is the modified code:

<br />
package org.jdesktop.jxlayer.demo;</p>
<p>import java.awt.Color;<br />
import java.awt.Graphics2D;<br />
import java.awt.event.ActionEvent;<br />
import java.awt.event.ActionListener;</p>
<p>import javax.swing.JButton;<br />
import javax.swing.JCheckBox;<br />
import javax.swing.JComponent;<br />
import javax.swing.JFrame;<br />
import javax.swing.JLabel;<br />
import javax.swing.JList;<br />
import javax.swing.JPanel;<br />
import javax.swing.JProgressBar;<br />
import javax.swing.JSlider;<br />
import javax.swing.JTabbedPane;<br />
import javax.swing.SwingUtilities;<br />
import javax.swing.Timer;<br />
import javax.swing.event.ChangeEvent;<br />
import javax.swing.event.ChangeListener;</p>
<p>import org.jdesktop.jxlayer.JXLayer;<br />
import org.jdesktop.jxlayer.demo.util.LafMenu;<br />
import org.jdesktop.jxlayer.plaf.BufferedLayerUI;</p>
<p>/**<br />
 * @author Alexander Potochkin<br />
 *<br />
 * <a href="https://swinghelper.dev.java.net/" title="https://swinghelper.dev.java.net/">https://swinghelper.dev.java.net/</a><br />
 * <a href="http://weblogs.java.net/blog/alexfromsun/" title="http://weblogs.java.net/blog/alexfromsun/">http://weblogs.java.net/blog/alexfromsun/</a><br />
 */<br />
public class TabbedPaneAnimationDemo {<br />
	public static void main(String[] args) {<br />
		SwingUtilities.invokeLater(new Runnable() {<br />
			public void run() {<br />
				createGui();<br />
			}<br />
		});<br />
	}</p>
<p>	private static void createGui() {<br />
		JFrame frame = new JFrame("TabbedPane demo");<br />
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br />
		frame.setJMenuBar(LafMenu.createMenuBar());</p>
<p>		final JTabbedPane pane = new JTabbedPane();</p>
<p>		JPanel p1 = new JPanel();<br />
		p1.add(new JButton("Hello"));<br />
		p1.add(new JButton("Hello"));<br />
		p1.add(new JButton("Hello"));<br />
		p1.add(new JButton("Hello"));<br />
		p1.add(new JButton("Hello"));<br />
		p1.add(new JButton("Hello"));<br />
		JLabel label = new JLabel("Select the next tab");<br />
		label.setFont(label.getFont().deriveFont(25f));<br />
		p1.add(label);<br />
		pane.add("One", new JXLayer(p1));</p>
<p>		JPanel p2 = new JPanel();<br />
		p2.add(new JProgressBar(0, 100));<br />
		p2.add(new JCheckBox("Hello"));<br />
		p2.add(new JCheckBox("World"));<br />
		p2.add(new JButton("lala"));<br />
		p2.add(new JButton("lala"));<br />
		p2.add(new JSlider(0, 100));<br />
		JLabel label2 = new JLabel("Do you see the animation ?");<br />
		label2.setFont(label2.getFont().deriveFont(20f));<br />
		p2.add(label2);<br />
		pane.add("Two", new JXLayer(p2));</p>
<p>		JPanel p3 = new JPanel();<br />
		String[] data = { "one", "two", "three", "four" };<br />
		p3.add(new JList(data));<br />
		p3.add(new JList(data));<br />
		p3.add(new JList(data));<br />
		JLabel label3 = new JLabel("It works !");<br />
		label3.setFont(label3.getFont().deriveFont(25f));<br />
		p3.add(label3);<br />
		label3.setForeground(Color.GREEN.darker());<br />
		pane.add("Three", new JXLayer(p3));</p>
<p>		pane.addChangeListener(new TabbedAnimatingChangeListener(50, .05f));</p>
<p>		frame.add(pane);</p>
<p>		frame.setSize(310, 230);<br />
		frame.setLocationRelativeTo(null);<br />
		frame.setVisible(true);<br />
	}</p>
<p>	static class TabbedAnimatingChangeListener implements ChangeListener {<br />
		private int index;<br />
		private Timer timer;<br />
		private BufferedLayerUI layerUI;<br />
		private float delta;</p>
<p>		public TabbedAnimatingChangeListener() {<br />
			this(100, .1f);<br />
		}</p>
<p>		public TabbedAnimatingChangeListener(int delay, final float delta) {<br />
			this.delta = delta;<br />
			layerUI = new AnimationLayerUI();</p>
<p>			timer = new Timer(delay, new ActionListener() {<br />
				public void actionPerformed(ActionEvent e) {</p>
<p>					if (layerUI.getAlpha() <= delta) {<br />
						layerUI.setAlpha(0);<br />
						timer.stop();<br />
						return;<br />
					}</p>
<p>					layerUI.setAlpha(layerUI.getAlpha() - delta);<br />
				}<br />
			});<br />
		}</p>
<p>		public float getDelta() {<br />
			return delta;<br />
		}</p>
<p>		public void setDelta(float delta) {<br />
			if (delta <= 0 || delta > 1) {<br />
				throw new IllegalArgumentException();<br />
			}<br />
			this.delta = delta;<br />
		}</p>
<p>		public int getDelay() {<br />
			return timer.getDelay();<br />
		}</p>
<p>		public void setDelay(int delay) {<br />
			timer.setDelay(delay);<br />
		}</p>
<p>		@SuppressWarnings("unchecked")<br />
		public void stateChanged(ChangeEvent e) {<br />
			JTabbedPane pane = (JTabbedPane) e.getSource();<br />
			JXLayer layer = (JXLayer) pane<br />
					.getSelectedComponent();<br />
			JXLayer oldLayer = (JXLayer) pane<br />
					.getComponentAt(index);</p>
<p>			layerUI.setAlpha(1 - layerUI.getAlpha());</p>
<p>			// set oldLayer as a foreground<br />
			//painter.setComponent(oldLayer);</p>
<p>			// swap layerUI's<br />
			oldLayer.setUI(layer.getUI());<br />
			layer.setUI(layerUI);</p>
<p>			//painter.update();<br />
			timer.start();<br />
			index = pane.getSelectedIndex();<br />
		}<br />
	}</p>
<p>	static class AnimationLayerUI extends BufferedLayerUI {<br />
		public AnimationLayerUI() {<br />
			setAlpha(0);<br />
		}</p>
<p>		@Override<br />
		protected void paintLayer(Graphics2D g2, JXLayer l) {<br />
			// paint the layer<br />
			l.paint(g2);<br />
			// paint the old layer with diminishing alpha<br />
			super.paintLayer(g2, l);<br />
		}</p>
<p>	}<br />
}<br />

Can anyone tell me where I'm wrong?
Thanks in advance

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
alexfromsun
Offline
Joined: 2005-09-05
Points: 0

Hello David

I am happy to hear that you decided to use the 3.0 version!

Here is the working TabbedPaneAnimationDemo for you:

[code]
import org.jdesktop.jxlayer.JXLayer;
import org.jdesktop.jxlayer.demo.util.LafMenu;
import org.jdesktop.jxlayer.plaf.BufferedLayerUI;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

/**
* @author Alexander Potochkin
*

* https://swinghelper.dev.java.net/
* http://weblogs.java.net/blog/alexfromsun/
*/
public class TabbedPaneAnimationDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGui();
}
});
}

private static void createGui() {
JFrame frame = new JFrame("TabbedPane demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(LafMenu.createMenuBar());

final JTabbedPane pane = new JTabbedPane();

JPanel p1 = new JPanel();
p1.add(new JButton("Hello"));
p1.add(new JButton("Hello"));
p1.add(new JButton("Hello"));
p1.add(new JButton("Hello"));
p1.add(new JButton("Hello"));
p1.add(new JButton("Hello"));
JLabel label = new JLabel("Select the next tab");
label.setFont(label.getFont().deriveFont(25f));
p1.add(label);
pane.add("One", new JXLayer(p1));

JPanel p2 = new JPanel();
p2.add(new JProgressBar(0, 100));
p2.add(new JCheckBox("Hello"));
p2.add(new JCheckBox("World"));
p2.add(new JButton("lala"));
p2.add(new JButton("lala"));
p2.add(new JSlider(0, 100));
JLabel label2 = new JLabel("Do you see the animation ?");
label2.setFont(label2.getFont().deriveFont(20f));
p2.add(label2);
pane.add("Two", new JXLayer(p2));

JPanel p3 = new JPanel();
String[] data = {"one", "two", "three", "four"};
p3.add(new JList(data));
p3.add(new JList(data));
p3.add(new JList(data));
JLabel label3 = new JLabel("It works !");
label3.setFont(label3.getFont().deriveFont(25f));
p3.add(label3);
label3.setForeground(Color.GREEN.darker());
pane.add("Three", new JXLayer(p3));

pane.addChangeListener(new TabbedAnimatingChangeListener(50, .05f));

frame.add(pane);

frame.setSize(310, 230);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

static class TabbedAnimatingChangeListener implements ChangeListener {
private int index;
private Timer timer;
private AnimationLayerUI layerUI;
private float delta;

public TabbedAnimatingChangeListener() {
this(100, .1f);
}

public TabbedAnimatingChangeListener(int delay, final float delta) {
this.delta = delta;
layerUI = new AnimationLayerUI();

timer = new Timer(delay, new ActionListener() {
public void actionPerformed(ActionEvent e) {

if (layerUI.getAlpha() <= delta) {
layerUI.setAlpha(0);
timer.stop();
return;
}

layerUI.setAlpha(layerUI.getAlpha() - delta);
}
});
}

public float getDelta() {
return delta;
}

public void setDelta(float delta) {
if (delta <= 0 || delta > 1) {
throw new IllegalArgumentException();
}
this.delta = delta;
}

public int getDelay() {
return timer.getDelay();
}

public void setDelay(int delay) {
timer.setDelay(delay);
}

@SuppressWarnings("unchecked")
public void stateChanged(ChangeEvent e) {
JTabbedPane pane = (JTabbedPane) e.getSource();
JXLayer layer = (JXLayer) pane
.getSelectedComponent();
JXLayer oldLayer = (JXLayer) pane
.getComponentAt(index);

layerUI.setAlpha(1 - layerUI.getAlpha());

// set oldLayer as a foreground
layerUI.setComponent(oldLayer);

// swap layerUI's
oldLayer.setUI(layer.getUI());
layer.setUI(layerUI);

//painter.update();
timer.start();
index = pane.getSelectedIndex();
}
}

static class AnimationLayerUI extends BufferedLayerUI {
private JComponent component;
private BufferedImage componentImage;

public AnimationLayerUI() {
setAlpha(0);
setIncrementalUpdate(false);
}

public void setComponent(JComponent component) {
this.component = component;
if (component == null
|| component.getWidth() <= 0 || component.getHeight() <= 0) {
componentImage = null;
} else {
if (componentImage == null
|| componentImage.getWidth() != component.getWidth()
|| componentImage.getHeight() != component.getHeight()) {
componentImage = createBuffer(component.getWidth(), component.getHeight());
}
Graphics g = componentImage.getGraphics();
component.paint(g);
g.dispose();
}
}

public void paint(Graphics g, JComponent c) {
// always paint the layer
c.paint(g);
super.paint(g, c);
}

protected void paintLayer(Graphics2D g2, JXLayer l) {
if (componentImage != null) {
// paint the old layer with diminishing alpha
g2.drawImage(componentImage, 0, 0, null);
}
}
}
}
[/code]

alexp

davide71
Offline
Joined: 2007-09-28
Points: 0

Thanks for that.
I was trying a similar setComponent method but it was not working propertly.
Now looking at your code I exactly know why. :)
Thanks once again.