Painting canvas and System.out.println() not working - java

I have JFrame with a start button, which triggers the calculation of a Julia Set.
The code that is executed when the start button is clicked is as follows:
public void actionPerformed(ActionEvent aActionEvent)
{
String strCmd = aActionEvent.getActionCommand();
if (strCmd.equals("Start"))
{
m_cCanvas.init();
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_bRunning = true;
this.handleCalculation();
}
else if (aActionEvent.getSource() == m_cTReal)
Which used to work fine, except that the application could not be closed anymore. So I tried to use m_bRunning in a separate method so that actionPerformed() isn't blocked all the time to see if that would help, and then set m_bRunning = false in the method stop() which is called when the window is closed:
public void run()
{
if(m_bRunning)
{
this.handleCalculation();
}
}
The method run() is called from the main class in a while(true) loop.
Yet unfortunately, neither did that solve the problem, nor do I now have any output to the canvas or any debug traces with System.out.println(). Could anyone point me in the right direction on this?
EDIT:
Here are the whole files:
// cMain.java
package juliaSet;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
public class cMain {
public static void main(String[] args)
{
int windowWidth = 1000;//(int)screenSize.getWidth() - 200;
int windowHeight = 800;//(int)screenSize.getHeight() - 50;
int plotWidth = 400;//(int)screenSize.getWidth() - 600;
int plotHeight = 400;//(int)screenSize.getHeight() - 150;
JuliaSet cJuliaSet = new JuliaSet("Julia Set", windowWidth, windowHeight, plotWidth, plotHeight);
cJuliaSet.setVisible(true);
while(true)
{
cJuliaSet.run();
}
}
}
// JuliaSet.java
package juliaSet;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.Random;
import java.io.*;
import java.lang.ref.*;
public class JuliaSet extends JFrame implements ActionListener
{
private JButton m_cBStart;
private JTextField m_cTReal;
private JTextField m_cTImag;
private JTextField m_cTDivergThresh;
private JLabel m_cLReal;
private JLabel m_cLImag;
private JLabel m_cLDivergThresh;
private int m_iDivergThresh = 10;
private String m_cMsgDivThresh = "Divergence threshold = " + m_iDivergThresh;
private JuliaCanvas m_cCanvas;
private int m_iPlotWidth; // number of cells
private int m_iPlotHeight; // number of cells
private Boolean m_bRunning = false;
private double m_dReal = 0.3;
private double m_dImag = -0.5;
private String m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
private String m_cMsgIter = "x = 0, y = 0";
private Complex m_cCoordPlane[][];
private double m_dAbsSqValues[][];
private int m_iIterations[][];
private Complex m_cSummand;
private BufferedImage m_cBackGroundImage = null;
private FileWriter m_cFileWriter;
private BufferedWriter m_cBufferedWriter;
private String m_sFileName = "log.txt";
private Boolean m_bWriteLog = false;
private static final double PLOTMAX = 2.0; // we'll have symmetric axes
// ((0,0) at the centre of the
// plot
private static final int MAXITER = 0xff;
JuliaSet(String aTitle, int aFrameWidth, int aFrameHeight, int aPlotWidth, int aPlotHeight)
{
super(aTitle);
this.setSize(aFrameWidth, aFrameHeight);
m_iPlotWidth = aPlotWidth;
m_iPlotHeight = aPlotHeight;
m_cSummand = new Complex(m_dReal, m_dImag);
m_cBackGroundImage = new BufferedImage(aFrameWidth, aFrameHeight, BufferedImage.TYPE_INT_RGB);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
stop();
super.windowClosing(e);
System.exit(0);
}
});
GridBagLayout cLayout = new GridBagLayout();
GridBagConstraints cConstraints = new GridBagConstraints();
this.setLayout(cLayout);
m_cCanvas = new JuliaCanvas(m_iPlotWidth, m_iPlotHeight);
m_cCanvas.setSize(m_iPlotWidth, m_iPlotHeight);
m_cBStart = new JButton("Start");
m_cBStart.addActionListener(this);
m_cTReal = new JTextField(5);
m_cTReal.addActionListener(this);
m_cTImag = new JTextField(5);
m_cTImag.addActionListener(this);
m_cTDivergThresh = new JTextField(5);
m_cTDivergThresh.addActionListener(this);
m_cLReal = new JLabel("Re(c):");
m_cLImag = new JLabel("Im(c):");
m_cLDivergThresh = new JLabel("Divergence Threshold:");
cConstraints.insets.top = 3;
cConstraints.insets.bottom = 3;
cConstraints.insets.right = 3;
cConstraints.insets.left = 3;
// cCanvas
cConstraints.gridx = 0;
cConstraints.gridy = 0;
cLayout.setConstraints(m_cCanvas, cConstraints);
this.add(m_cCanvas);
// m_cLReal
cConstraints.gridx = 0;
cConstraints.gridy = 1;
cLayout.setConstraints(m_cLReal, cConstraints);
this.add(m_cLReal);
// m_cTReal
cConstraints.gridx = 1;
cConstraints.gridy = 1;
cLayout.setConstraints(m_cTReal, cConstraints);
this.add(m_cTReal);
// m_cLImag
cConstraints.gridx = 0;
cConstraints.gridy = 2;
cLayout.setConstraints(m_cLImag, cConstraints);
this.add(m_cLImag);
// m_cTImag
cConstraints.gridx = 1;
cConstraints.gridy = 2;
cLayout.setConstraints(m_cTImag, cConstraints);
this.add(m_cTImag);
// m_cLDivergThresh
cConstraints.gridx = 0;
cConstraints.gridy = 3;
cLayout.setConstraints(m_cLDivergThresh, cConstraints);
this.add(m_cLDivergThresh);
// m_cTDivergThresh
cConstraints.gridx = 1;
cConstraints.gridy = 3;
cLayout.setConstraints(m_cTDivergThresh, cConstraints);
this.add(m_cTDivergThresh);
// m_cBStart
cConstraints.gridx = 0;
cConstraints.gridy = 4;
cLayout.setConstraints(m_cBStart, cConstraints);
this.add(m_cBStart);
if (m_bWriteLog)
{
try
{
m_cFileWriter = new FileWriter(m_sFileName, false);
m_cBufferedWriter = new BufferedWriter(m_cFileWriter);
} catch (IOException ex) {
System.out.println("Error opening file '" + m_sFileName + "'");
}
}
this.repaint();
this.transformCoordinates();
}
public synchronized void stop()
{
if (m_bRunning)
{
m_bRunning = false;
boolean bRetry = true;
}
if (m_bWriteLog)
{
try {
m_cBufferedWriter.close();
m_cFileWriter.close();
} catch (IOException ex) {
System.out.println("Error closing file '" + m_sFileName + "'");
}
}
}
public void collectGarbage()
{
Object cObj = new Object();
WeakReference ref = new WeakReference<Object>(cObj);
cObj = null;
while(ref.get() != null) {
System.gc();
}
}
public void setSummand(Complex aSummand)
{
m_cSummand.setIm(aSummand.getIm());
m_dImag = aSummand.getIm();
m_cSummand.setRe(aSummand.getRe());
m_dReal = aSummand.getRe();
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
}
public void paint(Graphics aGraphics)
{
Graphics cScreenGraphics = aGraphics;
// render on background image
aGraphics = m_cBackGroundImage.getGraphics();
this.paintComponents(aGraphics);
// drawString() calls are debug code only....
aGraphics.setColor(Color.BLACK);
aGraphics.drawString(m_cSMsg, 10, 450);
aGraphics.drawString(m_cMsgIter, 10, 465);
aGraphics.drawString(m_cMsgDivThresh, 10, 480);
// rendering is done, draw background image to on screen graphics
cScreenGraphics.drawImage(m_cBackGroundImage, 0, 0, null);
}
public void actionPerformed(ActionEvent aActionEvent)
{
String strCmd = aActionEvent.getActionCommand();
if (strCmd.equals("Start"))
{
m_cCanvas.init();
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_bRunning = true;
}
else if (aActionEvent.getSource() == m_cTReal)
{
m_dReal = Double.parseDouble(m_cTReal.getText());
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_cSummand.setRe(m_dReal);
}
else if (aActionEvent.getSource() == m_cTImag)
{
m_dImag = Double.parseDouble(m_cTImag.getText());
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_cSummand.setIm(m_dImag);
}
else if (aActionEvent.getSource() == m_cTDivergThresh)
{
m_iDivergThresh = Integer.parseInt(m_cTDivergThresh.getText());
m_cMsgDivThresh = "Divergence threshold = " + m_iDivergThresh;
}
this.update(this.getGraphics());
}
public void transformCoordinates()
{
double dCanvasHeight = (double) m_cCanvas.getHeight();
double dCanvasWidth = (double) m_cCanvas.getWidth();
// init matrix with same amount of elements as pixels in canvas
m_cCoordPlane = new Complex[(int) dCanvasHeight][(int) dCanvasWidth];
double iPlotRange = 2 * PLOTMAX;
for (int i = 0; i < dCanvasHeight; i++)
{
for (int j = 0; j < dCanvasWidth; j++)
{
m_cCoordPlane[i][j] = new Complex((i - (dCanvasWidth / 2)) * iPlotRange / dCanvasWidth,
(j - (dCanvasHeight / 2)) * iPlotRange / dCanvasHeight);
}
}
}
public void calcAbsSqValues()
{
int iCanvasHeight = m_cCanvas.getHeight();
int iCanvasWidth = m_cCanvas.getWidth();
// init matrix with same amount of elements as pixels in canvas
m_dAbsSqValues = new double[iCanvasHeight][iCanvasWidth];
m_iIterations = new int[iCanvasHeight][iCanvasWidth];
Complex cSum = new Complex();
if (m_bWriteLog) {
try
{
m_cBufferedWriter.write("m_iIterations[][] =");
m_cBufferedWriter.newLine();
}
catch (IOException ex)
{
System.out.println("Error opening file '" + m_sFileName + "'");
}
}
for (int i = 0; i < iCanvasHeight; i++)
{
for (int j = 0; j < iCanvasWidth; j++)
{
cSum.setRe(m_cCoordPlane[i][j].getRe());
cSum.setIm(m_cCoordPlane[i][j].getIm());
m_iIterations[i][j] = 0;
do
{
m_iIterations[i][j]++;
cSum.square();
cSum.add(m_cSummand);
m_dAbsSqValues[i][j] = cSum.getAbsSq();
} while ((m_iIterations[i][j] < MAXITER) && (m_dAbsSqValues[i][j] < m_iDivergThresh));
this.calcColour(i, j, m_iIterations[i][j]);
m_cMsgIter = "x = " + i + " , y = " + j;
if(m_bWriteLog)
{
System.out.println(m_cMsgIter);
System.out.flush();
}
if (m_bWriteLog) {
try
{
m_cBufferedWriter.write(Integer.toString(m_iIterations[i][j]));
m_cBufferedWriter.write(" ");
}
catch (IOException ex) {
System.out.println("Error writing to file '" + m_sFileName + "'");
}
}
}
if (m_bWriteLog) {
try
{
m_cBufferedWriter.newLine();
}
catch (IOException ex) {
System.out.println("Error writing to file '" + m_sFileName + "'");
}
}
}
m_dAbsSqValues = null;
m_iIterations = null;
cSum = null;
}
private void calcColour(int i, int j, int aIterations)
{
Color cColour = Color.getHSBColor((int) Math.pow(aIterations, 4), 0xff,
0xff * ((aIterations < MAXITER) ? 1 : 0));
m_cCanvas.setPixelColour(i, j, cColour);
cColour = null;
}
private void handleCalculation()
{
Complex cSummand = new Complex();
for(int i = -800; i <= 800; i++)
{
for(int j = -800; j <= 800; j++)
{
cSummand.setRe(((double)i)/1000.0);
cSummand.setIm(((double)j)/1000.0);
this.setSummand(cSummand);
this.calcAbsSqValues();
this.getCanvas().paint(m_cCanvas.getGraphics());
this.paint(this.getGraphics());
}
}
cSummand = null;
this.collectGarbage();
System.gc();
System.runFinalization();
}
public boolean isRunning()
{
return m_bRunning;
}
public void setRunning(boolean aRunning)
{
m_bRunning = aRunning;
}
public Canvas getCanvas()
{
return m_cCanvas;
}
public void run()
{
if(m_bRunning)
{
this.handleCalculation();
}
}
}
class JuliaCanvas extends Canvas
{
private int m_iWidth;
private int m_iHeight;
private Random m_cRnd;
private BufferedImage m_cBackGroundImage = null;
private int m_iRed[][];
private int m_iGreen[][];
private int m_iBlue[][];
JuliaCanvas(int aWidth, int aHeight)
{
m_iWidth = aWidth;
m_iHeight = aHeight;
m_cRnd = new Random();
m_cRnd.setSeed(m_cRnd.nextLong());
m_cBackGroundImage = new BufferedImage(m_iWidth, m_iHeight, BufferedImage.TYPE_INT_RGB);
m_iRed = new int[m_iHeight][m_iWidth];
m_iGreen = new int[m_iHeight][m_iWidth];
m_iBlue = new int[m_iHeight][m_iWidth];
}
public void init() {
}
public void setPixelColour(int i, int j, Color aColour)
{
m_iRed[i][j] = aColour.getRed();
m_iGreen[i][j] = aColour.getGreen();
m_iBlue[i][j] = aColour.getBlue();
}
private int getRandomInt(double aProbability)
{
return (m_cRnd.nextDouble() < aProbability) ? 1 : 0;
}
#Override
public void paint(Graphics aGraphics)
{
// store on screen graphics
Graphics cScreenGraphics = aGraphics;
// render on background image
aGraphics = m_cBackGroundImage.getGraphics();
for (int i = 0; i < m_iWidth; i++)
{
for (int j = 0; j < m_iHeight; j++)
{
Color cColor = new Color(m_iRed[i][j], m_iGreen[i][j], m_iBlue[i][j]);
aGraphics.setColor(cColor);
aGraphics.drawRect(i, j, 0, 0);
cColor = null;
}
}
// rendering is done, draw background image to on screen graphics
cScreenGraphics.drawImage(m_cBackGroundImage, 1, 1, null);
}
#Override
public void update(Graphics aGraphics)
{
paint(aGraphics);
}
}
class Complex {
private double m_dRe;
private double m_dIm;
public Complex()
{
m_dRe = 0;
m_dIm = 0;
}
public Complex(double aRe, double aIm)
{
m_dRe = aRe;
m_dIm = aIm;
}
public Complex(Complex aComplex)
{
m_dRe = aComplex.m_dRe;
m_dIm = aComplex.m_dIm;
}
public double getRe() {
return m_dRe;
}
public void setRe(double adRe)
{
m_dRe = adRe;
}
public double getIm() {
return m_dIm;
}
public void setIm(double adIm)
{
m_dIm = adIm;
}
public void add(Complex acComplex)
{
m_dRe += acComplex.getRe();
m_dIm += acComplex.getIm();
}
public void square()
{
double m_dReSave = m_dRe;
m_dRe = (m_dRe * m_dRe) - (m_dIm * m_dIm);
m_dIm = 2 * m_dReSave * m_dIm;
}
public double getAbsSq()
{
return ((m_dRe * m_dRe) + (m_dIm * m_dIm));
}
}

