2D moteur

This commit is contained in:
Antonin Boyon 2021-02-16 18:01:40 +01:00
commit b544c486af
6 changed files with 168 additions and 32 deletions

View File

@ -1,15 +0,0 @@
package lsystem.screen;
import javax.swing.*;
public class Help {
public Help(JTabbedPane tabs){
JPanel helpTab = new JPanel();
JTextArea helpText = new JTextArea();
helpText.setText(Constants.HELP);
helpText.setEditable(false);
helpTab.add(helpText);
tabs.addTab("Aide",(new JScrollPane(helpTab)));
}
}

View File

@ -0,0 +1,89 @@
package lsystem.screen;
import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.gl2.GLUT;
public class JoglEventListener implements GLEventListener {
private GLU glu = new GLU();
private GLUT glut = new GLUT();
private float camera [] = {0f, 0f, 5f};
private float [] light_0_ambient = {0.01f, 0.01f, 0.01f, 0.01f};
private float [] light_0_diffuse = {1.0f, 1.0f, 1.0f, 1.0f};
private float [] light_0_specular = {1.0f,1.0f, 1.0f, 1.0f};
private float [] light_0_position = {100f, 0f, 10f, 1f};
private float [] material_specular = {1.0f, 1.0f, 1.0f, 1.0f};
private float angle = 0f;
@Override
public void init(GLAutoDrawable glAutoDrawable) {
GL2 gl = glAutoDrawable.getGL().getGL2();
gl.glClearColor(0.8f, 0.5f, 0.0f, 1.0f);
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glClearDepth(1.0f);
gl.glShadeModel(GL2.GL_SMOOTH);
gl.glEnable(GL2.GL_LIGHTING);
gl.glEnable(GL2.GL_LIGHT0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, light_0_ambient, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, light_0_diffuse, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_0_specular, 0);
gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_0_position, 0);
gl.glEnable(GL2.GL_COLOR_MATERIAL);
gl.glColorMaterial(GL2.GL_FRONT_AND_BACK, GL2.GL_AMBIENT_AND_DIFFUSE);
gl.glMateriali(GL2.GL_FRONT_AND_BACK, GL2.GL_SHININESS, 90);
gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, material_specular, 0);
}
@Override
public void dispose(GLAutoDrawable glAutoDrawable) {
}
@Override
public void display(GLAutoDrawable glAutoDrawable) {
GL2 gl = glAutoDrawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
glu.gluLookAt(
camera[0], camera[1], camera[2],
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f
);
gl.glRotatef(angle, 0f, 1f, 0f);
gl.glColor3f(1.0f, 0.0f, 0.0f);
glut.glutSolidSphere(1.0f, 20, 20);
angle += 0.1f;
angle %= 360f;
}
@Override
public void reshape(GLAutoDrawable glAutoDrawable, int x, int y, int width, int height) {
GL2 gl = glAutoDrawable.getGL().getGL2();
gl.glViewport(x, y, width, height);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(60.0f, (float) width/height, 0.1f, 10.0f);
gl.glMatrixMode(GL2.GL_MODELVIEW);
}
}

View File

