|
|
|
|
Java 2D & Swing multilayer problem
Posted:
Jan 12, 2009 4:02 PM
|
|
|
Hi,
I did not find better place to ask.
I am learning creating Swing GUI + Java 2D. I need to have a background image on top of which I need to draw figures and place smaller images (using Java 2D I suppose). All of this needs to recognize when clicked by mouse, but this is far future now.
Right now I am trying to experiment with layers. I implemented layers example from Sun tutorials and it works fine but when I try to add my own draws from Java 2D as e.g. top layer they don't show.
In the below I have my own polygone class that I used to draw polygone on simple JPanel and it worked. Now, when try to do the same on layeredPane it fails to do anything.
Any hint on what I'm doing wrong will be appreciated.
Here's the code:
import javax.swing.*; import javax.swing.border.*; import javax.accessibility.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.AffineTransform; import java.awt.geom.GeneralPath; import java.awt.image.BufferedImage; /* * RootLayeredPaneDemo.java requires images/dukeWaveRed.gif. */ public class RootLayeredPaneDemo extends JPanel implements ActionListener,MouseMotionListener { private int[] layers = {-3, 0, 3}; private String[] layerStrings = { "Yellow (-30000)", "Magenta (0)", "Cyan (301)" }; private Color[] layerColors = { Color.yellow, Color.magenta, Color.cyan }; private JLayeredPane layeredPane; private JLabel dukeLabel; private JCheckBox onTop; private JComboBox layerList; //Action commands private static String ON_TOP_COMMAND = "ontop"; private static String LAYER_COMMAND = "layer"; //Adjustments to put Duke's toe at the cursor's tip. private static final int XFUDGE = 40; private static final int YFUDGE = 57; //Initial layer of dukeLabel. private static final int INITIAL_DUKE_LAYER_INDEX = 1; public RootLayeredPaneDemo(JLayeredPane layeredPane) { super(new GridLayout(1,1)); //Create and load the duke icon. final ImageIcon icon = createImageIcon("images/dukeWaveRed.gif"); //Create and set up the layered pane. this.layeredPane = layeredPane; layeredPane.addMouseMotionListener(this); //This is the origin of the first label added. Point origin = new Point(10, 100); //This is the offset for computing the origin for the next label. int offset = 35; //Add several overlapping, colored labels to the layered pane //using absolute positioning/sizing. for (int i = 0; i < layerStrings.length; i++) { JLabel label = createColoredLabel(layerStrings]]]]]]]]]]]]]
|
|
|
|
|
|
|
Re: Java 2D & Swing multilayer problem
Posted:
Jan 12, 2009 4:06 PM
in response to: r1omen
|
|
|
Is there any limit to text displayed? It looked ok when I pasted it but I see most of it cut off.
|
|
|
|
|
|
|
|
Re: Java 2D & Swing multilayer problem
Posted:
Jan 12, 2009 4:15 PM
in response to: r1omen
|
|
|
In general, less code you post equals higher chance the forum readers will look into it. My rule of thumb is that anything above 100 lines stands minimal chances of getting a response.
Your original code was 220 lines. You'll need to bring it to the absolute minimum necessary to reproduce the problem. That includes removing unnecessary Java2D operations (no need to draw a dashed polygon, one line is enough to show the problem), removing usage of external images (duke), colored labels, control panel and any mouse interaction (that you state yourself is for the future).
If the problem is that your custom layer doesn't show, this is what the example should be. No need for multiple drawings on that layer. One is enough to show the problem.
Kirill
|
|
|
|
|
|
|
|
Re: Java 2D & Swing multilayer problem
Posted:
Jan 13, 2009 1:32 PM
in response to: kirillcool
|
|
|
Everything Kirillcool said PLUS
Please use the code tags!
[\code]your java code here [\/code] Remove backslashes to make code. (just how does one escape bbcode in bbcode?)
e.g.
import javax.swing.*;
import javax.swing.border.*;
import javax.accessibility.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
/*
* RootLayeredPaneDemo.java requires images/dukeWaveRed.gif.
*/
public class RootLayeredPaneDemo extends JPanel
implements ActionListener, MouseMotionListener {
private int[] layers = {-3, 0, 3};
private String[] layerStrings = {"Yellow (-30000)",
"Magenta (0)",
"Cyan (301)"};
private Color[] layerColors = {Color.yellow,
Color.magenta,
Color.cyan};
private JLayeredPane layeredPane;
private JLabel dukeLabel;
private JCheckBox onTop;
private JComboBox layerList;
//Action commands
private static String ON_TOP_COMMAND = "ontop";
private static String LAYER_COMMAND = "layer";
//Adjustments to put Duke's toe at the cursor's tip.
private static final int XFUDGE = 40;
private static final int YFUDGE = 57;
//Initial layer of dukeLabel.
private static final int INITIAL_DUKE_LAYER_INDEX = 1;
public RootLayeredPaneDemo(JLayeredPane layeredPane) {
super(new GridLayout(1, 1));
//Create and load the duke icon.
final ImageIcon icon = createImageIcon("images/dukeWaveRed.gif");
//Create and set up the layered pane.
this.layeredPane = layeredPane;
layeredPane.addMouseMotionListener(this);
//This is the origin of the first label added.
Point origin = new Point(10, 100);
//This is the offset for computing the origin for the next label.
int offset = 35;
//Add several overlapping, colored labels to the layered pane
//using absolute positioning/sizing.
for (int i = 0; i < layerStrings.length; i++) {
JLabel label = createColoredLabel(layerStrings
]]]]]]]]]]]]]
|
|
|
|
|
|
|
|
Re: Java 2D & Swing multilayer problem
Posted:
Jan 13, 2009 2:33 AM
in response to: r1omen
|
|
|
I thoughg I just provide full class body for copy/paste.
Here is essence - I have no idea why I can add label, but polygone not.
public class RootLayeredPaneDemo extends JPanel{
public RootLayeredPaneDemo(JLayeredPane layeredPane){ ... layeredPane.add(label, new Integer(layers)); //this works polygone p = new polygone(); layeredPane.add(p,new Integer(layers[2]),-1); //does not work ... }
...
private static void createAndShowGUI() {
JFrame frame = new JFrame("RootLayeredPaneDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RootLayeredPaneDemo newContentPane = new RootLayeredPaneDemo(frame.getLayeredPane()); newContentPane.setOpaque(true); frame.setContentPane(newContentPane); frame.setVisible(true); } public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
|
|
|
|
|
|
|
|
Re: Java 2D & Swing multilayer problem
Posted:
Jan 13, 2009 12:48 PM
in response to: r1omen
|
|
|
How does the polygon class look like?
|
|
|
|
|
|
|
|
Re: Java 2D & Swing multilayer problem
Posted:
Jan 14, 2009 1:37 AM
in response to: r1omen
|
|
|
You're not showing enough of the component and layout creation code to be able to diagnose. In particular it's not clear what the z-order is, or how the components are sized and positioned.
If you try setting the various panels/components you add to opaque and setting the background color it may make it obvious where your components are positioned.
Any good debugger or tools like SwingExplorer (https://swingexplorer.dev.java.net/) should also allow you to interogate the container and find the order and position of the child components.
|
|
|
|
|