I'm quoting a recent comment from #MadProgrammer (including links)
"Swing is single threaded, nothing you can do to change that, all events are posted to the event queue and processed by the Event Dispatching Thread, see Concurrency in Swing for more details and have a look at Worker Threads and SwingWorker for at least one possible solution"
There is only one thread in your code. That thread is busy doing the calculation and can not respond to events located in the GUI. You have to separate the calculation in another thread that periodically updates the quantities that appears in the window. More info about that in the links, courtesy of #MadProgrammer, I insist.
UPDATED: As pointed by #Yusuf, the proper way of launching the JFrame is
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new JuliaSet("Julia Set", windowWidth, windowHeight, plotWidth, plotHeight);
}
});
Set the frame visible on construction and start calculation when the start button is pressed.

First;
Endless loop is not a proper way to do this. This part is loops and taking CPU and never give canvas to refresh screen. if you add below code your code will run as expected. but this is not the proper solution.
cMain.java:
while (true) {
cJuliaSet.run();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Second: you could call run method when start button clicked. But you should create a thread in run method to not freeze screen.
public static void main(String[] args) {
int windowWidth = 1000;// (int)screenSize.getWidth() - 200;
int windowHeight = 800;// (int)screenSize.getHeight() - 50;
int plotWidth = 400;// (int)screenSize.getWidth() - 600;
int plotHeight = 400;// (int)screenSize.getHeight() - 150;
JuliaSet cJuliaSet = new JuliaSet("Julia Set", windowWidth, windowHeight, plotWidth, plotHeight);
cJuliaSet.setVisible(true);
//While loop removed
}
actionPerformed:
if (strCmd.equals("Start")) {
m_cCanvas.init();
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_bRunning = true;
this.run(); // added call run method.
} else if (aActionEvent.getSource() == m_cTReal) {
run method:
public void run()
{
if(m_bRunning)
{
new Thread(){ //Thread to release screen
#Override
public void run() {
JuliaSet.this.handleCalculation();
}
}.start(); //also thread must be started
}
}
As said by #RubioRic, SwingUtilities.invokeLater method is also a part of solution. But you need to check whole of your code and you should learn about Threads.

Related

Why is the reset button for my game not working?

I have made a reset button which doesn't take away the focus of the KeyListener so that the game could still be playable after the clicking of the button. The rest of the button is all about resetting locations, emptying arraylists and arrays and so on. But it seems like whenever I click the restart button, the player object does go to its starting point, x = 2 y = 0. But after that whenever I make a move I see the man object/picture move to the position in was in before so the restart button isn't working at all. The player object is actually still at the location in which it stood when the restart button was clicked. I have read that there might be a second panel added beneath and thus the real panel not changing? I'm not sure and require your help.
My question specific: Why does the reset button not suffice for the changing of the player/man objects location, why is it failing?
package riddle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameFrame extends JFrame {
private JPanel p2;
private JButton reset;
private GameDraw component;
private Man m;
public GameFrame() {
this.setTitle("Riddle Man");
this.setSize(719, 850);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
component = new GameDraw();
m = new Man(component);
component.addKeyListener(m);
component.requestFocus();
createButton();
createPanel();
}
private void createButton() {
reset = new JButton("Restart");
ActionListener listener = new ClickListener();
reset.addActionListener(listener);
}
private void createPanel() {
p2 = new JPanel();
p2.setLayout(null);
component.setBounds(0, 0, 800, 800);
reset.setBounds(0, 725, 100, 50);
p2.add(component);
p2.add(reset);
add(p2);
}
class ClickListener implements ActionListener {
public ClickListener(){
}
#Override
public void actionPerformed(ActionEvent e) {
reset.setFocusable(false);
component.resetGame();
}
}
}
package riddle;
import org.apache.commons.lang3.StringUtils;
import java.awt.Color;
import static java.awt.Frame.NORMAL;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class GameField {
public String[][] field;
private String grass, wall, note, end, theRiddle, theAnswer;
private ArrayList<Integer> arrayG;
private ArrayList<Integer> arrayW;
private ArrayList<Integer> arrayN;
private ArrayList<Integer> arrayE;
private Riddle riddle;
private int instantRight, count, wrongs, retries;
private boolean ok = false;
public GameField() {
field = new String[10][10];
grass = "Grass";
wall = "Wall";
note = "Note";
end = "End";
theRiddle = "";
theAnswer = "";
instantRight = 0;
count = 0;
wrongs = 0;
retries = 0;
arrayG = new ArrayList<Integer>(Arrays.asList(0, 9, 10, 19, 20, 29, 30, 39, 40, 49, 50, 59, 60, 69, 70, 79, 80, 89, 90, 99));
arrayW = new ArrayList<Integer>(Arrays.asList(1, 3, 4, 5, 6, 7, 8, 11, 18, 21, 22, 23, 24, 25, 26, 28, 31, 38, 41, 43, 44, 45, 46, 47, 48, 51, 58, 61, 62, 63, 64, 65, 66, 68, 71, 78, 81, 83, 84, 85, 86, 87, 88, 91, 98));
arrayN = new ArrayList<Integer>(Arrays.asList(17, 35, 32, 54, 57, 76, 72, 94));
arrayE = new ArrayList<Integer>(Arrays.asList(97));
riddle = new Riddle();
fillField();
}
public String checkField(int x, int y) {
String result = "";
if (field[y][x] == "Wall") {
result = wall;
} else if (field[y][x] == "Note") {
result = note;
} else if (field[y][x] == "End") {
result = end;
endGame();
}
return result;
}
private void fillField() {
int k = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (arrayG.contains(k)) {
field[i][j] = grass;
} else if (arrayW.contains(k)) {
field[i][j] = wall;
} else if (arrayN.contains(k)) {
field[i][j] = note;
} else if (arrayE.contains(k)) {
field[i][j] = end;
}
k++;
}
}
}
public void setField(int x, int y) {
field[x][y] = null;
}
public boolean riddleTime() {
boolean check = false;
theRiddle = riddle.generateRiddle();
theAnswer = riddle.getAnswer();
while (!check) {
check = checkReply(theRiddle, theAnswer);
}
return check;
}
public boolean checkReply(String question, String answer) {
ImageIcon image = new ImageIcon(this.getClass().getResource("/resources/bad.png"));
ImageIcon image2 = new ImageIcon(this.getClass().getResource("/resources/good.png"));
String[] option = {"Yes", "Quit"};
String reply = JOptionPane.showInputDialog(question);
if (reply.contains(answer.toLowerCase()) || reply.contains(StringUtils.capitalize(answer)) || reply.contains(answer.toUpperCase())) {
JOptionPane.showMessageDialog(null, riddle.getResponseC(), "", NORMAL, image2);
ok = true;
if (count == 0) {
instantRight++;
} else {
count = 0;
}
} else {
wrongs++;
int selectedValue = JOptionPane.showOptionDialog(null, riddle.getResponseW(), "", JOptionPane.YES_NO_OPTION, JOptionPane.YES_NO_OPTION, image, option, NORMAL);
if (selectedValue == JOptionPane.YES_OPTION) {
checkReply(question, answer);
retries++;
}
if (selectedValue == JOptionPane.NO_OPTION) {
System.exit(0);
}
count++;
}
return ok;
}
private void endGame() {
ImageIcon bad = new ImageIcon(this.getClass().getResource("/resources/endingbad.png"));
ImageIcon good = new ImageIcon(this.getClass().getResource("/resources/endinggood.png"));
ImageIcon info = new ImageIcon(this.getClass().getResource("/resources/info.png"));
if (instantRight >= 6) {
JOptionPane.showMessageDialog(null, "", "", NORMAL, good);
JOptionPane.showMessageDialog(null, "<html><center><br><br>Instant Rights: " + instantRight + "\n" + "\n" + "Wrongs: " + wrongs + "\n" + "\n" + "Retries: " + retries + "\n" + "\n" + " Wisdom Level: " + indicate(instantRight, wrongs, retries), " Statistics", NORMAL, info);
} else {
JOptionPane.showMessageDialog(null, "", "", NORMAL, bad);
JOptionPane.showMessageDialog(null, "\n" + "Instant Rights: " + instantRight + "\n" + "\n" + "Wrongs: " + wrongs + "\n" + "\n" + "Retries: " + retries + "\n" + "\n" + " Wisdom Level: " + indicate(instantRight, wrongs, retries), " Statistics", NORMAL, info);
}
}
public String indicate(int instantRight, int wrongs, int retries) {
String level = "";
if (instantRight >= 6 && wrongs <= 4 && retries <= 4) {
level = "HIGH";
} else if (instantRight <= 3 && wrongs > 6 && retries > 6) {
level = "LOW";
} else {
level = "AVERAGE";
}
return level;
}
public void resetGame(){
fillField();
riddle.resetGame();
instantRight = 0;
wrongs = 0;
retries = 0;
count = 0;
}
}
package riddle;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
public class GameDraw extends JComponent {
private int rows;
private int columns;
private int RWIDTH;
private int RHEIGHT;
private int manX;
private int manY;
private BufferedImage man, grass, wall, note, end;
private Man m;
private GameField f;
public GameDraw() {
//Integer variables
rows = 9;
columns = 9;
RWIDTH = 70;
RHEIGHT = 70;
manX = 2;
manY = 0;
f = new GameField();
m = new Man(this);
this.setFocusable(true);
URL resourceMan = this.getClass().getResource("/resources/man.png");
try {
man = ImageIO.read(resourceMan);
} catch (IOException e) {
System.out.println("Er ging iets mis met het laden van de afbeelding van de speler");
}
}
#Override
public void paintComponent(Graphics g) {
drawField(g);
drawMan(g);
drawObjects(g);
}
private void drawField(Graphics g) {
int x = 0;
int y = 0;
Color lightgray = new Color(192, 192, 192);
for (int i = 0; i < rows + 1; i++) {
for (int j = 0; j < columns; j++) {
g.setColor(lightgray);
g.fillRect(x, y, RWIDTH, RHEIGHT);
x += RWIDTH;
}
g.setColor(lightgray);
g.fillRect(x, y, RWIDTH, RHEIGHT);
x = 0;
y += RHEIGHT;
}
}
private void drawMan(Graphics g) {
g.drawImage(man, manX * RWIDTH, manY * RHEIGHT, RWIDTH, RHEIGHT, this);
}
public void moveMan(int x, int y) {
manX = x;
manY = y;
repaint();
System.out.println(manX + " " + manY);
}
private void drawObjects(Graphics g) {
URL resourceGrass = this.getClass().getResource("/resources/Grassy.png");
URL resourceWall = this.getClass().getResource("/resources/wall.png");
URL resourceNote = this.getClass().getResource("/resources/note.png");
URL resourceEnd = this.getClass().getResource("/resources/chest.png");
//Grass object
try {
grass = ImageIO.read(resourceGrass);
wall = ImageIO.read(resourceWall);
note = ImageIO.read(resourceNote);
end = ImageIO.read(resourceEnd);
} catch (IOException e) {
System.out.println("Er ging iets mis met het laden van de afbeelding van de speler");
}
for (int j = 0; j < rows + 1; j++) {
for (int i = 0; i < columns + 1; i++) {
if (f.field[i][j] == "Grass") {
g.drawImage(grass, j * RWIDTH, i * RHEIGHT, RWIDTH, RHEIGHT, this);
}
if (f.field[i][j] == "Wall") {
g.drawImage(wall, j * RWIDTH, i * RHEIGHT, RWIDTH, RHEIGHT, this);
}
if (f.field[i][j] == "Note") {
g.drawImage(note, j * RWIDTH, i * RHEIGHT, RWIDTH, RHEIGHT, this);
}
if (f.field[i][j] == "End") {
g.drawImage(end, j * RWIDTH, i * RHEIGHT, RWIDTH, RHEIGHT, this);
}
}
}
}
public void repaintField(int x, int y) {
f.field[x][y] = null;
repaint();
}
public void resetGame(){
f.resetGame();
m.resetGame();
System.out.println(manX + " " + manY);
}
}
package riddle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Man implements KeyListener {
private int locationX;
private int locationY;
private GameDraw draw;
private GameField field;
public Man(GameDraw draw) {
locationX = 2;
locationY = 0;
this.draw = draw;
field = new GameField();
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
String check;
boolean pass;
switch (key) {
case KeyEvent.VK_UP:
locationY--;
if (locationY < 0) {
locationY++;
}
check = field.checkField(locationX, locationY);
if (!"Wall".equals(check) && !"Note".equals(check) && !"End".equals(check)) {
draw.moveMan(locationX, locationY);
} else if (check.equals("Wall") || check.equals("End")) {
locationY++;
} else if (check.equals("Note")) {
pass = field.riddleTime();
if (pass) {
clearNote(locationX, locationY);
draw.moveMan(locationX, locationY);
} else {
locationY++;
}
}
break;
case KeyEvent.VK_DOWN:
locationY++;
if (locationY > 9) {
locationY--;
}
check = field.checkField(locationX, locationY);
if (!"Wall".equals(check) && !"Note".equals(check) && !"End".equals(check)) {
draw.moveMan(locationX, locationY);
}
if (check.equals("Wall") || check.equals("End")) {
locationY--;
} else if (check.equals("Note")) {
pass = field.riddleTime();
if (pass) {
clearNote(locationX, locationY);
draw.moveMan(locationX, locationY);
} else {
locationY--;
}
}
break;
case KeyEvent.VK_LEFT:
locationX--;
;
check = field.checkField(locationX, locationY);
if (!"Wall".equals(check) && !"Note".equals(check) && !"End".equals(check)) {
draw.moveMan(locationX, locationY);
}
if (check.equals("Wall") || check.equals("End")) {
locationX++;
} else if (check.equals("Note")) {
pass = field.riddleTime();
if (pass) {
clearNote(locationX, locationY);
draw.moveMan(locationX, locationY);
} else {
locationX++;
}
}
break;
case KeyEvent.VK_RIGHT:
System.out.println(locationX + " " + locationY);
locationX++;
check = field.checkField(locationX, locationY);
if (!"Wall".equals(check) && !"Note".equals(check) && !"End".equals(check)) {
draw.moveMan(locationX, locationY);
}
if (check.equals("Wall") || check.equals("End")) {
locationX--;
} else if (check.equals("Note")) {
pass = field.riddleTime();
if (pass) {
clearNote(locationX, locationY);
draw.moveMan(locationX, locationY);
} else {
locationX--;
}
}
break;
default:
break;
}
}
#Override
public void keyReleased(KeyEvent e) {
}
public void clearNote(int x, int y) {
field.setField(y, x);
draw.repaintField(y, x);
}
public void resetGame(){
locationX = 2;
locationY = 0;
draw.moveMan(locationX, locationY);
System.out.println(locationX + " " + locationY);
}
}
package riddle;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Riddle {
private ArrayList<String> riddles;
private ArrayList<String> answers;
private ArrayList<String> correct;
private ArrayList<String> wrong;
private ArrayList<Integer> used;
private ArrayList<Integer> used2;
private Random ran;
private int indexAnswer;
public Riddle() {
riddles = new ArrayList<String>();
answers = new ArrayList<String>();
correct = new ArrayList<String>();
wrong = new ArrayList<String>();
used = new ArrayList<Integer>();
used2 = new ArrayList<Integer>();
ran = new Random();
fillRiddles();
fillAnswers();
fillCorrect();
fillWrong();
}
private void fillRiddles() {
Scanner sc2 = null;
try {
sc2 = new Scanner(new File("C:\\Users\\John\\Documents\\NetBeansProjects\\Riddle\\src\\resources\\riddles.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (sc2.hasNextLine()) {
Scanner s2 = new Scanner(sc2.nextLine());
while (s2.hasNext()) {
String s = s2.nextLine();
riddles.add(s);
}
}
}
private void fillAnswers() {
Scanner sc2 = null;
try {
sc2 = new Scanner(new File("C:\\Users\\John\\Documents\\NetBeansProjects\\Riddle\\src\\resources\\answers.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (sc2.hasNextLine()) {
Scanner s2 = new Scanner(sc2.nextLine());
while (s2.hasNext()) {
String s = s2.nextLine();
answers.add(s);
}
}
}
private void fillCorrect() {
Scanner sc2 = null;
try {
sc2 = new Scanner(new File("C:\\Users\\John\\Documents\\NetBeansProjects\\Riddle\\src\\resources\\correct.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (sc2.hasNextLine()) {
Scanner s2 = new Scanner(sc2.nextLine());
while (s2.hasNext()) {
String s = s2.nextLine();
correct.add(s);
}
}
}
private void fillWrong() {
Scanner sc2 = null;
try {
sc2 = new Scanner(new File("C:\\Users\\John\\Documents\\NetBeansProjects\\Riddle\\src\\resources\\wrong.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (sc2.hasNextLine()) {
Scanner s2 = new Scanner(sc2.nextLine());
while (s2.hasNext()) {
String s = s2.nextLine();
wrong.add(s);
}
}
}
public String generateRiddle() {
String theRiddle = "";
int index = ran.nextInt(riddles.size());
if (!used.contains(index)) {
theRiddle = riddles.get(index);
indexAnswer = index;
used.add(index);
} else {
theRiddle = generateRiddle();
}
return theRiddle;
}
public String getAnswer() {
String theAnswer = answers.get(indexAnswer);
return theAnswer;
}
public String getResponseC() {
String response = "";
int index = ran.nextInt(correct.size());
if (!used2.contains(index)) {
response = correct.get(index);
used2.add(index);
} else {
response = getResponseC();
}
return response;
}
public String getResponseW() {
String response = "";
int index = ran.nextInt(wrong.size());
response = wrong.get(index);
return response;
}
public void resetGame(){
riddles.clear();
answers.clear();
wrong.clear();
correct.clear();
used.clear();
used2.clear();
fillRiddles();
fillAnswers();
fillCorrect();
fillWrong();
indexAnswer = 0;
}
}

Why is my simple java2d Space Invaders game lagging?

I'm currently making a space invaders-esque game for my software engineering course. I've already got everything working that satisfies the requirements, so this isn't a 'solve my homework' kind of question. My problem is that the game will lag (at what seems like random times & intervals) to the point where it becomes too frustrating to play. Some things I think might be causing this - though I'm not positive - are as follows:
Problem with timer event every 10 ms (I doubt this because of the very limited resources required for this game).
Problem with collision detection (checking for collision with every visible enemy every 10 ms seems like it would take up a large chunk of resources)
Problem with repainting? This seems unlikely to me however...
#SuppressWarnings("serial")
public class SIpanel extends JPanel {
private SIpanel panel;
private Timer timer;
private int score, invaderPace, pulseRate, mysteryCount, distanceToEdge;
private ArrayList<SIthing> cast;
private ArrayList<SIinvader> invaders, dead;
private ArrayList<SImissile> missileBase, missileInvader;
private SIinvader[] bottomRow;
private SIbase base;
private Dimension panelDimension;
private SImystery mysteryShip;
private boolean gameOver, left, right, mysteryDirection, space, waveDirection;
private boolean runningTimer;
private Music sound;
private void pulse() {
pace();
processInputs();
if (gameOver) gameOver();
repaint();
}
private void pace() {
// IF invaders still live
if (!invaders.isEmpty()) {
invaderPace++;
// Switch back manager
if (distanceToEdge <= 10) {
switchBack();
pulseRate = (pulseRate >= 16) ? (int) (pulseRate*(0.8)) : pulseRate;
waveDirection = !waveDirection;
distanceToEdge = calculateDistanceToEdge();
}
// Move invaders left/right
else if (invaderPace >= pulseRate) {
invaderPace = 0;
distanceToEdge = calculateDistanceToEdge();
moveAI();
invadersFire();
if (!dead.isEmpty()) removeDead();
if (mysteryCount < 1) tryInitMysteryShip();
}
// All invaders are kill, create new wave
} else if (missileBase.isEmpty() && missileInvader.isEmpty() && !cast.contains(mysteryShip)) {
// System.out.println("New Wave!");
newWave();
}
// Every pace
if (!missileBase.isEmpty()) moveMissileBase();
// Every two paces
if (invaderPace % 2 == 0) {
if (!missileInvader.isEmpty()) moveMissileInvader();
if (mysteryCount > 0) moveMysteryShip();
}
}
private void processInputs() {
if (left) move(left);
if (right) move(!right);
if (space) fireMissile(base, true);
}
protected void fireMissile(SIship ship, boolean isBase) {
if(isBase && missileBase.isEmpty()) {
base.playSound();
SImissile m = new SImissile(ship.getX()+(ship.getWidth()/2), ship.getY()-(ship.getHeight()/4));
missileBase.add(m);
cast.add(m);
} else if (!isBase && missileInvader.size()<3) {
base.playSound();
SImissile m = new SImissile(ship.getX()+(ship.getWidth()/2), ship.getY()+(ship.getHeight()/4));
missileInvader.add(m);
cast.add(m);
}
}
private void newWave() {
pulseRate = 50;
int defaultY=60, defaultX=120, defaultWidth=30, defaultHeight=24;
for(int i=0; i<5; i++) {
for(int j=0; j<10; j++) {
if (i<1) invaders.add(new SItop((j*defaultWidth)+defaultX, (i*defaultHeight)+defaultY, defaultWidth, defaultHeight));
else if (i<3) invaders.add(new SImiddle((j*defaultWidth)+defaultX, (i*defaultHeight)+defaultY, defaultWidth, defaultHeight));
else if (i<5) invaders.add(new SIbottom((j*defaultWidth)+defaultX, (i*defaultHeight)+defaultY, defaultWidth, defaultHeight));
}
}
for (SIinvader s: invaders) {
cast.add(s);
}
if (!cast.contains(base)) {
cast.add(base);
}
bottomRow = getBottomRow();
}
private void tryInitMysteryShip() {
Random rand = new Random();
int x=rand.nextInt(1000);
if (x<=3) {
mysteryCount = 1;
if (rand.nextBoolean()) {
mysteryDirection = true;
}
if (mysteryDirection) {
mysteryShip = new SImystery(0, 60, 36, 18);
} else {
mysteryShip = new SImystery(480, 60, 36, 18);
}
cast.add(mysteryShip);
}
}
private void moveMysteryShip() {
int distance = 0;
if (mysteryDirection) {
mysteryShip.moveRight(5);
distance = getWidth() - mysteryShip.getX();
} else {
mysteryShip.moveLeft(5);
distance = 30+mysteryShip.getX()-mysteryShip.getWidth();
}
if (distance <= 5) {
dead.add(mysteryShip);
mysteryShip = null;
mysteryCount = 0;
}
}
private void removeDead() {
#SuppressWarnings("unchecked")
ArrayList<SIinvader> temp = (ArrayList<SIinvader>) dead.clone();
dead.clear();
for (SIinvader s : temp) {
invaders.remove(s);
cast.remove(s);
}
bottomRow = getBottomRow();
}
private void invadersFire() {
int[] p = new int[bottomRow.length];
for (int i=0; i<p.length; i++) {
for (int j=0; j<p.length; j++) {
p[j] = j;
}
Random rand = new Random();
int a=rand.nextInt(101);
if (a>=20) {
int b=rand.nextInt(p.length);
fireMissile(bottomRow[b], false);
}
}
}
private int calculateDistanceToEdge() {
int distance = 0;
SIinvader[] outliers = getOutliers();
if (waveDirection) {
distance = getWidth() - outliers[0].getX()-outliers[0].getWidth();
} else {
distance = outliers[1].getX();
}
return distance;
}
private SIinvader[] getOutliers() {
SIinvader leftMost = invaders.get(0), rightMost = invaders.get(0);
for (SIinvader s : invaders) {
if (s.getX() < leftMost.getX()) {
leftMost = s;
}
if (s.getX() > rightMost.getX()) {
rightMost = s;
}
}
return new SIinvader[] { rightMost, leftMost };
}
private SIinvader[] getBottomRow() {
SIinvader[] x = new SIinvader[(invaders.size()>10)?10:invaders.size()];
for (int i=0; i<x.length; i++) {
x[i] = invaders.get(i);
for (SIinvader s:invaders) {
if (s.getX() == x[i].getX()) {
if (s.getY() > x[i].getY()) {
x[i] = s;
}
}
}
}
return x;
}
private void move(boolean b) {
int defaultX = 5;
if (b) base.moveLeft(defaultX);
else base.moveRight(defaultX);
}
private void moveAI() {
for(SIinvader s : invaders) {
s.changeImage();
int defaultX = 5;
if (waveDirection) s.moveRight(defaultX);
else s.moveLeft(defaultX);
}
}
private void moveMissileBase() {
if (invaders.isEmpty()) return;
int movement = -5, bound = 0;
SImissile missile = missileBase.get(0);
missile.moveDown(movement);
SIinvader lowestInvader = getLowestInvader();
if (missile.getY() < (lowestInvader.getY() + lowestInvader.getHeight())) {
for (SIinvader s:bottomRow) {
if (checkCollision(missile, s)) {
s.setHit();
dead.add(s);
cast.remove(missile);
missileBase.clear();
score += s.value;
return;
}
}
if (mysteryCount > 0) {
if (checkCollision(missile, mysteryShip)) {
mysteryShip.setHit();
dead.add(mysteryShip);
cast.remove(missile);
missileBase.clear();
score += mysteryShip.value;
return;
}
}
if (missile.getY() < bound) {
missileBase.remove(missile);
cast.remove(missile);
}
}
}
private SIinvader getLowestInvader() {
SIinvader lowest = bottomRow[0];
for (SIinvader invader : bottomRow) {
if (invader.getY() > lowest.getY()) {
lowest = invader;
}
}
return lowest;
}
private void moveMissileInvader() {
int movement = 5, bound = (int) panelDimension.getHeight();
for (SImissile missile : missileInvader) {
missile.moveDown(movement);
if(missile.getY() >= base.getY()) {
if (checkCollision(missile, base)) {
base.setHit();
gameOver = true;;
missileInvader.remove(missile);
cast.remove(missile);
return;
} else if (missile.getY() >= bound-25) {
missileInvader.remove(missile);
cast.remove(missile);
return;
}
}
}
}
private boolean checkCollision(SIthing missile, SIthing ship) {
Rectangle2D rect1 = new Rectangle2D.Double(
missile.getX(),
missile.getY(),
missile.getWidth(),
missile.getHeight()
);
Rectangle2D rect2 = new Rectangle2D.Double(
ship.getX(),
ship.getY(),
ship.getWidth(),
ship.getHeight()
);
return rect1.intersects(rect2);
}
private void switchBack() {
int defaultY = 12;
for (SIinvader s : invaders) {
if (s.getY() > getHeight()) {
gameOver = true;
return;
}
s.moveDown(defaultY);
}
}
private void gameOver() {
pause(true);
SI.setGameOverLabelVisibile(true);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.GREEN);
Font font = new Font("Arial", 0, 20);
setFont(font);
String score = "Score: "+this.score;
Rectangle2D rect = font.getStringBounds(score, g2.getFontRenderContext());
int screenWidth = 0;
try { screenWidth = (int) panelDimension.getWidth(); }
catch (NullPointerException e) {}
g2.setColor(Color.GREEN);
g2.drawString(score, (int) (screenWidth - (10 + rect.getWidth())), 20);
for(SIthing a:cast) {
a.paint(g);
}
}
public SIpanel() {
super();
setBackground(Color.BLACK);
cast = new ArrayList<SIthing>();
missileBase = new ArrayList<SImissile>();
score = invaderPace = mysteryCount = pulseRate = 0;
sound = new Music("AmbientMusic.wav");
panel = this;
addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT : left = true; break;
case KeyEvent.VK_RIGHT : right = true; break;
case KeyEvent.VK_SPACE : space = true; break;
}
}
#Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT : left = false; break;
case KeyEvent.VK_RIGHT : right = false; break;
case KeyEvent.VK_SPACE : space = false; break;
}
}
});
setFocusable(true);
timer = new Timer(10, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
pulse();
}
});
}
public void reset() {
SI.setGameOverLabelVisibile(false);
score = invaderPace = mysteryCount = 0;
pulseRate = 50;
cast = new ArrayList<SIthing>();
invaders = new ArrayList<SIinvader>();
dead = new ArrayList<SIinvader>();
missileBase = new ArrayList<SImissile>();
missileInvader = new ArrayList<SImissile>();
base = new SIbase(230, 370, 26, 20);
waveDirection = true;
gameOver = false;
sound.stop();
sound.loop();
panelDimension = SI.getFrameDimensions();
bottomRow = getBottomRow();
newWave();
timer.start();
runningTimer=true;
}
public SIpanel getPanel() {
return this.panel;
}
public void pause(boolean paused) {
if (paused) timer.stop();
else timer.start();
}
}
I believe that collision detection may be the reason for lagging and you should simply investigate it by trying to increase and decrease count of enemies or missiles drastically to see if that makes a difference.
Consider garbage collector your enemy. In your checkCollision method you are instantiating two (very simple) objects. It may not seem like a lot, but consider that your might be creating them for each collision check, and that at 60fps it adds up until it may reach critical mass when GC says "stop the world" and you see noticeable lag.
If that is the case, possible solution to that would be to not instantiate any objects in a method called so frequently. You may create Rectangle2D once, and then update its position, instead of creating a new one each time, so you will avoid unnecessary memory allocation.

