still doesn’t work, even though the code gets called =/ When exactly does it call this swingUtilities method? Because it doesn’t seem to be called in chronical order
import java.awt.Color;
import java.awt.Image;
import com.yumeprojects.jimDB.JimDB;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferStrategy;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
* Graphics engine class for the program, handles rendering
* @author Kurten
* @version 0.42
* Last updated: 2011-11-08
*/
public class GraphicsEngine implements Runnable{
protected boolean looping;
private BufferStrategy strat;
private Graphics2D g2d;
private JFrame frame;
private Mathematics math;
private Image bgImage[];
private int bgIndex;
protected Color bgColor = Color.BLACK;
protected Color drawColor = Color.RED;
protected boolean drawAlgebraChoices, drawGeometryChoices, drawArithmeticChoice, drawMainMenu;
protected Button[] mainMenuButtons = {
new Button("Algebra", "/images/Buttondark.png", "/images/theButton3.png", 100, 50),
new Button("Geometry", "/images/Buttondark.png", "/images/theButton3.png", 205, 50),
new Button("Arithmetic", "/images/Buttondark.png", "/images/theButton3.png", 310, 50),
new Button("Statistics", "/images/Buttondark.png", "/images/theButton3.png", 415, 50),
new Button("Probability", "/images/Buttondark.png", "/images/theButton3.png", 520, 50)
};
protected Button[] algebraSectionButtons = {
new Button("Linear Equations" , "/images/Buttondark.png", "/images/theButton3.png", 100,50),
new Button("System of equations", "/images/Buttondark.png", "/images/theButton3.png", 205, 50),
new Button("Variables", "/images/Buttondark.png", "/images/theButton3.png", 310, 50)
};
protected Button[] geometrySectionButtons = {
new Button("Square area and volume", "/images/kvadrat volym och area100xtrans.png", "/images/kvadrat volym och area100xtransglow.png", 100, 50),
new Button("Circle area and volume", "/images/cirkel volym och area100pxtrans.png", "/images/cirkel volym och area100pxtransglow.png", 205, 50),
new Button("Triangle area and volume", "/images/Triangelvoloarea.png", "/images/Triangelvoloareaglow.png", 310, 50),
new Button("Angles", "/images/Vinklar.png", "/images/Vinklarglow.png", 415, 50),
new Button("Patterns", "/images/Buttondark.png", "/images/theButton3.png", 520, 50),
new Button("Square perimiter", "/images/Buttondark.png", "/images/theButton3.png", 100, 155),
new Button("Circle perimiter", "/images/Buttondark.png", "/images/theButton3.png", 205, 155),
new Button("Triangle perimiter", "/images/Buttondark.png", "/images/theButton3.png", 310, 155),
new Button("Degrees and radians", "/images/Buttondark.png", "/images/theButton3.png", 415, 155),
new Button("Symmetry", "/images/Buttondark.png", "/images/theButton3.png", 520, 155)
};
protected Button switchBackgroundButton = new Button("Switch Background", "/images/Buttondark.png", "/images/theButton3.png", 695, 565);
private String[][][] imageFilepath = {
//Algebra
{
{"/images/denshin enderman.PNG", "/images/enderman.PNG"}, //section 1
{"/images/Buttondark.png", "/images/theButton3.png"} //section 2
},
//Geometry
{
{"/images/Buttondark.png", "/images/theButton3.png"}, //Square are and volume
{"/images/denshin enderman.PNG", "/images/enderman.PNG"}, //Circle area and volume
{"/images/denshin enderman.PNG", "/images/enderman.PNG"}, //Triangle area and volume
{"/images/denshin enderman.PNG", "/images/enderman.PNG"}, //Angles
{"/images/denshin enderman.PNG", "/images/enderman.PNG"}, //Patterns
{"/images/denshin enderman.PNG", "/images/enderman.PNG"}, //Square perimiter
{"/images/denshin enderman.PNG", "/images/enderman.PNG"}, //Circle perimiter
{"/images/denshin enderman.PNG", "/images/enderman.PNG"}, //Triangle perimiter
{"/images/denshin enderman.PNG", "/images/enderman.PNG"}, //Degrees and radians
{"/images/denshin enderman.PNG", "/images/enderman.PNG"} //Symmetry
}
};
private String[][][] imageIDKeys = {
//Algebra
{
{"Linear equations 1", "Linear equations 2"},
{"System of equations 1", "System of equation 2"}
},
//Geometry
{
{"Square area and volume 1", "Square area and volume 2"},
{"Circle area and volume 1", "Circle area and volume 2"},
{"Triangle area and volume 1", "Circle area and volume 2"},
{"Angles 1", "Angles 2"},
{"Patterns 1", "Patterns 2"},
{"Square perimiter 1", "Square perimiter 2"},
{"Circle perimiter 1", "Circle perimiter 2"},
{"Triangle perimiter 1", "Triangle perimiter 2"},
{"Degrees and radians 1", "Degrees and radians 2"},
{"Symmetry 1", "Symmetry 2"}
}
};
protected JimDB database = new JimDB(this, imageFilepath, imageIDKeys);
private Image[] jimImage;
private int jimImageIndex;
WindowListener windowListener = new WindowAdapter(){
// anonymous WindowAdapter class
public void windowClosing (WindowEvent w){
exitProgram();
} // end windowClosing
};// end anonymous class
/**
* Constructor
* @param f
* @param buff
* @param m
* @throws IOException
*/
public GraphicsEngine(JFrame f, BufferStrategy buff, Mathematics m) throws IOException{
frame = f;
strat = buff;
math = m;
bgImage = new Image[5];
bgImage[0] = loadImage("/images/bakgrund01.png");
bgImage[1] = loadImage("/images/bakgrund02.png");
bgImage[2] = loadImage("/images/bakgrund03.png");
bgImage[3] = loadImage("/images/bakgrund04.png");
bgImage[4] = loadImage("/images/bakgrund05.png");
}
public void initFrameAndStuff(){
frame = new JFrame("Project Mathematics");
frame.setSize(800, 600);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(windowListener);
frame.setVisible(true);
try{
SwingUtilities.invokeLater(new Runnable(){ //TODO finish this stuff with the EDT thread.
public void run(){
System.out.println("Check");
frame.createBufferStrategy(2);
strat = frame.getBufferStrategy();
}
});
//frame.createBufferStrategy(2);
}catch(IllegalStateException e){
e.getMessage();
e.printStackTrace();
frame.setVisible(true);
frame.createBufferStrategy(2);
}finally{
math.startThreads();
}
}
public void exitProgram(){
math.stopAll();
frame.setVisible(false);
frame.dispose();
}
@Override
public void run(){
looping = true;
while(looping){
renderReInit();
render();
renderDispose();
renderUpdate();
try{
Thread.sleep(20);
}catch(InterruptedException e){
System.out.println("Skiten kraschade");
e.printStackTrace();
}
}
}
public void renderReInit(){
try{
g2d = (Graphics2D)strat.getDrawGraphics();
}catch(IllegalStateException e){
System.err.println("Buffer failed to recreate");
strat = frame.getBufferStrategy();
g2d = (Graphics2D)strat.getDrawGraphics();
System.err.println("Buffer recreated");
}
}
public void render(){
g2d.setColor(bgColor);
g2d.fillRect(0, 0, 800, 600);
g2d.drawImage(bgImage[bgIndex], 0, 0, null);
g2d.setColor(drawColor);
g2d.drawString("Choice: " + math.choice, 20, 50);
g2d.drawString("Unit: " + math.unit, 20, 70);
g2d.drawString("Dimensions on frame: 800x600 pix", 50, 300);
g2d.drawString("Button dimensions: " + geometrySectionButtons[0].getWidth() + "x" + geometrySectionButtons[0].getHeight() + " pix", 50, 320);
if(drawMainMenu){
for(int i = 0; i < mainMenuButtons.length; i++){
mainMenuButtons[i].draw(g2d);
}
switchBackgroundButton.draw(g2d);
}else if(drawAlgebraChoices){
for(int i = 0; i < algebraSectionButtons.length; i++){
algebraSectionButtons[i].draw(g2d);
}
}else if(drawGeometryChoices){
for(int i = 0; i < geometrySectionButtons.length; i++){
geometrySectionButtons[i].draw(g2d);
}
}
if(database.drawImage){
if(jimImageIndex < jimImage.length){
g2d.drawImage(jimImage[jimImageIndex], 200, 300, null);
}else{
g2d.drawImage(jimImage[0], 20, 300, null);
}
}
if(drawAlgebraChoices){
g2d.drawString("Algebra! :D", 100, 100);
}else if(drawGeometryChoices){
g2d.drawString("Geometry! :D", 100, 100);
}else if(drawArithmeticChoice){
g2d.drawString("Arithmetic! :D", 100, 100);
}
if(!strat.contentsLost()){
strat.show();
}else{
System.err.println("Buffer contents lost");
System.err.println("Calm down though, this shit ain't serious");
System.err.println("Just continue chillin', the program will handle it");
}
}
public void renderDispose(){
g2d.dispose();
}
public void renderUpdate(){
database.update();
}
public void setButtonHoverStatus(int index, boolean status){
if(!(mainMenuButtons[index].isHovered())){
mainMenuButtons[index].setHover(true);
}else{
mainMenuButtons[index].setHover(false);
}
}
public Image loadImage(String path) throws IOException{
System.out.println("Loading image: " + path);
Image tempImg;
try{
tempImg = new ImageIcon(getClass().getResource(path)).getImage();
if(tempImg != null){
System.out.println("Image " + path + " loaded without errors");
return tempImg;
}else{
return null;
}
}catch(NullPointerException assfuck){
throw new IOException();
}
}
public void modifyBackgroundIndex(int delta){
if(bgIndex + delta < bgImage.length){
bgIndex += delta;
}else{
bgIndex = 0;
}
}
public JFrame getAppFrame(){return frame;}
public void setJimImage(Image[] img){
jimImage = img;
}
public Image[] getJimImage(){return jimImage;}
}
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFrame;
/**
* Main class for the project
* @author Johan Uttermalm
* @version 0.42
* Last updated: 2011-11.06
*/
public class Mathematics extends JFrame implements Runnable{
protected Choice c1Test = new Choice("lol");
protected boolean squareIncomplete;
protected String choice = "";
private int amount;
private float xValue, yValue, zValue;
protected int unit;
private int[] squareDim = new int[4];
private int squareIndex;
private Scanner amed = new Scanner(System.in);
private boolean running;
protected boolean drawing;
protected JFrame frame;
protected BufferStrategy strat;
protected Graphics2D g2d;
protected Thread graphicsLoop;
protected Thread listeningLoop;
protected Thread mainLoop;
protected GraphicsEngine ge;
protected InputListener inLis;
protected MathGeek charles = new MathGeek();
protected boolean drawShapes, drawSquare, drawTriangle, drawCircle;
protected boolean performingEquation, performingQuadratic, performingNegativeQuadratic;
protected double resultToGraphEng;
private char currentTypedChar;
public static void main(String[] args){ //TODO Remember to use the -Dsun.java2d.d3d=false command when executing!!!
Mathematics math = new Mathematics();
if(args.length < 1){
System.err.println("Please execute with the -Dsun.java2d.d3d=false command");
System.err.println("This can be done by typing java -Dsun.java2d.d3d=false -jar \"File path here\" in the start.bat script i have provided");
System.err.println("For devs: just add the line -Dsun.java2d.d3d=false to the program arguments section in run configuration");
try{
Thread.sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}
}else{
System.out.println(args[0]);
math.init();
}
//math.start();
}
public void init(){
try{
ge = new GraphicsEngine(frame, strat, this); //If shit get's unstable, change 'frame' to 'this'
}catch(IOException assfuckingIOException){
assfuckingIOException.printStackTrace();
try{
Thread.sleep(10000);
}catch(InterruptedException e){
e.printStackTrace();
}
System.exit(1);
}
graphicsLoop = new Thread(ge);
listeningLoop = new Thread(inLis);
mainLoop = new Thread(this);
ge.initFrameAndStuff();
inLis = new InputListener(this, ge);
}
public void startThreads(){
listeningLoop.start();
mainLoop.start();
graphicsLoop.start();
}
public void stopAll(){
inLis.listening = false;
ge.looping = false;
running = false;
}
@Override
public void run(){
running = true;
while(running){
checkAction();
checkSubSection();
ge.database.update();
try{
Thread.sleep(20);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
public void checkAction(){
if(inLis != null && ge != null){
if(inLis.isInAlgebra){
ge.drawAlgebraChoices = true;
ge.drawMainMenu = false;
}else if(inLis.isInGeometry){
ge.drawGeometryChoices = true;
ge.drawMainMenu = false;
}else if(inLis.isInArithmetic){
ge.drawArithmeticChoice = true;
ge.drawMainMenu = false;
}
else{
ge.drawAlgebraChoices = false;
ge.drawGeometryChoices = false;
ge.drawArithmeticChoice = false;
ge.drawMainMenu = true;
}
}
}
public void checkSubSection(){
if(inLis != null && ge != null && ge.database != null){
if(inLis.isInAlgebra){
ge.database.inAlgebra = true;
if(inLis.isInAlgLinearEquat){
ge.database.inLinearEquat = true;
ge.drawAlgebraChoices = false;
}else if(inLis.isInAlgSystemOfEquat){
ge.database.inSystemOfEquat = true;
ge.drawAlgebraChoices = false;
}else{
//ge.test.inAlgebra = false; kanske ta bort det här sen, vet inte :3
ge.database.inLinearEquat = false;
ge.database.inSystemOfEquat = false;
}
}else if(inLis.isInGeometry){
ge.database.inGeometry = true;
if(inLis.inGeoSquareAreaVolume){
ge.database.inSquareAreaVolume = true;
ge.drawGeometryChoices = false;
}else if(inLis.inGeoCircleAreaVolume){
ge.database.inCircleAreaVolume = true;
ge.drawGeometryChoices = false;
}else if(inLis.inGeoTriangleAreaVolume){
ge.database.inTriangleAreaVolume = true;
ge.drawGeometryChoices = false;
}else if(inLis.inGeoAngles){
ge.database.inAngles = true;
ge.drawGeometryChoices = false;
}else if(inLis.inGeoPatterns){
ge.database.inPatterns = true;
ge.drawGeometryChoices = false;
}else if(inLis.inGeoSquarePerimiter){
ge.database.inSquarePerimiter = true;
ge.drawGeometryChoices = false;
}else if(inLis.inGeoCirclePerimiter){
ge.database.inCirclePerimiter = true;
ge.drawGeometryChoices = false;
}else if(inLis.inGeoTrianglePerimiter){
ge.database.inTrianglePerimiter = true;
ge.drawGeometryChoices = false;
}else if(inLis.inGeoDegreesRadians){
ge.database.inDegreesRadians = true;
ge.drawGeometryChoices = false;
}else if(inLis.inGeoSymmetry){
ge.database.inSymmetry = true;
ge.drawGeometryChoices = false;
}
else{
//row 0
ge.database.inSquareAreaVolume = false;
ge.database.inCircleAreaVolume = false;
ge.database.inTriangleAreaVolume = false;
ge.database.inAngles = false;
ge.database.inPatterns = false;
//row 1
ge.database.inSquarePerimiter = false;
ge.database.inCirclePerimiter = false;
ge.database.inTrianglePerimiter = false;
ge.database.inDegreesRadians = false;
ge.database.inSymmetry = false;
}
}else if(inLis.isInArithmetic){
ge.drawArithmeticChoice = true;
ge.drawMainMenu = false;
}
else{
if(!inLis.isInAlgebra || !inLis.isInGeometry){
//do not draw sub-section choices of buttons
ge.drawAlgebraChoices = false;
ge.drawGeometryChoices = false;
ge.drawArithmeticChoice = false;
//draw main menu section choices
ge.drawMainMenu = true;
//do not draw sub-section images
ge.database.inAlgebra = false;
ge.database.inGeometry = false;
//take care of rest variables
ge.database.inLinearEquat = false;
ge.database.inSystemOfEquat = false;
}
}
}
}
}