@ -21,16 +21,18 @@ public class MainFrame extends JFrame {
private JButton newGen; private JButton newGen;
private JButton help; private JButton help;
private int nbRules; private int nbRules;
public HashMap<Integer,Component> componentList; public HashMap<Integer,JTextArea> textAreaList;
public HashMap<Integer,JTextField> textFieldList;
public MainFrame(){ public MainFrame(){
componentList = new HashMap<>(); textAreaList = new HashMap<>();
textFieldList = new HashMap<>();
nbRules = 1; nbRules = 1;
nbTabs = 0; nbTabs = 0;
basePanel = new JPanel(); basePanel = new JPanel();
basePanel.setLayout(new FlowLayout(FlowLayout.CENTER)); basePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
tabs = new JTabbedPane(SwingConstants.TOP); tabs = new JTabbedPane(SwingConstants.TOP);
JToolBar toolBar = new JToolBar(); JToolBar toolBar = new JToolBar();
newGen = new JButton("Nouvelle génération"); newGen = new JButton("Nouvelle génération");
newGen.addActionListener(new NewGenListener(this)); newGen.addActionListener(new NewGenListener(this));
@ -38,8 +40,8 @@ public class MainFrame extends JFrame {
help = new JButton("Aide"); help = new JButton("Aide");
help.addActionListener(new HelpListener(this)); help.addActionListener(new HelpListener(this));
toolBar.add(help); toolBar.add(help);
this.setTitle("L-system interface"); this.setTitle("L-system interface");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(Constants.WIDTH, Constants.HEIGHT); this.setSize(Constants.WIDTH, Constants.HEIGHT);
this.setLocationRelativeTo(null); this.setLocationRelativeTo(null);
@ -47,12 +49,26 @@ public class MainFrame extends JFrame {
this.add(toolBar, BorderLayout.NORTH); this.add(toolBar, BorderLayout.NORTH);
this.setPreferredSize(new Dimension(640,600)); this.setPreferredSize(new Dimension(640,600));
} }
public void addToComponentList(Component c ,Integer i){ public void addToTextAreaList(JTextArea c ,Integer i){
componentList.put(i,c); textAreaList.put(i,c);
}
public void addToTextFieldList(JTextField c ,Integer i){
textFieldList.put(i,c);
} }
public void newHelp() { public void newHelp() {
new Help(tabs); JFrame aide = new JFrame();
aide.setTitle("Aide");
aide.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
aide.setSize(700,800);
aide.setLocationRelativeTo(null);
aide.setVisible(true);
JTextArea helpText = new JTextArea();
helpText.setText(Constants.HELP);
helpText.setEditable(false);
aide.add(helpText);
} }
public void newTab() { public void newTab() {
if(nbTabs>2) if(nbTabs>2)
@ -66,7 +82,7 @@ public class MainFrame extends JFrame {
public void closeTab() { public void closeTab() {
//TODO : Pour fermer un onglet, nécessite l'implémentation d'un button fermer grâce à la méthode newTab(). //TODO : Pour fermer un onglet, nécessite l'implémentation d'un button fermer grâce à la méthode newTab().
} }
public void changeList(Character stringToAdd, byte messageType, JTextArea list) { public void changeList(String stringToAdd, byte messageType, JTextArea list) {
if (stringToAdd == null) { if (stringToAdd == null) {
switch(messageType) { switch(messageType) {
case 0: case 0:
@ -75,9 +91,9 @@ public class MainFrame extends JFrame {
list.setText("Règles : \n"); list.setText("Règles : \n");
default: default:
throw new IllegalArgumentException("Wrong argument given to method changeList"); throw new IllegalArgumentException("Wrong argument given to method changeList");
} }
}else{ }else{
list.append(String.valueOf(stringToAdd)); list.append(stringToAdd);
} }
} }
public String getAxiom(){ public String getAxiom(){
@ -106,4 +122,3 @@ public class MainFrame extends JFrame {
} }
} }

View File

@ -0,0 +1,34 @@
package lsystem.screen;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.FPSAnimator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class SwingGLCanvas {
public SwingGLCanvas() {
GLProfile glProfile = GLProfile.getDefault();
GLCapabilities glCapabilities = new GLCapabilities(glProfile);
final GLCanvas glCanvas = new GLCanvas(glCapabilities);
glCanvas.addGLEventListener(new JoglEventListener());
final JFrame jframe = new JFrame("L-System");
final FPSAnimator animator = new FPSAnimator(glCanvas, 60);
jframe.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
jframe.dispose();
}
});
jframe.getContentPane().add(glCanvas, BorderLayout.CENTER);
jframe.setSize(Constants.WIDTH, Constants.HEIGHT);
animator.start();
jframe.setVisible(true);
}
}

View File

@ -1,5 +1,7 @@
package lsystem.screen; package lsystem.screen;
import lsystem.screen.listener.AxiomFieldListener;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import javax.swing.JTabbedPane; import javax.swing.JTabbedPane;
@ -12,22 +14,27 @@ public class Tab {
JTextArea axiomList = new JTextArea(); JTextArea axiomList = new JTextArea();
axiomList.setText("Axiome : \n"); axiomList.setText("Axiome : \n");
axiomList.setEditable(false); axiomList.setEditable(false);
frame.addToTextAreaList(axiomList,nbTabs);
JTextArea rulesList = new JTextArea(); JTextArea rulesList = new JTextArea();
rulesList.setText("Règles : \n"); rulesList.setText("Règles : \n");
rulesList.setEditable(false); rulesList.setEditable(false);
frame.addToTextAreaList(rulesList,nbTabs+10);
JButton closeButton = new JButton("x"); JButton closeButton = new JButton("x");
JLabel axiome = new JLabel("Axiome"); JLabel axiome = new JLabel("Axiome");
JLabel rules = new JLabel("Règle "+ nbRules); JLabel rules = new JLabel("Règle "+ nbRules);
JTextField axiomeField = new JTextField(); JTextField axiomeField = new JTextField();
frame.addToComponentList(axiomeField,nbTabs); axiomeField.addKeyListener(new AxiomFieldListener(frame,nbTabs));
axiomeField.setPreferredSize(new Dimension(20,20)); axiomeField.setPreferredSize(new Dimension(20,20));
frame.addToTextFieldList(axiomeField,nbTabs);
JTextField rulesField = new JTextField(); JTextField rulesField = new JTextField();
frame.addToComponentList(rulesField,nbTabs+10);
rulesField.setPreferredSize(new Dimension(20,20)); rulesField.setPreferredSize(new Dimension(20,20));
tab.add(axiome); tab.add(axiome);
tab.add(axiomeField); tab.add(axiomeField);
tab.add(axiomList); tab.add(axiomList);

View File

@ -19,16 +19,22 @@ public class AxiomFieldListener implements KeyListener {
} }
@Override @Override
public void keyTyped(KeyEvent ke) { public void keyTyped(KeyEvent ke) {
frame.changeList(ke.getKeyChar(),(byte)0, (JTextArea) frame.componentList.get(index)); if(ke.getKeyCode() != KeyEvent.VK_ENTER)
frame.changeList(String.valueOf(ke.getKeyChar()),(byte)0, (JTextArea) frame.textAreaList.get(index));
else{
String str = ";\r\n";
frame.changeList(str,(byte)0, (JTextArea) frame.textAreaList.get(index));
frame.textFieldList.get(index).setText(null);
}
} }
@Override @Override
public void keyPressed(KeyEvent ke) { public void keyPressed(KeyEvent ke) {
if(ke.getKeyCode() == KeyEvent.VK_ENTER)
frame.textFieldList.get(index).setText(null);
} }
@Override @Override
public void keyReleased(KeyEvent ke) { public void keyReleased(KeyEvent ke) {
} }
} }