Author Topic: Interesting code???  (Read 3749 times)

Offline Hwangar

  • byte
  • *
  • Posts: 1
    • View Profile
Interesting code???
« on: December 13, 2006, 08:19:40 pm »
Hi! I'm new in jpct (in java have looong time of programming)

I was trying several projects of 3d to integrate with swing applications (lightweight trouble... :P).  I have done this code... maybe it can be interesting to you...

The thing is to create a JPanel where you can put your world in order to integrate it with all the swing stuff... here you can see that the JMenu is "OVER" the 3d image and there is a first version of the component that can be a "lightweight" panel

Tell me you opinions...

Anyway... good work, mates!
Hwangar!

Code: [Select]

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import com.threed.jpct.Camera;
import com.threed.jpct.Config;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.IRenderer;
import com.threed.jpct.Lights;
import com.threed.jpct.Object3D;
import com.threed.jpct.Primitives;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.TextureManager;
import com.threed.jpct.World;
import com.threed.jpct.util.KeyMapper;
import com.threed.jpct.util.KeyState;

public class HelloJuan extends JFrame {

  public static void main(String[] args) {
    HelloJuan h = new HelloJuan();
  }

  /**
   */
  public HelloJuan() {
    setFrameStuff();
  }

private class Panel3D extends JPanel {
private World _world;
private FrameBuffer _buffer;
public Panel3D(World world) {
_world = world;
   World.setDefaultThread(Thread.currentThread());
   
   _buffer = new FrameBuffer(640, 480, FrameBuffer.SAMPLINGMODE_NORMAL);    
   //_buffer.enableRenderer(IRenderer.RENDERER_OPENGL );
   _buffer.enableRenderer(IRenderer.RENDERER_SOFTWARE);
_buffer.disableRenderer(IRenderer.RENDERER_OPENGL);

final Camera camera = _world.getCamera();
(new Thread() {
private int i=200;
public void run() {
while(true) {
camera.setPosition(0, -100, i);
Panel3D.this.repaint();
i--;
//System.out.println(">>>>" + i);
try { Thread.sleep(50);
} catch(Exception e) { e.printStackTrace(); }
}
}
}).start();
}
public void paintComponent(Graphics g) {
//System.out.println("Init painting");
     _buffer.clear();
     _world.renderScene(_buffer);
     _world.draw(_buffer);
     _buffer.update();
     _buffer.display(g, 0, 0);
     //Thread.yield();
//System.out.println("Finish painting");
}
}

  private void setFrameStuff() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("jPCT Hello World (" + Config.getVersion() + ")");

    JPanel pane = (JPanel)getContentPane();
    pane.setLayout(new BorderLayout());
    pane.add(new JLabel("Juan"), BorderLayout.NORTH);    
   
    JPanel center = new Panel3D(getMyWorld());
    pane.add(center, BorderLayout.CENTER);

setJMenuBar(getMyMenuBar());

    pack();
    setSize(800,600);
    setResizable(true);
    setLocationRelativeTo(null);
    setVisible(true);
   
  }

  private World getMyWorld() {
    World world = new World();
    TextureManager textureManager = TextureManager.getInstance();
    Object3D box = Primitives.getBox(50, 1);
    box.setOrigin(new SimpleVector(0, 0, 0));
    world.addObject(box);
    world.buildAllObjects();
    /**
     * Place the camera at the starting position.
     */
    Camera camera = world.getCamera();
    camera.setPosition(0, -100, 200);
    camera.lookAt(box.getTransformedCenter());

    // Config.fadeoutLight = false;
    world.getLights().setOverbrightLighting(
        Lights.OVERBRIGHT_LIGHTING_DISABLED);
    world.getLights().setRGBScale(Lights.RGB_SCALE_2X);
    world.setAmbientLight(25, 30, 30);

    /**
     * Place the lightsources…
     */
    world.addLight(new SimpleVector(0, -150, 0), 25, 22, 19);
    world.addLight(new SimpleVector(-100, -150, 100), 22, 5, 4);
    world.addLight(new SimpleVector(100, -150, -100), 4, 2, 22);
   
    return world;
  }
 
  private JMenuBar getMyMenuBar() {
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JRadioButtonMenuItem rbMenuItem;
JCheckBoxMenuItem cbMenuItem;

//Create the menu bar.
menuBar = new JMenuBar();

//Build the first menu.
menu = new JMenu("A Menu");
menu.setMnemonic(KeyEvent.VK_A);
menu.getAccessibleContext().setAccessibleDescription(
       "The only menu in this program that has menu items");
menuBar.add(menu);

//a group of JMenuItems
menuItem = new JMenuItem("A text-only menu item",
                        KeyEvent.VK_T);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
       KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription(
       "This doesn't really do anything");
menu.add(menuItem);

menuItem = new JMenuItem("Both text and icon",
                        new ImageIcon("images/middle.gif"));
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);

menuItem = new JMenuItem(new ImageIcon("images/middle.gif"));
menuItem.setMnemonic(KeyEvent.VK_D);
menu.add(menuItem);

//a group of radio button menu items
menu.addSeparator();
ButtonGroup group = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
rbMenuItem.setSelected(true);
rbMenuItem.setMnemonic(KeyEvent.VK_R);
group.add(rbMenuItem);
menu.add(rbMenuItem);

rbMenuItem = new JRadioButtonMenuItem("Another one");
rbMenuItem.setMnemonic(KeyEvent.VK_O);
group.add(rbMenuItem);
menu.add(rbMenuItem);

//a group of check box menu items
menu.addSeparator();
cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
cbMenuItem.setMnemonic(KeyEvent.VK_C);
menu.add(cbMenuItem);

cbMenuItem = new JCheckBoxMenuItem("Another one");
cbMenuItem.setMnemonic(KeyEvent.VK_H);
menu.add(cbMenuItem);

//a submenu
menu.addSeparator();
submenu = new JMenu("A submenu");
submenu.setMnemonic(KeyEvent.VK_S);

menuItem = new JMenuItem("An item in the submenu");
menuItem.setAccelerator(KeyStroke.getKeyStroke(
       KeyEvent.VK_2, ActionEvent.ALT_MASK));
submenu.add(menuItem);

menuItem = new JMenuItem("Another item");
submenu.add(menuItem);
menu.add(submenu);

//Build second menu in the menu bar.
menu = new JMenu("Another Menu");
menu.setMnemonic(KeyEvent.VK_N);
menu.getAccessibleContext().setAccessibleDescription(
       "This menu does nothing");
menuBar.add(menu);  
return menuBar;
  }

}

Offline cyberkilla

  • float
  • ****
  • Posts: 413
    • View Profile
    • http://futurerp.net
Interesting code???
« Reply #1 on: December 13, 2006, 10:35:34 pm »
Nice work:) Thats similar to what im doing.

Im using a jpanel to display the images,
but i use paintimmediately to get the maximum speed.
http://futurerp.net - Text Based MMORPG
http://beta.rpwar.com - 3D Isometric MMORPG