a LayeredPane with images

Hello,

For school I have to make an animation of al LoadingInstallation. The installation is a simple graphics bean. Inside of this there must be placed an image.

I've put the LoadingInstallationBean in a JLabel.

private JLabel getLabelLoading(){

if (labelLoading ==null){

labelLoading =new JLabel();

labelLoading.add(getLoadingInstallationBean());

}

return labelLoading;

}

There is a second Label in which I put the figure.

All this works, but I would like to put the figure in the loading installation. I have worked with a Layered Pane, but the problem is that the loading installation is always on top and that I can't see the figure.

layeredPane.add(getLabelVullen(),new Integer(2));//bevat the figure

layeredPane.add(getLabelLoading(),new Integer(1));//contains the bean with the installation

I really hope that somebody can help me so that I see the figure in the installation.

[1382 byte] By [StijnPa] at [2007-10-2 4:59:54]
# 1
We seldome use JLabel#add() method.> labelLoading.add(getLoadingInstallationBean());What are you doing here?
hiwaa at 2007-7-16 1:03:54 > top of Java-index,Java Essentials,Java Programming...
# 2

Hello,

With this method I load a bean, this happens with this code.

private LoadingInstallationBean getLoadingInstallationBean() {

if (loadingInstallation == null) {

loadingInstallation = new LoadingInstallationBean();

loadingInstallation.setBounds(10, 10, 116, 114);

loadingInstallation.setState(true);

}

return loadingInstallation;

}

The loading installation is a bean where I can adapt the color, there is an onColor and an offColor. The bean consists of a method paint(Graphics g) in which I paint some lines.

I load the LoadingInstallation in a JLabel because it's a way to get it on the LayeredPane.

I hope you can help me with this information.

StijnPa at 2007-7-16 1:03:54 > top of Java-index,Java Essentials,Java Programming...
# 3
> The loading installation is a beanShow the code of LoadingInstallationBean class.
hiwaa at 2007-7-16 1:03:54 > top of Java-index,Java Essentials,Java Programming...
# 4

Here is the code of the LoadingInstallation:

package newGrafcetpackage;

import java.awt.Canvas;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Polygon;

import java.io.Serializable;

/**

* This class gives on if the installation is buzy to laod the trolley.

*

* @author Stijn Dely

*

*/

public class LoadingInstallationBean extends Canvas implements Serializable {

/**

* The color when the state of the contact is true. The default value is

* green.

*/

private Color onColor = Color.GREEN;

/**

* The color when the state of the contact is false. The default value is

* gray.

*/

private Color offColor = Color.GRAY;

/**

* This boolean holds the state of the cylinder.

*/

private boolean state = false;

/**

* Returns the color when the contact has a state 'true'.

*

* @return onColor The color when the contact is on.

*/

public Color getOnColor() {

return onColor;

}

/**

* Sets the color when the contact has a state 'true'.

*

* @param newOnColor

*The new color when the contact is on.

*/

public void setOnColor(Color newOnColor) {

onColor = newOnColor;

repaint();

}

/**

* Returns the color when the contact has a state 'false'.

*

* @return offColor The color when the contact is off.

*/

public Color getOffColor() {

return offColor;

}

/**

* Sets the color when the contact has a state 'false'.

*

* @param newOffColor

*The new color when the contact is off.

*/

public void setOffColor(Color newOffColor) {

offColor = newOffColor;

repaint();

}

/**

* Returns the actual state of the contact, more specific a boolean with

* value 'true' when the contact is on or a value 'false' when the contact

* is off.

*

* @return state The actual state of the contact.

*/

public boolean getState() {

return state;

}

/**

* Sets the state of the contact.

*

* @param newState

*The new state of the contact.

*/

public void setState(boolean newState) {

state = newState;

repaint();

}

/**

* This method paints the bean. Therefore, it gets the state to determine

* the color of the endcylinder of the left side.

*

* @see Graphics

*/

public void paint(Graphics g) {

if (getState() == true) {

g.setColor(getOnColor());

} else {

g.setColor(getOffColor());

}

Polygon L = new Polygon();

L.addPoint(0, 0);

L.addPoint(0, 65);

L.addPoint(30, 85);

L.addPoint(30, 114);

L.addPoint(35, 114);

L.addPoint(35, 82);

L.addPoint(5, 62);

L.addPoint(5, 0);

g.fillPolygon(L);

Polygon J = new Polygon();

J.addPoint(111, 0);

J.addPoint(111, 62);

J.addPoint(86, 82);

J.addPoint(86, 114);

J.addPoint(91, 114);

J.addPoint(91, 85);

J.addPoint(116, 65);

J.addPoint(116, 0);

g.fillPolygon(J);

}

/**

* This is the default constructor which sets the dimensions.

*/

public LoadingInstallationBean() {

setSize(116,114);

}

}

I hope this can help.

StijnPa at 2007-7-16 1:03:54 > top of Java-index,Java Essentials,Java Programming...
# 5

Thank you posting the code.

