As a JAVA beginner I ran in a bit a problem.
The program runs fine but everytime after several compilations there are JButtons created in the JButtons...
I think there is a big structural mistake but I can't seem to find where
Code of MainProgram.java
CODE
package wieIsHet;
public class MainProgram
{
private static boolean restart = true;
public static void main(String[] args)
{
Singleton singleton = new Singleton();
//for(;restart;)
//{
Game game = new Game();
game.start();
//}
}
}
Code of Game.java
CODE
package wieIsHet;
public class Game
{
private Lijst lijst;
private Singleton singleton;
public void start()
{
lijst = new Lijst();
singleton = Singleton.getSingletonInstance();
singleton.geefLijst(lijst);
singleton.venster2.teken(1);
singleton.plaatsLabels();
}
}
Code of Lijst.java
CODE
package wieIsHet;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JOptionPane;
public class Lijst
{
private ArrayList<Persoon> personen;
public List<Persoon> gekozenPersonen;
private Singleton singleton;
public Lijst()
{
singleton = Singleton.getSingletonInstance();
try {
//lees de personen in
BufferedReader lezen = new BufferedReader(new FileReader("personen.txt"));
personen = new ArrayList<Persoon>();
gekozenPersonen = new ArrayList<Persoon>();
String s = lezen.readLine();
for(;s!=null;)
{
//splits in kenmerken en maak nieuw object persoon aan
String[] temp = s.split(";");
personen.add(new Persoon(temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], temp[8]));
s = lezen.readLine();
}
lezen.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null,
"De lijst van personen kon niet ingeladen worden",
"Error 2!", JOptionPane.OK_OPTION);
System.exit(2);
} catch (IOException e)
{
JOptionPane.showMessageDialog(null,
"De lijst van personen kon niet ingeladen worden",
"Error 3!", JOptionPane.OK_OPTION);
System.exit(3);
}
if(personen.size() < 20)
{
System.exit(4);
}
else
{
Random willekeurig = new Random();
for(int i = 0; i < 20; i++)
{
//kies 20 willekeurige personen
int randomCijfer = willekeurig.nextInt(personen.size());
gekozenPersonen.add(personen.get(randomCijfer));
personen.remove(randomCijfer);
}
}
}
}
Code of Persoon.java
CODE
package wieIsHet;
public class Persoon
{
private String pad;
private String haarKleur;
private String naam;
private String haarType;
private String ogenKleur;
private String ornament;
private String leeftijd;
private String huidsKleur;
private String geslacht;
private boolean geraden = false;
public Persoon(String pad, String naam, String haarKleur, String haarType, String ogenKleur, String ornament, String leeftijd, String huidsKleur, String geslacht)
{
this.pad = pad;
this.naam = naam;
this.haarKleur = haarKleur;
this.haarType = haarType;
this.ogenKleur = ogenKleur;
this.ornament = ornament;
this.leeftijd = leeftijd;
this.huidsKleur = huidsKleur;
this.geslacht = geslacht;
}
public boolean isGeraden() {
return geraden;
}
public void setGeraden(boolean geraden) {
this.geraden = geraden;
}
public String getPad() {
return pad;
}
public String getHaarKleur() {
return haarKleur;
}
public String getNaam() {
return naam;
}
public String getHaarType() {
return haarType;
}
public String getOgenKleur() {
return ogenKleur;
}
public String getOrnament() {
return ornament;
}
public String getLeeftijd() {
return leeftijd;
}
public String getHuidsKleur() {
return huidsKleur;
}
public String getGeslacht() {
return geslacht;
}
}
Code of Singleton.java
CODE
package wieIsHet;
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Singleton extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
private static Singleton instance;
private GraphicsDevice graphicsDevice;
public JFrame venster;
private DisplayMode origDisplayMode;
public MyFrame venster2;
public Lijst lijst;
public JButton[] buttons;
public JButton button1;
public JButton button2;
public JButton button3;
public JButton button4;
public JButton button5;
public JButton button6;
public JButton button7;
public JButton button8;
public JButton button9;
public JButton button10;
public JButton button11;
public JButton button12;
public JButton button13;
public JButton button14;
public JButton button15;
public JButton button16;
public JButton button17;
public JButton button18;
public JButton button19;
public JButton button20;
protected Singleton()
{
super();
//verkrijg scherminstellingen
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] devices = graphicsEnvironment.getScreenDevices();
FullScreen01(devices[0]);
origDisplayMode = graphicsDevice.getDisplayMode();
maakFrame();
}
public static Singleton getSingletonInstance()
{
if(instance==null)
{
instance = new Singleton();
}
return instance;
}
public void FullScreen01(GraphicsDevice graphicsDevice)
{
this.graphicsDevice = graphicsDevice;
}
public void maakFrame()
{
venster2 = new MyFrame();
//minimale resolutie van 800 x 600
if(origDisplayMode.getHeight() >= 600 && origDisplayMode.getWidth() >= 800)
{
if(graphicsDevice.isFullScreenSupported())
{
venster2.setUndecorated(true);
venster2.setResizable(false);
DisplayMode tempDisplay = new DisplayMode(origDisplayMode.getWidth(), origDisplayMode.getHeight(), 32, 60);
graphicsDevice.setFullScreenWindow(venster2);
graphicsDevice.setDisplayMode(tempDisplay);
venster2.copyData(0, origDisplayMode, venster2, graphicsDevice);
}
else
{
JOptionPane.showMessageDialog(venster2,
"Uw computer of grafische kaart ondersteund geen volledig scherm,\n" +
"installeer de laatste nieuwe drivers en probeer opnieuw.",
"Error 0!", JOptionPane.OK_OPTION);
System.exit(0);
}
}
else
{
JOptionPane.showMessageDialog(venster2,
"Uw resolutie moet minstens 800 x 600 pixels berdragen,\n" +
"verhoog uw resolutie en probeer opnieuw.",
"Error 1!", JOptionPane.OK_OPTION);
System.exit(1);
}
}
public void endGame()
{
graphicsDevice.setDisplayMode(origDisplayMode);
System.exit(2);
}
public void vulTabelButtons()
{
buttons = new JButton[20];
for(int i = 0; i < 20; i++)
{
buttons[i] = new JButton();
}
buttons[0].add(button1);
buttons[1].add(button2);
buttons[2].add(button3);
buttons[3].add(button4);
buttons[4].add(button5);
buttons[5].add(button6);
buttons[6].add(button7);
buttons[7].add(button8);
buttons[8].add(button9);
buttons[9].add(button10);
buttons[10].add(button11);
buttons[11].add(button12);
buttons[12].add(button13);
buttons[13].add(button14);
buttons[14].add(button15);
buttons[15].add(button16);
buttons[16].add(button17);
buttons[17].add(button18);
buttons[18].add(button19);
buttons[19].add(button20);
}
public void newButtons()
{
button1 = new JButton();
button2 = new JButton();
button3 = new JButton();
button4 = new JButton();
button5 = new JButton();
button6 = new JButton();
button7 = new JButton();
button8 = new JButton();
button9 = new JButton();
button10 = new JButton();
button11 = new JButton();
button12 = new JButton();
button13 = new JButton();
button14 = new JButton();
button15 = new JButton();
button16 = new JButton();
button17 = new JButton();
button18 = new JButton();
button19 = new JButton();
button20 = new JButton();
}
public void plaatsLabels()
{
venster2.getContentPane().setLayout(null);
//venster2.setLayout(null);
setLayout(null);
int stapX = (int) ((origDisplayMode.getWidth() - 300 - 500)/6);
int stapY = (int) ((origDisplayMode.getHeight() - 200 - 400)/5);
int cnt = 0;
newButtons();
vulTabelButtons();
for(int i = 0; i < 5 && cnt < 20; i++)
{
for(int j = 0; j < 4 && cnt < 20; j++)
{
Persoon persoon = lijst.gekozenPersonen.get(cnt);
ImageIcon iconGekozen = new ImageIcon("images/wie is het.png");
String path = "images/" + persoon.getPad();
try {
BufferedImage foto = ImageIO.read(new File(path));
BufferedImage bi = new BufferedImage(85, 85,
BufferedImage.TYPE_INT_ARGB);
bi.getGraphics().drawImage(foto, 0, 0, 85, 85, null);
ImageIcon icon = new ImageIcon(bi);
if(persoon.isGeraden())
{
buttons[cnt].setBorder(null);
buttons[cnt].setEnabled(false);
buttons[cnt].setIcon(iconGekozen);
buttons[cnt].setDisabledIcon(iconGekozen);
}
else
{
buttons[cnt].setIcon(icon);
}
buttons[cnt].setBounds(((150 + 100 * i + stapX + stapX * i)+15/2), ((100 + 100 * j + stapY + stapY * j)+15/2), 85, 85);
venster2.getContentPane().add(buttons[cnt]);
} catch (IOException e) {
JOptionPane.showMessageDialog(venster,
"Een foto kon niet geladen worden",
"Error 5!", JOptionPane.OK_OPTION);
System.exit(5);
}
cnt++;
}
}
for(int i = 0; i < 20; i++)
{
buttons[i].requestFocus();
}
}
public void geefLijst(Lijst lijst)
{
this.lijst = lijst;
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
static class MyFrame extends JFrame {
private static final long serialVersionUID = 2L;
private Integer keuze = 0;
private DisplayMode origDisplayMode;
private Color c;
private boolean switcher = false;
private Color c2;
private GradientPaint gradatie;
private Image bordje;
private MyFrame venster;
private JPanel labels;
private JFrame frame;
private GraphicsDevice graphicsDevice;
public void copyData(Integer keuze, DisplayMode origDisplayMode, MyFrame venster, GraphicsDevice graphicsDevice)
{
this.keuze = keuze;
this.origDisplayMode = origDisplayMode;
this.graphicsDevice = graphicsDevice;
switcher = true;
this.venster = venster;
keuze = 0;
repaint();
}
public void teken(Integer keuze)
{
this.keuze = keuze;
repaint();
}
public void paint(Graphics g) {
if(switcher)
{
switcher = true;
if(keuze == 0)
{
//teken achtergrond
c = new Color(186,171,250);
g.setColor(c);
g.fillRect(0, 0, origDisplayMode.getWidth(), origDisplayMode.getHeight());
}
if(keuze == 1)
{
c = new Color(186,171,250);
g.setColor(c);
g.fillRect(0, 0, origDisplayMode.getWidth(), origDisplayMode.getHeight());
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
c2 = new Color(53,32,140);
Rectangle2D l = new Rectangle2D.Float(0,0,150,origDisplayMode.getHeight());
gradatie = new GradientPaint(0,0,c2,150,0,c);
g2.setPaint(gradatie);
g2.fill(l);
Rectangle2D r = new Rectangle2D.Float((origDisplayMode.getWidth()-150),0, origDisplayMode.getWidth(), origDisplayMode.getHeight());
gradatie = new GradientPaint((origDisplayMode.getWidth()-150),0,c,origDisplayMode.getWidth(),0,c2);
g2.setPaint(gradatie);
g2.fill(r);
//middenscherm
int stapX = (int) ((origDisplayMode.getWidth() - 300 - 500)/6);
int stapY = (int) ((origDisplayMode.getHeight() - 200 - 400)/5);
ImageIcon icon = new ImageIcon("images/bordje.png");
Image bordje = icon.getImage();
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 4; j++)
{
g.drawImage(bordje, (150 + 100 * i + stapX + stapX * i), (100 + 100 * j + stapY + stapY * j), null);
}
}
}
if(keuze == 2)
{
}
}
}
}
}