Using jPCT in Swing

From JPCT
Revision as of 00:30, 2 February 2013 by Admin (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Preface

There is an example for using AWTGL-Renderer with a Swing component inside the download package (HelloWorldAWTGL), but in this article it should be explained how you can separate the GUI from the jPCT things.

There are some aspects that should be considered:

  • It seems that there is no other way than extending a swing component.
  • The painting is actually done by the paint-method of the component, which must be overwritten.

The steps

  1. Write a class, that extends the component in which the OpenGL stuff should be rendered - in this example a JPanel component.
     public class RenderPanel extends JPanel
  2. Create a world an do the things you want to do in it.
  3. Don't forget to overwrite the paint component
    @Override
       public void paintComponent(Graphics g) {
            buffer.display(g);
       }
  4. Write the GUI class and insert the extending class in the right place.
    RenderPanel panel_1 = new RenderPanel();
    		panel.add(panel_1, BorderLayout.CENTER);
  5. Every time you want the image to update, use the repaint() method (this will mostly be done in a while-loop, but not in my example).


The Code

The jPCT class

package jPCT;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class HelloGui extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					HelloGui frame = new HelloGui();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	
	
	public HelloGui() throws Exception {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 800, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnTest = new JButton("Left");
		btnTest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				RenderPanel.rotateleftright(1);
				repaint();
			}
		});
		btnTest.setBounds(10, 11, 89, 23);
		contentPane.add(btnTest);
		
		JButton btnRight = new JButton("Right");
		btnRight.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				RenderPanel.rotateleftright(-1);
				repaint();
			}
		});
		btnRight.setBounds(10, 45, 89, 23);
		contentPane.add(btnRight);
				
		JPanel panel = new JPanel();
		panel.setBounds(109, 11, 665, 540);
		contentPane.add(panel);
		panel.setLayout(new BorderLayout(0, 0));
		
		RenderPanel panel_1 = new RenderPanel();
		panel.add(panel_1, BorderLayout.CENTER);
				
	}
}


The GUI Class:

import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;

import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Matrix;
import com.threed.jpct.Object3D;
import com.threed.jpct.Primitives;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;
import com.threed.jpct.World;

public class RenderPanel extends JPanel {
	private static final long serialVersionUID = 1L;

	static World world;
	static FrameBuffer buffer;
	Object3D box;
	Object3D plane, plane1;
	{
	
	world = new World();
	
	world.setAmbientLight(255, 255, 255);

	TextureManager.getInstance().addTexture("boxt",new Texture(128, 128, Color.PINK));

	box = Primitives.getBox(3f, 2f);
	
	box.setTexture("boxt");
	box.setTransparency(0);
	box.setEnvmapped(Object3D.ENVMAP_ENABLED);
	box.build();
	
	world.addObject(box);

	world.getCamera().setPosition(50, -20, -5);
	world.getCamera().lookAt(box.getTransformedCenter());
	buffer = new FrameBuffer(660, 530, FrameBuffer.SAMPLINGMODE_NORMAL);}	
	
	
	public RenderPanel() {
		setPreferredSize(new Dimension(buffer.getOutputWidth(), buffer.getOutputHeight()));
		setFocusable(true);
		setLayout(null);
		
		buffer.clear(java.awt.Color.DARK_GRAY);
		world.renderScene(buffer);
//		world.drawWireframe(buffer, Color.WHITE);
		world.draw(buffer);
		buffer.update();		
	}
	
	public static void rotateleftright(float v){
		SimpleVector temp = world.getCamera().getPosition();
		float rotation = v* (float) Math.PI/30;
		temp.rotateY(-rotation);
		
		
		world.getCamera().setPosition(temp);
		world.getCamera().lookAt(new SimpleVector(0,0,0));
		
		buffer.clear(java.awt.Color.DARK_GRAY);
		world.renderScene(buffer);
//		world.drawWireframe(buffer, Color.WHITE);
		world.draw(buffer);
		buffer.update();
	}
	
	
	@Override
    public void paintComponent(Graphics g) {
        buffer.display(g);
    }
    
}