Hallo,
wie kann ich eine Hintergrundbild verwenden?
Ich möchte einen Tachometer erstellen. Dazu habe ich den Zeiger als 3D-Objekt aus einer 3ds geladen und rotiere ihn. Im Hintergrund soll eine Grafik (Tachometer) dargestellt werden.
Ich habe es mit JLayeredPane versucht, allerdings weiß ich nicht, wie ich den Hintergrund der Welt durchsichtig bekomme.
Weiterhin habe ich auch schon versucht, direkt dem FrameBuffer die Hintergrundgrafik zu übergeben, dabei wird dann aber leider meine 3D-Objekt nicht mehr richtig gezeichnet.
Hier mein aktueller Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.Timer;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Loader;
import com.threed.jpct.Matrix;
import com.threed.jpct.Object3D;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;
import com.threed.jpct.World;
/**
*/
public class HelloWorldOGL implements KeyListener {
/**
*
*/
private final int myScale = 1;
/**
*
*/
private final JFrame frame;
/**
*
*/
private final my3DPanel arrowPanel;
/**
*
*/
private boolean isRotatingX, isRotatingY, isRotatingZ = false;
/**
*
*/
private final int interval = 10;
/**
* @param args
*/
public static void main(final String[] args) {
new HelloWorldOGL();
}
/**
*/
HelloWorldOGL() {
this.frame = new JFrame("Blender Model Loading");
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.addKeyListener(this);
final JLayeredPane jlp = new JLayeredPane();
final JLabel bg = new JLabel();
bg.setIcon(new ImageIcon("c:/tres/t3.gif"));
bg.setVisible(true);
bg.setBounds(0, 0, 800, 600);
this.arrowPanel =
new my3DPanel("c:/tres/ft_arrow.3ds", "c:/tres/ft_arrow3_Cube2.jpg", this.myScale, this.interval);
this.arrowPanel.setBounds(0, 0, 800, 600);
jlp.add(bg, 2);
jlp.add(this.arrowPanel, 1);
jlp.moveToFront(this.arrowPanel);
this.frame.add(jlp);
this.frame.setSize(800, 630);
this.frame.setVisible(true);
}
/**
* (non-Javadoc)
* @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
*/
@Override
public void keyPressed(final KeyEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getKeyCode() == KeyEvent.VK_A) {
this.isRotatingX = !this.isRotatingX;
}
if (arg0.getKeyCode() == KeyEvent.VK_S) {
this.isRotatingY = !this.isRotatingY;
}
if (arg0.getKeyCode() == KeyEvent.VK_D) {
this.isRotatingZ = !this.isRotatingZ;
}
}
/**
* (non-Javadoc)
* @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
*/
@Override
public void keyReleased(final KeyEvent arg0) {
// TODO Auto-generated method stub
}
/**
* myCOmment
* @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
*/
@Override
public void keyTyped(final KeyEvent arg0) {
// TODO Auto-generated method stub
}
/**
*/
private class my3DPanel extends JPanel implements ActionListener {
/**
*
*/
private static final long serialVersionUID = -1502763015170950669L;
/**
*
*/
private final Object3D arrowObj;
/**
*
*/
private final World world;
/**
*
*/
private final FrameBuffer buffer;
/**
*
*/
private final Timer timer;
/**
* @param modelFile
* @param texFile
* @param scale
* @param interval
*/
my3DPanel(final String modelFile, final String texFile, final int scale, final int interval) {
this.world = new World();
this.world.setAmbientLight(150, 150, 150);
TextureManager.getInstance().addTexture("arrowTex", new Texture(texFile));
this.arrowObj = loadModel(modelFile, scale);
this.arrowObj.setTexture("arrowTex");
this.arrowObj.translate(0, 0, -10);
this.arrowObj.build();
this.world.addObject(this.arrowObj);
this.world.getCamera().setPosition(0, 0, 30);
this.world.getCamera().lookAt(new SimpleVector(0, 0, 0));
this.buffer = new FrameBuffer(800, 600, FrameBuffer.SAMPLINGMODE_NORMAL);
this.timer = new Timer(interval, this);
this.timer.start();
}
/**
* @param modelFile
* @param scale
* @return the Object3D for the pointer
*/
private Object3D loadModel(final String modelFile, final int scale) {
final Object3D[] model = Loader.load3DS(modelFile, scale);
Object3D o3d = new Object3D(0);
Object3D temp = null;
for (int i = 0; i < model.length; i++) {
temp = model[i];
temp.setCenter(SimpleVector.ORIGIN);
temp.rotateX((float) Math.toRadians(180));
temp.rotateMesh();
temp.setRotationMatrix(new Matrix());
o3d = Object3D.mergeObjects(o3d, temp);
o3d.build();
}
return o3d;
}
/**
* TODO
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(final ActionEvent ae) {
this.arrowObj.setRotationPivot(new SimpleVector(0, 0, -4));
this.arrowObj.rotateZ(0.01f);
this.buffer.clear();
this.world.renderScene(this.buffer);
this.world.draw(this.buffer);
this.buffer.update();
this.buffer.display(getGraphics());
}
}
}