My Five Philosophers are all eating at the same time, why?

This is the class philosopher
public class Filosofo implements Runnable{
public String nome = null;
public static Bacchetta[] bacchette = new Bacchetta[5]; //this should be the resource
public Bacchetta bacchettaDX; //right resource
public Bacchetta bacchettaSX; //left resource
public static int indice = 0;
public int x[] = {};
public int y[] = {};
public JButton filosofo = new JButton(); //Button associated to the philos.
public Filosofo(String nome, int SX, int DX){
indice++;
for(int i = 0; i<5; i++){
bacchette[i] = new Bacchetta();
}
this.nome = nome;
this.bacchettaSX = bacchette[SX];
this.bacchettaDX = bacchette[DX];
}
#Override
public synchronized void run() {
Random r = new Random();
int random;
while(true){
random = (int) r.nextInt(100);
pensa(5000);
random = (int) r.nextInt(100);
mangia(5000);
}
}
//the method mangia means the phil. is eating, so has both chopsticks
public synchronized void mangia(int tempo){
do{
if(!bacchettaSX.isOccupied){
bacchettaSX.isOccupied = true;
bacchettaSX.setChiOccupa(this.nome);
}
if(!bacchettaDX.isOccupied){
bacchettaDX.isOccupied = true;
bacchettaDX.setChiOccupa(this.nome);
}
}while(bacchettaSX.getChiOccupa().compareTo(this.nome) != 0 && bacchettaDX.getChiOccupa().compareTo(this.nome) != 0);
this.filosofo.setBackground(Color.GREEN);
try {
sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Filosofo.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("\t\t\t" + this.nome + " sta mangiando");
int a = 0;
/*for(long i = 0; i<1000000000; i++){
a++;
}*/
bacchettaSX.isOccupied = false;
bacchettaDX.isOccupied = false;
bacchettaSX.setChiOccupa(null);
bacchettaDX.setChiOccupa(null);
System.out.println("\t\t\t\t\t\t" + this.nome + " ha finito di mangiare");
this.filosofo.setBackground(Color.BLUE);
}
//the method pensa means the philosopher is no longer eating
public void pensa(int tempo){
System.out.println(this.nome + " sta ponderando");
try {
sleep(tempo);
} catch (InterruptedException ex) {
Logger.getLogger(Filosofo.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
It's supposed to print out in the terminal what's doing what, the problem is that they should eat one by one or in the best scenario maximum two philosophers. However, they eat all together. The synchronization is not doing what it's supposed to be doing. Where is the problem?
Your code block
for(int i = 0; i<5; i++){
bacchette[i] = new Bacchetta();
}
Initializes the static array each time you create a new philosopher, so they all end up with different bachettaSX and bachettaDX.
Initialize it once in a static code block outside of the constructor.
static {
for(int i = 0; i<5; i++){
bacchette[i] = new Bacchetta();
}
}

Java: read values continuously to multiple jprogressbars that never completes

I have 3 "JProgressBars" in a Java GUI application, which I need to keep updating the values at constant time intervals.
This is my application and highlighted are the progress meters which I want to read the values into.
to read the values for each progress meter I have 3 methods in my object (here a robot class)
example:
robot.readBattery();
robot.readSonic();
robot.readLight();
All methods will return a value between 0 and 100.
I have seen for a single progress bar this can be done using a swingworker.
so does that mean I need 3 swing worker classes in my program to serve all three progress bars?
even if I did that how does the property change listener distinguish among progress bars?
I'll be thankful if somebody could guide me through this.
I have a connect method in robot which returns true if the program is connected to the robot.
ie: boolean connected = robot.connect();
so inside a swingworker the code should be something like ,
while (true) {
if (connected){
setProgress(robot.readBattery());
}
sleep(1000);
}
I may be wrong. please guide me.
SSCCE as requested:
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class Tester extends JFrame implements PropertyChangeListener,
ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JProgressBar batteryMeter, lightMeter, sonicMeter;
private Robot robot = new Robot();
private BatteryTask bt;
private JButton start = new JButton("Start");
public Tester() {
JPanel statusPanel = new JPanel();
statusPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(2, 4, 2, 4);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
statusPanel.add(new JLabel("Battery Level:"), c);
batteryMeter = new JProgressBar(0, 100);
batteryMeter.setStringPainted(false);
batteryMeter.setPreferredSize(new Dimension(215, 15));
batteryMeter.setValue(0);
c.gridx = 1;
c.gridy = 0;
statusPanel.add(batteryMeter, c);
c.gridx = 0;
c.gridy = 1;
statusPanel.add(new JLabel("Light Sensor:"), c);
lightMeter = new JProgressBar(0, 100);
lightMeter.setStringPainted(false);
lightMeter.setPreferredSize(new Dimension(215, 15));
lightMeter.setValue(0);
c.gridx = 1;
c.gridy = 1;
statusPanel.add(lightMeter, c);
c.gridx = 0;
c.gridy = 2;
statusPanel.add(new JLabel("Ultrasonic Sensor:"), c);
sonicMeter = new JProgressBar(0, 100);
sonicMeter.setStringPainted(false);
sonicMeter.setPreferredSize(new Dimension(215, 15));
sonicMeter.setValue(0);
c.gridx = 1;
c.gridy = 2;
statusPanel.add(sonicMeter, c);
c.gridx = 0;
c.gridy = 3;
start.addActionListener(this);
statusPanel.add(start, c);
this.getContentPane().add(statusPanel);
this.setSize(500, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Tester();
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == start) {
bt = new BatteryTask();
bt.addPropertyChangeListener(this);
bt.execute();
}
}
#Override
public void propertyChange(PropertyChangeEvent evt) {
// TODO Auto-generated method stub
if ("progress" == evt.getPropertyName()) {
int progress = (Integer) evt.getNewValue();
batteryMeter.setValue(progress);
}
}
class BatteryTask extends SwingWorker<Void, Void> {
/*
* Main task. Executed in background thread.
*/
#Override
public Void doInBackground() {
// Initialize progress property.
setProgress(0);
while (true) {
// Sleep for up to one second.
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {
}
// Make random progress.
setProgress(robot.readBattery());
}
}
}
}
class Robot {
private Random r = new Random();
int readBattery() {
int i = r.nextInt(100 - 1) + 1;
System.out.println(i);
return i;
}
int readSonic() {
return r.nextInt(100 - 1) + 1;
}
int readLight() {
return r.nextInt(100 - 1) + 1;
}
}
The PropertyChangeEvent named "progress" communicates only a single value, and the worker's progress as a whole is irrelevant. Instead, let a single RobotTask background thread publish() a StatusRecord holding the three new values. The worker's process() method, which executes on the event dispatch thread, can update the three progress bars as new records arrive. There are related examples here and here.
class RobotTask extends SwingWorker<StatusRecord, StatusRecord> {…}
I have seen for a single progress bar this can be done using a
swingworker. so does that mean I need 3 swing worker classes in my
program to serve all three progress bars?
not neccessary, its about code design
even if I did that how does the property change listener distinguish
among progress bars?
no idea without posting your SSCCE
Thread.sleep(int) isn't about good practicies in Java, but inside doInBackground() haven't any negatice whatever, only freeze loop for sleep period
four different methods for How to move with progress
.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.beans.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import javax.swing.*;
import javax.swing.table.*;
public class TableIcon extends JFrame implements Runnable {
private static final long serialVersionUID = 1L;
private JTable table;
private JLabel myLabel = new JLabel("waiting");
private JLabel lastRunLabel = new JLabel("waiting");
private int pHeight = 40;
private boolean runProcess = true;
private int count = 0;
private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
private ScheduledExecutorService scheduler;
private AccurateScheduledRunnable periodic;
private ScheduledFuture<?> periodicMonitor;
private Executor executor = Executors.newCachedThreadPool();
private Date dateLast;
private Date dateNext;
private Date dateRun;
private int taskPeriod = 1;
private int dayCount = 0;
private int hourCount = 0;
private int minuteCount = 0;
private int secondCount = 0;
private Timer timerRun;
private int delay = 3000;
private boolean bolo = false;
public TableIcon() {
ImageIcon errorIcon = (ImageIcon) UIManager.getIcon("OptionPane.errorIcon");
ImageIcon infoIcon = (ImageIcon) UIManager.getIcon("OptionPane.informationIcon");
ImageIcon warnIcon = (ImageIcon) UIManager.getIcon("OptionPane.warningIcon");
String[] columnNames = {"Picture", "Description"};
Object[][] data = {{errorIcon, "About"}, {infoIcon, "Add"}, {warnIcon, "Copy"},};
DefaultTableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
#Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
table = new JTable(model);
table.setRowHeight(pHeight);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);
lastRunLabel.setPreferredSize(new Dimension(200, pHeight));
lastRunLabel.setHorizontalAlignment(SwingConstants.CENTER);
add(lastRunLabel, BorderLayout.NORTH);
myLabel.setPreferredSize(new Dimension(200, pHeight));
myLabel.setHorizontalAlignment(SwingConstants.CENTER);
add(myLabel, BorderLayout.SOUTH);
scheduler = Executors.newSingleThreadScheduledExecutor();
periodic = new AccurateScheduledRunnable() {
private final int ALLOWED_TARDINESS = 200;
private int countRun = 0;
private int countCalled = 0;
#Override
public void run() {
countCalled++;
if (this.getExecutionTime() < ALLOWED_TARDINESS) {
countRun++;
executor.execute(new TableIcon.MyTask("GetCurrTime")); // non on EDT
}
}
};
periodicMonitor = scheduler.scheduleAtFixedRate(periodic, 0, taskPeriod, TimeUnit.MINUTES);
periodic.setThreadMonitor(periodicMonitor);
new Thread(this).start();
prepareStartShedule();
}
private void prepareStartShedule() {
timerRun = new javax.swing.Timer(delay, startCycle());
timerRun.setRepeats(true);
timerRun.start();
}
private Action startCycle() {
return new AbstractAction("Start Shedule") {
private static final long serialVersionUID = 1L;
#Override
public void actionPerformed(ActionEvent e) {
executor.execute(new TableIcon.MyTask("StartShedule")); // non on EDT
}
};
}
private void changeTableValues() {
Runnable doRun = new Runnable() {
#Override
public void run() {
if (bolo) {
bolo = false;
table.getModel().setValueAt("*/*/*/**/*/*/*", 0, 1);
table.getModel().setValueAt(" k k k k k k k k", 1, 1);
table.getModel().setValueAt("#######", 2, 1);
} else {
bolo = true;
table.getModel().setValueAt("Green Peper", 0, 1);
table.getModel().setValueAt("Yellow Apple", 1, 1);
table.getModel().setValueAt("Orange Bus", 2, 1);
}
}
};
SwingUtilities.invokeLater(doRun);
}
private void distAppInfo() {
Runnable doRun = new Runnable() {
#Override
public void run() {
dateNext = new java.util.Date();
dateLast = new java.util.Date();
long tme = dateNext.getTime();
tme += (taskPeriod * 60) * 1000;
dateNext.setTime(tme);
lastRunLabel.setText("Last : " + sdf.format(dateLast) + " / Next : " + sdf.format(dateNext));
}
};
SwingUtilities.invokeLater(doRun);
}
private void changeLabelColor() {
Runnable doRun = new Runnable() {
#Override
public void run() {
Color clr = lastRunLabel.getForeground();
if (clr == Color.red) {
lastRunLabel.setForeground(Color.blue);
} else {
lastRunLabel.setForeground(Color.red);
}
}
};
SwingUtilities.invokeLater(doRun);
}
#Override
public void run() {
while (runProcess) {
try {
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
executor.execute(new TableIcon.MyTask("ChangeIconLabel")); // non on EDT
}
}
private void setIconLabel() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
String text = "";
dateRun = new java.util.Date();
long tme = dateRun.getTime();
long she = periodicMonitor.getDelay(TimeUnit.SECONDS);
dayCount = (int) (she / (24 * 60 * 60));
hourCount = (int) (she / (60 * 60));
minuteCount = (int) (she / (60));
secondCount = (int) she;
int hourss = hourCount;
int minutess = minuteCount;
if (dayCount > 0) {
hourCount -= (dayCount * 24);
minuteCount -= ((dayCount * 24 * 60) + (hourCount * 60));
secondCount -= (minutess * 60);
//System.out.println(" Days : " + dayCount + " ,Hours : " + hourCount + " , Minutes : " + minuteCount + " , Seconds : " + secondCount);
text = (" " + dayCount + " Days " + hourCount + " h : " + minuteCount + " m : " + secondCount + " s");
} else if (hourCount > 0) {
minuteCount -= ((hourss * 60));
secondCount -= (minutess * 60);
//System.out.println(" Hours : " + hourCount + " , Minutes : " + minuteCount + " , Seconds : " + secondCount);
text = (" " + hourCount + " h : " + minuteCount + " m : " + secondCount + " s");
} else if (minuteCount > 0) {
secondCount -= (minutess * 60);
//System.out.println(" Minutes : " + minuteCount + " , Seconds : " + secondCount);
text = (" " + minuteCount + " m : " + secondCount + " s");
} else {
//System.out.println(" Seconds : " + secondCount);
text = (" " + secondCount + " s");
}
tme += she * 1000;
ImageIcon myIcon = (ImageIcon) table.getModel().getValueAt(count, 0);
String lbl = "Row at : " + count + " Remains : " + text;
myLabel.setIcon(myIcon);
myLabel.setText(lbl);
count++;
if (count > 2) {
count = 0;
}
}
});
}
public static void main(String[] args) {
TableIcon frame = new TableIcon();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLocation(150, 150);
frame.pack();
frame.setVisible(true);
}
private class MyTask extends SwingWorker<Void, Integer> {
private String str;
private String namePr;
MyTask(String str) {
this.str = str;
addPropertyChangeListener(new SwingWorkerCompletionWaiter(str, namePr));
}
#Override
protected Void doInBackground() throws Exception {
if (str.equals("GetCurrTime")) {
distAppInfo();
} else if (str.equals("ChangeIconLabel")) {
setIconLabel();
} else if (str.equals("StartShedule")) {
changeTableValues();
}
return null;
}
#Override
protected void process(List<Integer> progress) {
//System.out.println(str + " " + progress.get(progress.size() - 1));
}
#Override
protected void done() {
if (str.equals("GetCurrTime")) {
changeLabelColor();
} else if (str.equals("ChangeIconLabel")) {
//setIconLabel();
} else if (str.equals("StartShedule")) {
//changeTableValues();
}
}
}
private class SwingWorkerCompletionWaiter implements PropertyChangeListener {
private String str;
private String namePr;
SwingWorkerCompletionWaiter(String str, String namePr) {
this.str = str;
this.namePr = namePr;
}
SwingWorkerCompletionWaiter(String namePr) {
this.namePr = namePr;
}
#Override
public void propertyChange(PropertyChangeEvent event) {
if ("state".equals(event.getPropertyName()) && SwingWorker.StateValue.DONE == event.getNewValue()) {
System.out.println("Thread Status with Name :" + str + ", SwingWorker Status is " + event.getNewValue());
} else if ("state".equals(event.getPropertyName()) && SwingWorker.StateValue.PENDING == event.getNewValue()) {
System.out.println("Thread Status with Mame :" + str + ", SwingWorker Status is " + event.getNewValue());
} else if ("state".equals(event.getPropertyName()) && SwingWorker.StateValue.STARTED == event.getNewValue()) {
System.out.println("Thread Status with Name :" + str + ", SwingWorker Status is " + event.getNewValue());
} else {
System.out.println("SomeThing Wrong happends with Thread Status with Name :" + str);
}
}
}
}
abstract class AccurateScheduledRunnable implements Runnable {
private ScheduledFuture<?> thisThreadsMonitor;
public void setThreadMonitor(ScheduledFuture<?> monitor) {
this.thisThreadsMonitor = monitor;
}
protected long getExecutionTime() {
long delay = -1 * thisThreadsMonitor.getDelay(TimeUnit.MILLISECONDS);
return delay;
}
}
Based on answer from #trashgod, I modified my code. which is working as expected.
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class Tester extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JProgressBar batteryMeter, lightMeter, sonicMeter;
private Robot robot = new Robot();
private ReadSensorTask readSensor;
private JButton start = new JButton("Start");
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Tester();
}
});
}
public Tester() {
JPanel statusPanel = new JPanel();
statusPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(2, 4, 2, 4);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
statusPanel.add(new JLabel("Battery Level:"), c);
batteryMeter = new JProgressBar(0, 100);
batteryMeter.setStringPainted(false);
batteryMeter.setPreferredSize(new Dimension(215, 15));
batteryMeter.setValue(0);
c.gridx = 1;
c.gridy = 0;
statusPanel.add(batteryMeter, c);
c.gridx = 0;
c.gridy = 1;
statusPanel.add(new JLabel("Light Sensor:"), c);
lightMeter = new JProgressBar(0, 100);
lightMeter.setStringPainted(false);
lightMeter.setPreferredSize(new Dimension(215, 15));
lightMeter.setValue(0);
c.gridx = 1;
c.gridy = 1;
statusPanel.add(lightMeter, c);
c.gridx = 0;
c.gridy = 2;
statusPanel.add(new JLabel("Ultrasonic Sensor:"), c);
sonicMeter = new JProgressBar(0, 100);
sonicMeter.setStringPainted(false);
sonicMeter.setPreferredSize(new Dimension(215, 15));
sonicMeter.setValue(0);
c.gridx = 1;
c.gridy = 2;
statusPanel.add(sonicMeter, c);
c.gridx = 0;
c.gridy = 3;
start.addActionListener(this);
statusPanel.add(start, c);
this.getContentPane().add(statusPanel);
this.setSize(500, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == start) {
readSensor = new ReadSensorTask();
readSensor.execute();
}
}
private class RobotValues {
private int sonic, light, battery;
RobotValues(int b, int l, int s) {
this.light = l;
this.battery = b;
this.sonic = s;
}
}
class ReadSensorTask extends SwingWorker<Void, RobotValues> {
/*
* Main task. Executed in background thread.
*/
#Override
public Void doInBackground() {
while (true) {
publish(new RobotValues(robot.readBattery(), robot.readLight(),
robot.readSonic()));
// Sleep for up to one second.
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {
}
}
}
#Override
protected void process(List<RobotValues> rbv) {
RobotValues rb = rbv.get(rbv.size() - 1);
batteryMeter.setValue(rb.battery);
sonicMeter.setValue(rb.sonic);
lightMeter.setValue(rb.light);
}
}
}
class Robot {
private Random r = new Random();
int readBattery() {
int i = r.nextInt(100 - 1) + 1;
return i;
}
int readSonic() {
return r.nextInt(100 - 1) + 1;
}
int readLight() {
return r.nextInt(100 - 1) + 1;
}
}

