Skip to main content

BusyPainter - not centered

1 reply [Last post]
kvaso
Offline
Joined: 2010-12-30
Points: 0

Hi
I've encountered a strange problem - I took code from this blog post: http://weblogs.java.net/blog/alexfromsun/archive/2008/07/jxlayer_30_lock... - to be specific I took LockableDemo class that is a part of this blog post
All I did was that I removed all LockableUI except of BusyPainter - also a compilation error rised at line:
private BusyPainter<JComponent> busyPainter;
(and then on the line with BusyPainter constructor)

type org.jdesktop.swingx.painter.BusyPainter does not take parameters

So I simply removed generics and it is compilable now.
However it is not wotking properly: method doPaint, that is overriden in LockableDemo class is never called and therefore busy animation is not centered.

Do you know how to solve this problem?
I am also attaching java code I am running

Thanks a lot

<jcomponent><jcomponent><jcomponent><jcomponent><jcomponent><jcomponent><br />
<br />
import org.jdesktop.jxlayer.JXLayer;<br />
import org.jdesktop.jxlayer.plaf.effect.LayerEffect;<br />
import org.jdesktop.jxlayer.plaf.ext.LockableUI;<br />
import org.jdesktop.swingx.painter.BusyPainter;<br />
<br />
<code>
import javax.swing.AbstractAction;<br />
import javax.swing.AbstractButton;<br />
import javax.swing.BorderFactory;<br />
import javax.swing.Box;<br />
import javax.swing.ButtonGroup;<br />
import javax.swing.JButton;<br />
import javax.swing.JCheckBox;<br />
import javax.swing.JCheckBoxMenuItem;<br />
import javax.swing.JComponent;<br />
import javax.swing.JFrame;<br />
import javax.swing.JMenu;<br />
import javax.swing.JMenuBar;<br />
import javax.swing.JOptionPane;<br />
import javax.swing.JPanel;<br />
import javax.swing.JRadioButton;<br />
import javax.swing.JRadioButtonMenuItem;<br />
import javax.swing.JTextField;<br />
import javax.swing.KeyStroke;<br />
import javax.swing.SwingUtilities;<br />
import javax.swing.Timer;<br />
import java.awt.BorderLayout;<br />
import java.awt.Cursor;<br />
import java.awt.FlowLayout;<br />
import java.awt.Graphics2D;<br />
import java.awt.GridBagLayout;<br />
import java.awt.Rectangle;<br />
import java.awt.event.ActionEvent;<br />
import java.awt.event.ActionListener;<br />
import java.awt.event.InputEvent;<br />
import java.awt.event.ItemEvent;<br />
import java.awt.event.ItemListener;<br />
import java.awt.event.KeyEvent;<br />
import java.awt.geom.Ellipse2D;<br />
<br />
/**<br />
* A demo to show the abilities of the {@link LockableUI}.<br />
*<br />
* @see BusyPainterUI<br />
*/<br />
public class LockableDemo extends JFrame {<br />
private JXLayer&lt;JComponent&gt; layer;<br />
private LockableUI busyPainterUI = new BusyPainterUI();<br />
<br />
private JCheckBoxMenuItem disablingItem =<br />
new JCheckBoxMenuItem(new AbstractAction(&quot;Lock the layer&quot;) {<br />
<br />
public void actionPerformed(ActionEvent e) {<br />
busyPainterItem.setEnabled(! disablingItem.isSelected());<br />
<br />
busyPainterUI.setLocked(disablingItem.isSelected());<br />
}<br />
});<br />
<br />
private JRadioButtonMenuItem busyPainterItem = new JRadioButtonMenuItem(&quot;BusyPainter&quot;);<br />
<br />
public LockableDemo() {<br />
super(&quot;Lockable layer demo&quot;);<br />
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br />
JComponent view = createLayerPanel();<br />
layer = new JXLayer&lt;JComponent&gt;(view);<br />
<br />
layer.setUI(busyPainterUI);<br />
add(layer);<br />
add(createToolPanel(), BorderLayout.EAST);<br />
setJMenuBar(createMenuBar());<br />
setSize(380, 300);<br />
setLocationRelativeTo(null);<br />
}<br />
<br />
public static void main(String[] args) throws Exception {<br />
SwingUtilities.invokeLater(new Runnable() {<br />
public void run() {<br />
new LockableDemo().setVisible(true);<br />
}<br />
});<br />
}<br />
<br />
private JMenuBar createMenuBar() {<br />
JMenu menu = new JMenu(&quot;Menu&quot;);<br />
<br />
disablingItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.ALT_MASK));<br />
<br />
menu.add(disablingItem);<br />
menu.addSeparator();<br />
<br />
<br />
busyPainterItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3, InputEvent.ALT_MASK));<br />
menu.add(busyPainterItem);<br />
<br />
ButtonGroup group = new ButtonGroup();<br />
group.add(busyPainterItem);<br />
<br />
ItemListener menuListener = new ItemListener() {<br />
public void itemStateChanged(ItemEvent e) {<br />
if (busyPainterItem.isSelected()) {<br />
layer.setUI(busyPainterUI);<br />
}<br />
}<br />
};<br />
<br />
busyPainterItem.addItemListener(menuListener);<br />
<br />
JMenuBar bar = new JMenuBar();<br />
bar.add(menu);<br />
<br />
return bar;<br />
}<br />
<br />
private JComponent createLayerPanel() {<br />
JComponent panel = new JPanel();<br />
panel.add(new JCheckBox(&quot;JCheckBox&quot;));<br />
panel.add(new JRadioButton(&quot;JRadioButton&quot;));<br />
panel.add(new JTextField(15));<br />
JButton button = new JButton(&quot;Have a nice day&quot;);<br />
button.setMnemonic('H');<br />
button.addActionListener(new ActionListener() {<br />
public void actionPerformed(ActionEvent e) {<br />
JOptionPane.showMessageDialog(LockableDemo.this,<br />
&quot;actionPerformed&quot;);<br />
}<br />
});<br />
panel.add(button);<br />
panel.add(new JTextField(15));<br />
panel.add(new JCheckBox(&quot;JCheckBox&quot;));<br />
panel.add(new JRadioButton(&quot;JRadioButton&quot;));<br />
panel.add(new JTextField(15));<br />
panel.add(new JCheckBox(&quot;JCheckBox&quot;));<br />
panel.add(new JRadioButton(&quot;JRadioButton&quot;));<br />
panel.setBorder(BorderFactory.createEtchedBorder());<br />
return panel;<br />
}<br />
<br />
private JComponent createToolPanel() {<br />
JComponent box = Box.createVerticalBox();<br />
JCheckBox button = new JCheckBox(disablingItem.getText());<br />
button.setModel(disablingItem.getModel());<br />
box.add(Box.createGlue());<br />
box.add(button);<br />
box.add(Box.createGlue());<br />
JRadioButton translucent = new JRadioButton(busyPainterItem.getText());<br />
translucent.setModel(busyPainterItem.getModel());<br />
box.add(translucent);<br />
box.add(Box.createGlue());<br />
return box;<br />
}<br />
<br />
<br />
/**<br />
* Subclass of the {@link LockableUI} which shows a button<br />
* that allows to unlock the {@link JXLayer} when it is locked<br />
*/<br />
public static class EnhancedLockableUI extends LockableUI {<br />
private JButton unlockButton = new JButton(&quot;Unlock&quot;);<br />
<br />
public EnhancedLockableUI(LayerEffect... lockedEffects) {<br />
super(lockedEffects);<br />
unlockButton.addActionListener(new ActionListener() {<br />
public void actionPerformed(ActionEvent e) {<br />
setLocked(false);<br />
}<br />
});<br />
unlockButton.setVisible(false);<br />
}<br />
<br />
public AbstractButton getUnlockButton() {<br />
return unlockButton;<br />
}<br />
<br />
@Override<br />
@SuppressWarnings(&quot;unchecked&quot;)<br />
public void installUI(JComponent c) {<br />
super.installUI(c);<br />
JXLayer&lt;JComponent&gt; l = (JXLayer&lt;JComponent&gt;) c;<br />
l.getGlassPane().setLayout(new GridBagLayout());<br />
l.getGlassPane().add(unlockButton);<br />
unlockButton.setCursor(Cursor.getDefaultCursor());<br />
}<br />
<br />
@Override<br />
@SuppressWarnings(&quot;unchecked&quot;)<br />
public void uninstallUI(JComponent c) {<br />
super.uninstallUI(c);<br />
JXLayer&lt;JComponent&gt; l = (JXLayer&lt;JComponent&gt;) c;<br />
l.getGlassPane().setLayout(new FlowLayout());<br />
l.getGlassPane().remove(unlockButton);<br />
}<br />
<br />
public void setLocked(boolean isLocked) {<br />
super.setLocked(isLocked);<br />
unlockButton.setVisible(isLocked);<br />
}<br />
}<br />
<br />
/**<br />
* Subclass of the {@link LockableUI} which uses the {@link BusyPainterUI}<br />
* from the SwingX project to implement the &quot;busy effect&quot;<br />
* when {@link JXLayer} is locked.<br />
*/<br />
public static class BusyPainterUI extends LockableUI<br />
implements ActionListener {<br />
private BusyPainter busyPainter;<br />
private Timer timer;<br />
private int frameNumber;<br />
<br />
public BusyPainterUI() {<br />
busyPainter = new BusyPainter() {<br />
protected void doPaint(Graphics2D g, JComponent object,<br />
int width, int height) {<br />
System.out.println(&quot;print me!&quot;);      //------------------------never printed<br />
// centralize the effect<br />
Rectangle r = getTrajectory().getBounds();<br />
int tw = width - r.width - 2 * r.x;<br />
int th = height - r.height - 2 * r.y;<br />
g.translate(tw / 2, th / 2);<br />
super.doPaint(g, object, width, height);<br />
}<br />
};<br />
busyPainter.setPointShape(new Ellipse2D.Double(0, 0, 20, 20));<br />
busyPainter.setTrajectory(new Ellipse2D.Double(0, 0, 100, 100));<br />
timer = new Timer(100, this);<br />
}<br />
<br />
@Override<br />
protected void paintLayer(Graphics2D g2, JXLayer&lt;? extends JComponent&gt; l) {<br />
super.paintLayer(g2, l);<br />
if (isLocked()) {<br />
busyPainter.paint(g2, l, l.getWidth(), l.getHeight());<br />
}<br />
}<br />
<br />
@Override<br />
public void setLocked(boolean isLocked) {<br />
super.setLocked(isLocked);<br />
if (isLocked) {<br />
timer.start();<br />
} else {<br />
timer.stop();<br />
}<br />
}<br />
<br />
// Change the frame for the busyPainter<br />
// and mark BusyPainterUI as dirty<br />
public void actionPerformed(ActionEvent e) {<br />
frameNumber = (frameNumber + 1) % 8;<br />
busyPainter.setFrame(frameNumber);<br />
// this will repaint the layer<br />
setDirty(true);<br />
}<br />
}<br />
}<br />
  

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kvaso
Offline
Joined: 2010-12-30
Points: 0

Ok, I figured it out :)
simply rewrite method paintLayer:

@Override

  protected void paintLayer(Graphics2D g2, JXLayer&lt;? extends JComponent&gt; l) {<br />
super.paintLayer(g2, l);<br />
if (isLocked()) {
//centering<br />
Rectangle r = busyPainter.getTrajectory().getBounds();<br />
int tw = l.getWidth() - r.width - 2 * r.x;<br />
int th = l.getHeight() - r.height - 2 * r.y;<br />
g2.translate(tw / 2, th / 2);
   //paint<br />
busyPainter.paint(g2, l, l.getWidth(), l.getHeight());<br />
}<br />
        <br />

<p> </p>