Seeing the code I could recognize that your GUI design is non-standard and wrong for your

requirement.

Will you try and study this code:

(Prepare an image file for main() method, and write the propert path name.)

import java.awt.*;

import java.awt.event.*;

import java.io.Serializable;

import javax.swing.*;

public class ImagePanel extends JPanel implements Serializable{

Image image;

Color color = Color.green;

public ImagePanel(){

super();

}

public void setImage(Image img){

image = img;

repaint();

}

public ImagePanel(Image img){

image = img;

}

public ImagePanel(String imageFile){

ImageIcon ii = new ImageIcon(imageFile);

image = ii.getImage();

}

public void paintComponent(Graphics g){

super.paintComponent(g);

if (image != null){

g.drawImage(image, 0, 0, this);

}

g.setColor(color);

Polygon L = new Polygon();

L.addPoint(0, 0);

L.addPoint(0, 65);

L.addPoint(30, 85);

L.addPoint(30, 114);

L.addPoint(35, 114);

L.addPoint(35, 82);

L.addPoint(5, 62);

L.addPoint(5, 0);

g.fillPolygon(L);

Polygon J = new Polygon();

J.addPoint(111, 0);

J.addPoint(111, 62);

J.addPoint(86, 82);

J.addPoint(86, 114);

J.addPoint(91, 114);

J.addPoint(91, 85);

J.addPoint(116, 65);

J.addPoint(116, 0);

g.fillPolygon(J);

}

/* main() for testing */

public static void main(String[] args){

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

ImagePanel ip = new ImagePanel("rose.gif");

frame.add(ip);

frame.setSize(ip.image.getWidth(ip), ip.image.getHeight(ip));

frame.setVisible(true);

}

}

hiwaa at 2007-7-16 1:03:54 > top of Java-index,Java Essentials,Java Programming...
# 6

Hello,

The code you gave works, but that is not really what I want. The aim is to make an applet where I place the LoadingInstallation (this is the bean with the properties onColor, offColor and state). In that bean, there must be placed a figure so simulate what happens at a particular moment. That simulation is not important here, one figure is enough.

What I really need is an applet with a JPanel or a JLayeredPane where I can place the bean (LoadingInstallation) and the figure. I must look as if the figure is inside the bean.

Therefore, I initially worked with Labels. I put the bean and the figure in a Label and placed this in a JLayeredPane. I had the intention to place the bean at the back and the figure on top of it, but the bean (which is put into an Label) stands always at the front. That is what I did here:

layeredPane.add(getLabelVullen(), new Integer(2)); //contains the figure

layeredPane.add(getLabelLoading(), new Integer(1)); //contains the bean with the installation

I hope this can help, it is indead a bit complicated but that is what has been asked me.

StijnPa at 2007-7-16 1:03:54 > top of Java-index,Java Essentials,Java Programming...
# 7
> but that is not really what I wantThat is exactly what you want.Don't use JLabel for such purpose. A simple component that extends the JPanel should bemore than capable, ... for anything you want.
hiwaa at 2007-7-16 1:03:54 > top of Java-index,Java Essentials,Java Programming...
# 8

> Don't use JLabel for such purpose. A simple component that extends the

> JPanel should be more than capable, ... for anything you want.

I have adapted the code of the bean so that it is a JPanel:

public class LoadingInstallationBean extends JPanel implements Serializable

Now, I will place this bean on the JLayeredPane.

private JLayeredPane getJLayeredPane() {

if (layeredPane == null) {

layeredPane = new JLayeredPane();

layeredPane.setPreferredSize(new Dimension(30, 30));

layeredPane.setBackground(Color.WHITE);

layeredPane.setLayout(null);

layeredPane.add(getLabelVullen(), new Integer(2)); //this is a Label with the figure

layeredPane.add(getLoadingInstallationBean(), new Integer(1)); //here is the bean with the installation added to the panel

}

return layeredPane;

}

I can place both components on the pane, but the problem is that the figure is standing above the bean, so that I can't see the whole loading installation. That is of course not ideal. The figure should be transparent so that I can see the bean through it.

How can I do this? I hope that this will be a final step...

StijnPa at 2007-7-16 1:03:54 > top of Java-index,Java Essentials,Java Programming...
# 9

You don't need to use layeredpane at all.

Simply add your component onto anothe container or window/frame.

That's all you should do.

Write a proper paintComponent() as I suggested.

layeredPane.add(getLabelVullen(), new Integer(2)); //contains the figure

layeredPane.add(getLabelLoading(), new Integer(1)); //contains the bean with the installation

Wrong, wrong, wrong, wroooong! Hideously wrong.

YOU DON'T NEED TO USE SEPARATE LAYERS FOR IMAGE AND DRAWING.

Use one and simple custom component. That's enough.

hiwaa at 2007-7-16 1:03:54 > top of Java-index,Java Essentials,Java Programming...
# 10
All right, thanks a lot. It works.
StijnPa at 2007-7-16 1:03:54 > top of Java-index,Java Essentials,Java Programming...