Applet does not load on html page

I have this applet and i cant figure out why it doesnt load on html page.I have added full permissions in java.policy file. I use the default html file from NetBeans Applet's output.
/* Hearts Cards Game with AI*/
import java.applet.Applet;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.awt.Graphics;
import java.awt.Image;
import java.security.AccessController;
import javax.swing.ImageIcon;
import javax.swing.*;
import javax.swing.JPanel;
public class Game extends JApplet implements MouseListener, Runnable {
int initNoCards = 13;
int width, height;
boolean endGame = false;
int turn = -1;
int firstCard = 0;
int firstTrick = 0;
String leadingSuit = null;
Cards leadingCard = null;
Cards playCard = null;
String startCard = "c2";
Cards[] trickCards = new Cards[4];
ArrayList<Cards>[] playerCards = new ArrayList[4];
ArrayList<Cards>[] takenCards = new ArrayList[4];
boolean heartsBroken = false;
ArrayList<Cards> cards = new ArrayList<Cards>();
String[] hearts = {"h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "h12", "h13", "h14", "h15"};
String queen = "s13";
int cardHeight = 76;
int cardWidth = 48;
ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>();
int selectedCard = -1;
//set the background image
Image backImage = new ImageIcon("deck\\back2.png").getImage();
public void GetDataFromXML() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean name = false;
boolean image = false;
#Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("NAME")) {
name = true;
}
if (qName.equalsIgnoreCase("IMAGE")) {
image = true;
}
}
#Override
public void endElement(String uri, String localName,
String qName) throws SAXException {
}
#Override
public void characters(char ch[], int start, int length) throws SAXException {
String s = new String(ch, start, length);
if (name) {
cards.add(new Cards(s));
name = false;
}
if (image) {
image = false;
}
}
};
saxParser.parse("deck\\deck.xml", handler);
} catch (Exception e) {
}
}
//function for comparing cards from same suite
public boolean lowerThan(Cards c1, Cards c2) {
int a, b;
a = Integer.parseInt(c1.getName().substring(1));
b = Integer.parseInt(c2.getName().substring(1));
return a < b;
}
//checks if a card is valid to play
public boolean ValidMove(Cards c) {
if (firstCard == 0) {
if (c.getName().equals(startCard)) {
firstCard = 1;
return true;
}
return false;
}
boolean result = playerCards[turn].indexOf(c) >= 0;
if (leadingSuit == null) {
return result;
}
boolean found = false;
for (int i = 0; i < playerCards[turn].size(); i++) {
if (playerCards[turn].get(i).getName().charAt(0) == leadingSuit.charAt(0)) {
found = true;
break;
}
}
if (!found) {
boolean justHearts = true;
for (int i = 0; i < playerCards[turn].size(); i++) {
if (playerCards[turn].get(i).getName().charAt(0) != 'h') {
justHearts = false;
break;
}
}
if (firstTrick == 0) {
if (c.getName().equals(queen)) {
return false;
}
if (!justHearts && c.getName().charAt(0) == 'h') {
return false;
}
} else {
if (c.getName().charAt(0) == 'h' && leadingSuit == null && !heartsBroken && !justHearts) {
return false;
}
}
} else {
if (c.getName().charAt(0) != leadingSuit.charAt(0)) {
return false;
}
}
return result;
}
#Override
public void init() {
GetDataFromXML();
setSize(500, 500);
width = super.getSize().width;
height = super.getSize().height;
setBackground(Color.white);
addMouseListener(this);
for (int i = 0; i < cards.size(); i++) {
System.out.println(cards.get(i).getName());
System.out.println(cards.get(i).getImage());
}
Shuffle();
}
public int GetTrickCount() {
int count = 0;
for (int i = 0; i < trickCards.length; i++) {
if (trickCards[i] != null) {
count++;
}
}
return count;
}
public void ResetTrick() {
for (int i = 0; i < trickCards.length; i++) {
trickCards[i] = null;
}
}
#Override
public void run() {
try {
PlayTurn();
} catch (InterruptedException ex) {
}
}
public void start() {
Thread th = new Thread(this);
th.start();
}
//function for shuffling cards and painting players cards
public void Shuffle() {
for (int i = 0; i < 4; i++) {
playerCards[i] = new ArrayList<Cards>();
takenCards[i] = new ArrayList<Cards>();
}
ArrayList<Cards> list = new ArrayList<Cards>();
list.addAll(cards);
Collections.shuffle(list);
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i).getName() + " ");
}
//initializare liste carti
for (int i = 0; i < 4; i++) {
playerCards[i] = new ArrayList<Cards>();
takenCards[i] = new ArrayList<Cards>();
for (int j = 0; j < initNoCards; j++) {
playerCards[i].add((list.get(j + i * initNoCards)));
if (list.get(j + i * initNoCards).getName().equals(startCard)) {
turn = i;
}
}
Collections.sort(playerCards[i], c);
ShowCards(i);
}
for (int i = 0; i < playerCards[0].size() - 1; i++) {
rectangles.add(new Rectangle((141 + 1) + 13 * i - 2, 350 + 1, 13 - 2, cardHeight - 1));
}
rectangles.add(new Rectangle((141 + 1) + 13 * 12 - 2, 350 + 1, cardWidth, cardHeight - 1));
ShowPlayersCards();
}
Comparator<Cards> c = new Comparator<Cards>() {
#Override
public int compare(Cards o1, Cards o2) {
if (o2.getName().charAt(0) != o1.getName().charAt(0)) {
return o2.getName().charAt(0) - o1.getName().charAt(0);
} else {
int a, b;
a = Integer.parseInt(o1.getName().substring(1));
b = Integer.parseInt(o2.getName().substring(1));
return a - b;
}
}
};
public void PlayTurn() throws InterruptedException {
endGame = true;
System.out.println("Its " + turn);
for (int i = 0; i < 4; i++) {
if (!playerCards[i].isEmpty()) {
endGame = false;
}
}
if (endGame) {
System.out.println("Game over!");
GetPlayersScore();
return;
}
if (turn != 0) {
Random r = new Random();
int k = r.nextInt(playerCards[turn].size());
Cards AIcard = playerCards[turn].get(k);
while (!ValidMove(AIcard)) {
k = r.nextInt(playerCards[turn].size());
AIcard = playerCards[turn].get(k);
}
leadingCard = AIcard;
playCard = AIcard;
} else {
System.out.println("\nIt is player's (" + turn + ") turn");
System.out.println("Player (" + turn + ") enter card to play:");
leadingCard = null;
playCard = null;//new Cards(read);
while (true) {
if (playCard != null) {
break;
}
Thread.sleep(50);
}
}
repaint();
Thread.sleep(1000);
repaint();
if (playCard.getName().charAt(0) == 'h') {
heartsBroken = true;
}
playerCards[turn].remove(playCard);
trickCards[turn] = playCard;
if (GetTrickCount() == 1)//setez leading suit doar pentru trickCards[0]
{
leadingSuit = GetSuit(playCard);
}
System.out.println("Leading suit " + leadingSuit);
System.out.println("Player (" + turn + ") chose card " + playCard.getName() + " to play");
ShowTrickCards();
ShowPlayersCards();
if (GetTrickCount() < 4) {
turn = (turn + 1) % 4;
} else {
turn = GetTrickWinner();
leadingSuit = null;
firstTrick = 1;
playCard = null;
repaint();
}
PlayTurn();
}
public void ShowTrickCards() {
System.out.println("Cards in this trick are:");
for (int i = 0; i < 4; i++) {
if (trickCards[i] != null) {
System.out.print(trickCards[i].getName() + " ");
}
}
}
public String GetSuit(Cards c) {
if (c.getName().contains("c")) {
return "c";
}
if (c.getName().contains("s")) {
return "s";
}
if (c.getName().contains("h")) {
return "h";
}
if (c.getName().contains("d")) {
return "d";
}
return null;
}
public String GetValue(Cards c) {
String get = null;
get = c.getName().substring(1);
return get;
}
public int GetTrickWinner() {
int poz = 0;
for (int i = 1; i < 4; i++) {
if (trickCards[poz].getName().charAt(0) == trickCards[i].getName().charAt(0) && lowerThan(trickCards[poz], trickCards[i]) == true) {
poz = i;
}
}
System.out.println("\nPlayer (" + poz + ") won last trick with card " + trickCards[poz].getName());
ResetTrick();
return poz;
}
public void ShowPlayersCards() {
ShowCards(0);
ShowCards(1);
ShowCards(2);
ShowCards(3);
}
public void GetPlayersScore() {
GetScore(0);
GetScore(1);
GetScore(2);
GetScore(3);
}
public void ShowCards(int player) {
System.out.print("\nPlayer (" + player + ") cards: ");
for (int i = 0; i < playerCards[player].size(); i++) {
System.out.print(playerCards[player].get(i).getName() + " ");
}
System.out.println();
}
public int GetScore(int player) {
int score = 0;
for (int i = 0; i < takenCards[player].size(); i++) {
for (int j = 0; j < hearts.length; j++) {
if (takenCards[player].get(i).getName().equals(hearts[j])) {
score++;
break;
}
}
if (takenCards[player].get(i).getName().equals(queen)) {
score += 13;
}
}
return score;
}
#Override
public void paint(Graphics g) {
g.drawImage(backImage, 0, 0, getWidth(), getHeight(), this);
for (int i = 0; i < playerCards[0].size(); i++) {
if (selectedCard == i) {
g.drawImage(playerCards[0].get(i).getImage(), 141 + i * 13, 340, null);
} else {
g.drawImage(playerCards[0].get(i).getImage(), 141 + i * 13, 350, null);
}
if (trickCards[0] != null) {
g.drawImage(trickCards[0].getImage(), 225, 250, 48, 76, null);
}
if (trickCards[1] != null) {
g.drawImage(trickCards[1].getImage(), 177, 174, 48, 76, null);
}
if (trickCards[2] != null) {
g.drawImage(trickCards[2].getImage(), 225, 98, 48, 76, null);
}
if (trickCards[3] != null) {
g.drawImage(trickCards[3].getImage(), 273, 174, 48, 76, null);
}
}
}
#Override
public void mouseClicked(MouseEvent e) {
if (turn != 0) {
return;
}
for (int i = 0; i < rectangles.size(); i++) {
if (rectangles.get(i).contains(e.getPoint())) {
if (i == selectedCard) {
if (ValidMove(playerCards[0].get(i))) {
selectedCard = -1;
rectangles.get(rectangles.size() - 2).width = rectangles.get(rectangles.size() - 1).width;
playCard = playerCards[0].get(i);
leadingCard = playCard;
rectangles.remove(rectangles.size() - 1);
trickCards[0] = playerCards[0].remove(i);
} else {
if (firstCard == 0) {
JOptionPane.showMessageDialog(this, "You have to play 2 of clubs!");
}
}
} else {
selectedCard = i;
rectangles.get(i).y -= 10;
}
repaint();
break;
}
}
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
}
class Cards extends JPanel {
private String name;
private String image;
private Image img;
public Cards(String name) {
super();
this.name = name;
this.image = "deck\\" + name + ".png";
this.img = new ImageIcon(image).getImage();
}
public Cards() {
super();
this.name = null;
this.image = null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Image getImage() {
return img;
}
public void setImage(String image) {
this.image = image;
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Cards)) {
return false;
}
Cards c = (Cards) obj;
return name.equals(c.getName()) && image.equals(c.getImage());
}
#Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + (this.name != null ? this.name.hashCode() : 0);
hash = 31 * hash + (this.image != null ? this.image.hashCode() : 0);
return hash;
}
#Override
public void paint(Graphics g) {
g.drawImage(img, WIDTH, HEIGHT, this);
}
public boolean lowerThan(Cards c1, Cards c2) {
int a, b;
a = Integer.parseInt(c1.getName().substring(1));
b = Integer.parseInt(c2.getName().substring(1));
return a < b;
}
public int compareTo(Cards c) {
if (c.getName().charAt(0) != name.charAt(0)) {
return c.getName().charAt(0) - name.charAt(0);
} else {
int a, b;
a = Integer.parseInt(name.substring(1));
b = Integer.parseInt(c.getName().substring(1));
return a - b;
}
}
}
HTML
<HTML>
<HEAD>
<TITLE>Applet HTML Page</TITLE>
</HEAD>
<BODY>
<H3><HR WIDTH="100%">Applet HTML Page<HR WIDTH="100%"></H3>
<P>
<APPLET codebase="classes" code="Game.class" width=350 height=200></APPLET>
</P>
<HR WIDTH="100%"><FONT SIZE=-1><I>Generated by NetBeans IDE</I></FONT>
</BODY>
</HTML>
Image backImage = new ImageIcon("deck\\back2.png").getImage();
If I am the user of the applet when it is on the internet, the will cause the JRE to search for a File relative to the current user directory on my PC, either that or the cache of FF. In either case, it will not locate an image by the name of back2.png.
For fear of sounding like a looping Clip:
Resources intended for an applet (icons, BG image, help files etc.) should be accessed by URL.
An applet will not need trust to access those resources, so long as the resources are on the run-time class-path, or on the same server as the code base or document base.
Further
I have added full permissions in java.policy file.
This is pointless. It will not be workable at time of deployment unless you control every machine it is intended to run on. If an applet needs trust in a general environment, it needs to be digitally signed. You might as well sign it while building the app.
cant figure out why it doesnt load on html page.
Something that would assist greatly is to configure the Java Console to open when an applet is loaded. There is a setting in the last tab of the Java Control Panel that configures it.

Categories