Skip to main content

JXLayer painting issue

No replies
snoopygee
Offline
Joined: 2010-01-13
Points: 0

Hi All,
I am experiencing painting issue when changing the size of the components (and therefore the scaling ratio).
Using this small example (below) you can see that when the application starts, it's initial size is 800x600 and the JFrame is not painting correctly, however when reszing the JFrame (by dragging the window) to a resolution approx 450x400, painting seems to start accuring correctly.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import org.jdesktop.jxlayer.JXLayer;
import org.pbjar.jxlayer.demo.QualityHints;
import org.pbjar.jxlayer.demo.TransformUtils;

public class TestSmallScale extends JFrame {
private final static String[] COMBO_ITEMS = {"One", "Two", "Three" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
private final static Dimension SCREEN_DIM = new Dimension(800, 600);

private JXLayer<JPanel> _theJXLayer = null;
private JPanel _theOuterPanel = new JPanel();
private JPanel _theInnerPanel = new JPanel();
private JButton _theOuterButton = new JButton("Outer Button"); //$NON-NLS-1$
private JButton _theInnerButton = new JButton("Inner button"); //$NON-NLS-1$
private JComboBox _theCombo = new JComboBox();

public TestSmallScale(){

doLayoutComponents();

this.setSize( SCREEN_DIM );
this.setPreferredSize( SCREEN_DIM );
//this.setMinimumSize( SCREEN_DIM );

this.addComponentListener( new ComponentAdapter(){
public void componentResized(ComponentEvent e) {
System.out.println( "Size: " + TestSmallScale.this.getWidth() + "x" + TestSmallScale.this.getHeight() ); //$NON-NLS-1$ //$NON-NLS-2$
}
});
}

private void doLayoutComponents(){

this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
this.setLayout( new BorderLayout() );

_theJXLayer = createTransformJXLayer( _theOuterPanel, 1 );
this.add( _theJXLayer, BorderLayout.CENTER );

_theInnerPanel.setBackground( Color.BLUE );
_theInnerPanel.setLayout( new FlowLayout() );
_theInnerPanel.add( _theInnerButton );
_theInnerPanel.add( createTable() );

_theInnerPanel.setSize( new Dimension(400, 400) );
_theInnerButton.setSize( new Dimension(60, 20) );

_theOuterPanel.setBackground( Color.RED );
_theOuterPanel.setLayout( new BorderLayout() );
_theOuterPanel.add( _theCombo, BorderLayout.NORTH );
_theOuterPanel.add( _theInnerPanel, BorderLayout.CENTER );
_theOuterPanel.add( _theOuterButton, BorderLayout.SOUTH );

_theOuterButton.setPreferredSize( new Dimension(100, 20) );
_theOuterButton.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent evt ){
onButtonClick();
}
});
_theCombo.setPreferredSize( new Dimension(100, 20) );

for ( String theItem : COMBO_ITEMS ){
_theCombo.addItem( theItem );
}
}

private JScrollPane createTable(){

JScrollPane theScroll = new JScrollPane();

JTable theTable = new JTable();
TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() { return 10; }
public int getRowCount() { return 10;}
public Object getValueAt(int row, int col) { return new Integer(row*col); }
};

theTable.setModel( dataModel );
theScroll.getViewport().add( theTable );
theScroll.setSize( new Dimension(200, 200) );

return theScroll;
}

private void onButtonClick(){
_theInnerPanel.add( createTable() );
_theInnerPanel.validate();
}

public JXLayer<JPanel> createTransformJXLayer( JComponent panelToWrap, int scale ){
return (JXLayer)TransformUtils.createTransformJXLayer(panelToWrap, scale, new QualityHints());
}

public static void main( String[] args ){

SwingUtilities.invokeLater( new Runnable(){
public void run(){
TestSmallScale theApp = new TestSmallScale();

theApp.setSize( SCREEN_DIM );
Dimension theScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
theApp.setLocation( (theScreenSize.width - theApp.getWidth()) /2,
(theScreenSize.height - theApp.getHeight()) /2);
theApp.setVisible( true );
}
});
}
}
Any idea why the painting does not work correctly above a certain dimension?
Thanks
Simon