Skip to main content

Panel addButton disapearring under background image

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
cschlamb
Offline
Joined: 2011-12-14

hey so i am new to this. I am trying to create a background picture with a Panel on the east side for options (school project) what I can not figure out is how to make it so my background image doesn't lay on top of my Panel of buttons. I see them when the program lods, but the image completely lays on top of it once it loads.

Please help

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

import javax.media.j3d.*;

import javax.vecmath.*;

import com.sun.j3d.utils.universe.*;

import com.sun.j3d.utils.geometry.*;

import com.sun.j3d.utils.applet.MainFrame;

import java.awt.image.*;

import java.io.*;

import java.net.URL;

import javax.imageio.*;

import java.applet.*;

public class Project extends Applet implements ActionListener {

public static void main(String[] args) {

new MainFrame(new Project(), 640, 480);

}

Background background = null;

ImageComponent2D image = null;

public void init() {

setLayout(new BorderLayout());

Panel p = new Panel();

p.setLayout(new GridLayout(12,1,10,5));

add(p, BorderLayout.EAST);

p.add(new Panel());

p.add(new Label("Weather Conditions"));

Button button = new Button("Rain");

p.add(button);

button.addActionListener(this);

button = new Button("Sun");

p.add(button);

button.addActionListener(this);

button = new Button("Fog");

p.add(button);

button.addActionListener(this);

GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();

Canvas3D cv = new Canvas3D(gc);

setLayout(new BorderLayout());

add(cv, BorderLayout.CENTER);

BranchGroup bg = createSceneGraph();

bg.compile();

SimpleUniverse su = new SimpleUniverse(cv);

su.getViewingPlatform().setNominalViewingTransform();

su.addBranchGraph(bg);

}

Appearance ap;

private BranchGroup createSceneGraph() {

BranchGroup root = new BranchGroup();

//object

ap = new Appearance();

ap.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);

ap.setCapability(Appearance.ALLOW_POINT_ATTRIBUTES_WRITE);

ap.setCapability(Appearance.ALLOW_LINE_ATTRIBUTES_WRITE);

ap.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE);

ap.setCapability(Appearance.ALLOW_RENDERING_ATTRIBUTES_WRITE);

ap.setCapability(Appearance.ALLOW_MATERIAL_WRITE);

ap.setMaterial(null);

//light

//PointLight light = new PointLight(new Color3f(Color.white),

//new Point3f(1f,1f,1f),

//new Point3f(1f,0.1f,0f));

BoundingSphere bounds = new BoundingSphere();

//light.setInfluencingBounds(bounds);

//root.addChild(light);

//background

background = new Background(1.0f, 1.0f, 1.0f);

background.setApplicationBounds(bounds);

//rotator.setSchedulingBounds(bounds);

//load image

URL url = getClass().getClassLoader().getResource("back.jpg");

BufferedImage bi = null;

try {

bi = ImageIO.read(url);

} catch (IOException ex) {

ex.printStackTrace();

}

image = new ImageComponent2D(ImageComponent2D.FORMAT_RGB, bi);

background.setImage(image);

//set capacity bit to allow color and image change

//background.setCapability(Background.ALLOW_COLOR_WRITE);

//background.setCapability(Background.ALLOW_IMAGE_READ);

//background.setCapability(Background.ALLOW_IMAGE_WRITE);

root.addChild(background);

return root;

}

public void actionPerformed(ActionEvent actionEvent) {

String cmd = actionEvent.getActionCommand();

if ("Rain".equals(cmd)) {

} else if ("Sun".equals(cmd)) {

} else if ("Fog".equals(cmd)) {

}

}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
cschlamb
Offline
Joined: 2011-12-14

never mind, seems that I had two setLayout(new BorderLayouts()) in the code.