I have tried a bunch of arrangements for my code, and this is my last version of code. I am trying to create a window, that will cycle through my 5 questions, and the end it will display how many you got correct.
Currently after entering my first answer in the text field it jumps to the end of the array. After that it will continually stay on the same question.
Sorry if I have made many coding errors, as this is my first time creating a program after watching a bunch of tutorials. Thanks for any help!
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class testwin extends JFrame {
private JTextField input;
private JLabel problem;
private String[] questions = new String[5];
private String[] answers = new String[5];
private String[] response = new String[5];
private int total = 5;
private int result = 0;
private String mark;
public testwin(){
super("Pop Quiz");
setLayout(new FlowLayout());
questions[0] = "Solve for x \t (x + 3)*2-10 = 4";
questions[1] = "Factorize \t x^2 + 10x + 21";
questions[2] = "Find the square root of 64";
questions[3] = "Multiply 23 and 94";
questions[4] = "Add 2145, 1452, 253,1414";
answers[0] = "4";
answers[1] = "(x + 3)(x + 7)";
answers[2] = "8";
answers[3] = "2162";
answers[4] = "5264";
problem = new JLabel(questions[0]);
add(problem);
input = new JTextField("Answer goes here",20);
add(input);
mark = String.format("You got %s correct out of %s", result,total);
input.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
int y = 0;
int x = 0;
if(event.getSource() == input){
while(x < questions.length){
problem.setText(questions[x]);
response[y] = String.format("%s",event.getActionCommand());
input.setText("Answer goes here");
x++;
y++;
}
}
if(x == 4)
JOptionPane.showMessageDialog(null, mark);
for(int z = 0; z < questions.length; z++){
if(response[z] == answers[z])
result++;
}
}
}
);
add(input);
setSize(250,250);
setVisible(true);
}
}
During each actionPerformed call you cycle through the whole list. Here:
while(x < questions.length){
problem.setText(questions[x]);
response[y] = String.format("%s",event.getActionCommand());
input.setText("Answer goes here");
x++;
y++;
}
So by the time the text is actually displayed again, you have set to it to each different question, but stop with the last one and that is what the user actually sees. You only want to change the text once, to the next question, everytime an action is performed.
You'll need to keep some sort of counter for which question you are on that can be accessed by the actionPerformed method
Also, as mentioned in the comments, you will want to change your result checking to use the equals method. Strings can't be compared using the == sign because for Strings == compares the reference each String object is pointing to, not the value of the String object
if(response[z].equals(answers[z]))
result++;
The issue is the while() loop inside your ActionListener, it will always iterate through the entire set of questions and finish at the last entry. This is due to the fact that your x and y variables are local to the scope of the ActionListener, and as such, are always reset to 0 and looped on each input click.
To fix this, you don't need a while loop, just make your x variable a private class field (questionIndex), use an if statement ensure the index is within the array bounds, and update the problem and response values accordingly.
Here's some psuedocode that should do the right thing:
private int questionIndex = 0;
public void actionPerformed(ActionEvent event){
if(event.getSource() == input){
if(questionIndex < questions.length){
problem.setText(questions[questionIndex]);
response[questionIndex] = String.format("%s",event.getActionCommand());
input.setText("Answer goes here");
questionIndex++;
}
...
}
}
Related
I am writing a program where users answer questions by clicking on JTable cells. If the question is answered correctly/incorrectly, the MouseListener changes the value of answeredResult. The program will keep running until there are no available questions (the available array list contains a list of Items, which is a class I created that contains strings event, year, and description, called by getEvent(), getYear(), and getDescription() methods respectively). However, for each question, I want to wait until a mouseClickedEvent is detected before executing the proceeding code (see below).
I know that adding another while loop for waiting is really bad. I've also seen
some answers suggesting to put whatever I want to do in the mouseClicked method, but I don't know how in my case, since I want to repeat it multiple times.
I think another problem is that the program is stuck in the while(available.size()!=0) loop. So it cant detect the mouse click.
Any suggestions on how to make this work?
ArrayList<Item> available = new ArrayList<Item>();
for(int i = 0; i < 5; i++){
available.add(new Item("Event " + i, "Year " + i, "Description " + i));
}
JTable table = new JTable(model);
table.addMouseListener(new java.awt.event.MouseAdapter() {
#Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
int row = table.rowAtPoint(evt.getPoint());
int col = table.columnAtPoint(evt.getPoint());
if (row >= 0 && col == 2 && canPlay) {
clickedYear = (String)data[row][col];
if(clickedYear.equals(answerYear)){
answeredResult = 1;
}else{
answeredResult = -1;
}
}
}
});
while(available.size()!=0){
promptPanel.removeAll();
answeredResult = 0; //0-unanswered 1-correct 2-incorrect
int random = (int)(Math.random()*(available.size()-1));
JLabel label = new JLabel(available.get(random).getEvent()+"\n");
promptPanel.add(label);
canPlay = true;
//wait for mouse clicked before proceeding to the next code
if(answeredResult == 1){
JLabel correct = new JLabel("Correct!");
promptPanel.add(correct);
}else if(answeredResult == -1){
JLabel wrong = new JLabel("Wrong.");
promptPanel.add(wrong);
}
revalidate();
repaint();
available.remove(random);
}
I'm making a hangman game for school and I've run into a problem that I simply can't solve, maybe I'm overthinking, maybe I'm not. Anyways, I need to let the user input a letter, and if that letter is in the word used for the game (pikachu. I know, stupid choice but it's pretty basic and easy so I used that) then the letter is revealed, the problem is that after inputting a letter, the user can't guess any more letters. I need a way to loop through the letter input and revealing so that I can actually play the game.
I'm sorry if the solution is so simple but I just can't figure out what needs to change in my code in order to fix my problem because I'm very new to java.
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.*;
import java.awt.event.KeyListener;
import javax.swing.*;
public class PanDisp extends JPanel {
JLabel lblOutput;
JLabel lblGuess;
JButton btnUpdateLabel;
Image imgPkmn;
FraImg fraImg;
String sSecret;
public PanDisp() {//Constructor
KeyInput keyInput = new KeyInput();
KeyInput.LabelChangeListener labelChange = keyInput.new LabelChangeListener();
sSecret = "*******";
lblGuess = new JLabel("Type will go here");
lblOutput = new JLabel(sSecret);
btnUpdateLabel = new JButton("Enter");
add(lblOutput);
add(btnUpdateLabel);
addKeyListener(new KeyInput());
setFocusable(true);
btnUpdateLabel.addActionListener(labelChange);
fraImg = new FraImg(imgPkmn);
}
public void GameOver() {
}
class KeyInput implements KeyListener {
String sInput;
String sWord = "pikachu";
String sSecret = "*******";
char chInput;
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
chInput = (char) e.getKeyChar();
sInput = String.valueOf(chInput);
lblOutput.setText(sInput);
}
#Override
public void keyReleased(KeyEvent e) {
}
class LabelChangeListener implements ActionListener {
char cWord;
int nCorrect, nIncorrect, nNum;
public void actionPerformed(ActionEvent event) {
if (sWord.contains(sInput)) {
for (int i = 0; i < sWord.length(); i++) {
sSecret.replace(sSecret.charAt(i), sWord.charAt(i));
}
nCorrect += 1;
}
else {
nIncorrect += 1;
if (nIncorrect == 7) {
GameOver();
}
}
}
}
}
}
Your problem is that your mindset is off and has to be changed. Don't think "loop", and in fact get "loop" out of the equation. You're programming in an event-driven programming environment, and the loop you're thinking of belongs in the linear console programming environment. Instead think "state of object" and "behavioral changes to state changes", and you'll move much further in this quest. So change the state of your class -- number of guesses, number of correct guesses, and then change the response to the user's input based on this state
For instance, if you wanted to create a console program that allowed a user to enter 5 Strings, and then displayed those Strings back to the user, it would be pretty straight forward, in that you'd create your String array, and then use a for loop to prompt the user 5 times to enter text, grabbing each entered String within the loop. Here "loops" like the one you're requesting work.
Linear Console Program
import java.util.Scanner;
public class Enter5Numbers1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter 5 sentences:");
String[] sentences = new String[5];
for (int i = 0; i < sentences.length; i++) {
System.out.printf("Enter sentence #%d: ", (i + 1));
sentences[i] = scanner.nextLine();
}
System.out.println("You entered the following sentences:");
for (String sentence : sentences) {
System.out.println(sentence);
}
scanner.close();
}
}
If on the other hand you wanted to create a GUI that did something similar, that prompted the user for 5 Strings and accepted those Strings into an array, you couldn't use the same type of for loop. Instead you would need to give your class an int String counter, perhaps called enteredSentenceCount, and in a JButton's ActionListener (or Action -- which is something very similar), you would accept an entered String (perhaps typed into a JTextField called entryField), only if the enteredSentenceCount is less than 5, less than the maximum number of Strings allowed. You would of course increment the enteredSentenceCount variable each time a String is entered. And this combination of increase a counter variable and checking its value will need to substutite for the concept of a "loop". So here the "state" of the class is held in the enteredSentenceCount, and the behavioral change we want is to alter what the button's Action does depending on the enteredSentenceCount's value -- if less than 5, accept a String, and if it is equal to or greater than 5, display the entered Strings.
Event Driven GUI Program
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class Enter5Numbers2 extends JPanel {
private static final int MAX_SENTENCE_COUNT = 5; // number of Strings to enter
private static final String PROMPT_TEMPLATE = "Please enter sentence number %d:";
private String[] sentences = new String[MAX_SENTENCE_COUNT]; // array to hold entered Strings
private int enteredSentenceCount = 0; // count of number of Strings entered
private JTextField entryField = new JTextField(20); // field to accept text input frm user.
// JLabel to display prompts to user:
private JLabel promptLabel = new JLabel(String.format(PROMPT_TEMPLATE, (enteredSentenceCount + 1)));
public Enter5Numbers2() {
// create GUI
// First create Action / ActionListener for button
EntryAction entryAction = new EntryAction("Enter");
JButton entryButton = new JButton(entryAction); // pass it into the button
entryField.setAction(entryAction); // but give it also to JTextField so that the enter key will trigger it
// JPanel to accept user data entry
JPanel entryPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
entryPanel.add(entryField);
entryPanel.add(entryButton);
// allow main JPanel to display prompt
setBorder(BorderFactory.createTitledBorder("Please Enter 5 Sentences"));
setLayout(new GridLayout(2, 1));
add(promptLabel);
add(entryPanel);
}
// Action class, similar to an ActionListener
private class EntryAction extends AbstractAction {
public EntryAction(String name) {
super(name);
putValue(MNEMONIC_KEY, (int) name.charAt(0));
}
#Override
public void actionPerformed(ActionEvent e) {
// check that we haven't entered more than the max number of sentences
if (enteredSentenceCount < MAX_SENTENCE_COUNT) {
// if OK, get the entered text
String sentence = entryField.getText();
// put it in our array
sentences[enteredSentenceCount] = sentence;
entryField.setText(""); // clear the text field
entryField.requestFocusInWindow(); // set the cursor back into the textfield
enteredSentenceCount++; // increment our entered sentence count variable
promptLabel.setText(String.format(PROMPT_TEMPLATE, (enteredSentenceCount + 1))); // change prompt
}
// if the number of sentences added equals the number we want, display it
if (enteredSentenceCount == MAX_SENTENCE_COUNT) {
JTextArea textArea = new JTextArea(6, 30);
for (String sentence : sentences) {
textArea.append(sentence + "\n");
}
JScrollPane scrollPane = new JScrollPane(textArea);
JOptionPane.showMessageDialog(Enter5Numbers2.this, scrollPane, "Five Sentences Entered",
JOptionPane.PLAIN_MESSAGE);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Enter 5 Numbers");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(new Enter5Numbers2());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
Ok, I am sorry I am repeating questions that have already been asked but I have searched and searched and searched and nobody's answers seemed to have helped me... I tried the following questions:
JButton "stay pressed" after click in Java Applet
JButton stays pressed when focus stolen by JOptionPane
(I apologize if I'm just being dumb.. it is hard to relate to my code)
I have tried everything: using another thread to handle all the stuff, changing the JFrame to a JDialog as apparently they are "modal" so it would work independently. But that didn't seem to work either. I am stuck now so I am using my last resource (asking Stack Overflow).
What I am trying to do is get the user to enter some numbers in a textfield (4,2,7) then they press a JButton "Calculate Mean" and it finds the mean of the numbers and displays it in a JOptionPane message. When the user closes the JOptionPane dialog box they should be able to edit the numbers and do it again but the "Calculate Mean" button stays pressed and the user can't do anything but close the window. Even pressing the Tab key doesn't change anything. Does anyone know why this is? My code is down below:
PLEASE FORGIVE ME IF MY CODE IS HARD TO READ! I spent a very long time trying to indent it all correctly and I also tried to make it as short as possible by taking out any bits unrelated to the question. I was unsure which bits to take out so there still might be some unnecessary bits...
I am sorry for my messy code but this is the code:
package MathsProgram_II;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
public class Mean implements Runnable {
JFrame meanFrame = new JFrame(); //I tried changing this to dialog
JPanel meanPanel = new JPanel(new GridBagLayout());
JLabel enterNums = new JLabel("Enter Numbers: ");
JTextField txtNums = new JTextField(20);
JButton calculate = new JButton("Calculate Mean");
boolean valid = true;
double answer = 0;
ButtonListener bl = new ButtonListener();
public synchronized double[] getArray() {
String nums = txtNums.getText();
String[] numsArray = nums.split(",");
double[] doubleArray = new double[numsArray.length];
if (nums.isEmpty() == true) {
JOptionPane.showMessageDialog(meanFrame, "You did not enter anything!",
"Fail", JOptionPane.ERROR_MESSAGE);
valid = false;
calculate.setEnabled(false);
} else {
for (int i = 0; i < numsArray.length; i++) {
try {
doubleArray[i] = Double.parseDouble(numsArray[i]);
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(meanFrame, "Error getting numbers!",
"Error", JOptionPane.ERROR_MESSAGE);
valid = false;
}
}
}
return doubleArray;
}
public synchronized void calculateMean() {
ArrayList<Double> numbersList = new ArrayList<Double>(20);
double[] theNumbers = getArray();
double tempAnswer = 0;
if (valid == true) {
int length = theNumbers.length;
for (int i = 0; i < theNumbers.length; i++) {
numbersList.add(theNumbers[i]);
}
for (int i = 0; i < length; i++) {
double y = numbersList.get(i);
tempAnswer = tempAnswer + y;
}
this.answer = tempAnswer / length;
//I ALSO TRIED DOING THIS:
txtNums.requestFocus();
calculate.setEnabled(false);
showMean();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
}
}
public void showMean() {
JOptionPane.showMessageDialog(meanFrame, "The Mean: " + answer, "The Mean of Your Numbers", JOptionPane.INFORMATION_MESSAGE);
}
private class ButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == calculate) {
meanFrame.remove(meanPanel);
meanFrame.setVisible(true);
calculateMean();
}
}
}
}
Don't remove the panel from your mainframe.
Don't use "synchronized" on your "calculateMean()" method. The code is executed on the Event Dispatch Thread (EDT) so it will be single threaded.
Don't use a Thread.sleep() on the EDT. This will prevent the GUI from repainting itself.
I'm new in object-oriented programming and I use Java. I'm stil finding it hard to manipulate through the classes using generics and stuff. As a practice, I looked for codes in the internet and my colleague suggested me a program that came from this site.
This is the first class:
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class quine extends JFrame implements ActionListener, WindowListener{
/**
*
*/
private static final long serialVersionUID = 1L;
static ArrayList<Term>[][] table=new ArrayList[5][5]; // SERVES AS OUR STORAGE FOR ARRANGING TERMS AND TO OUR RESULTING TERMS THAT ARE BEING COMPARED.
static Vector<String> inputTerm= new Vector <String>(); // STORES OUR ORIGINAL INPUT/NUMBERS
static Vector<String> resultingTerms= new Vector <String>(); // STORES RESULTING TERMS FOR EACH SET OF COMPARISON
static int var=0; //NUMBER OF VARIABLE
static int numbers=0; //NUMBER OF INPUTS
final static int maxTerms=1000; //MAXIMUM NUMBER OF TERMS WITH SAME NUMBER OF 1'S
static TextField result = new TextField(" ",50);
static TextField text;
static TextField text1;
static quine qWindow;
static String finalT = "";
public static void main(String[] args){
qWindow = new quine("Quine-McCluskey Simulator"); //creates a window
qWindow.setSize(400,250); //sets the size of the window
qWindow.setVisible(true); //makes the window visible
qWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); //CLOSES THE WINDOW WHEN CLOSING OR CLICKING THE X BUTTON
JOptionPane.showMessageDialog(null, "Welcome to my Quine-McCluskey Simulator!"); //DISPLAYS MESSAGE
}//end main
public static int count_one(String temp){ //COUNT 1'S FROM EACH TERM
int count=0;
char[] tempArray=temp.toCharArray();
int i=0;
while(i<temp.length()){
if(tempArray[i]=='1'){
count++;
}
i++;
}//end while
return count;
}//end one
public static void getPrimeImplicants(){ // PAIRS TERMS UNTIL NOTHING TO PAIR, END TERMS ARE OUR PRIME IMPLICANTS
table=createTermTable(inputTerm);
printTermTable();
createPairing();
}
public static ArrayList<Term>[][] createTermTable(Vector <String> input){ // CREATE TABLE, ARRANGES TERMS BASED ON THE NUMBER OF 1'S IN
//EACH TERM USING count_one,therefore, row 1 contains terms with 1 1 bit.
Term temp;
int one=0;
int element=0;
ArrayList[][] arrayLists = new ArrayList[var+1][maxTerms+1]; //CREATES AN ARRAY WITH VAR ROWS CORRESPONDING TO POSSIBLE NUMBER OF
// 1 FOR EACH TERM AND 1000 COLUMNS
ArrayList<Term> [][]tempTable = arrayLists;
for(int x=0;x<=var;x++){ //?
for(int y=0;y<=maxTerms;y++){
tempTable[x][y]= new ArrayList<Term>();
}//end y
}//end for x
for(int i=0;i<input.size();i++){
one=count_one(input.get(i)); //COUNT 1'S FROM EACH TERM
temp=initTerm(input.get(i),false); // INITIALIZE PROPERTIES OF THAT TERM
while(!tempTable[one][element].isEmpty()){
element++;
}//end while
tempTable[one][element].add(temp);
element=0;
} //end for
return tempTable;
}//end createTermTable
public static Term initTerm(String n,boolean u){ //INITIALIZE USED and NUM PROPERTY OF THE TERM
Term term=new Term();
term.used=u; // TO INDICATE IF THE TERM IS ALREADY PAIRED
term.num=n; // THE TERM ITSELF
return term;
}//end initTerm
public static void printTermTable(){ // PRINTS THE COMPUTATION/TABLES
System.out.println("\nCOMPUTING:");
for(int i=0;i<var+1;i++){
System.out.print(i);
System.out.println(" --------------------------------------------");
for(int j=0;!table[i][j].isEmpty();j++){ //PRINTS TERM ON EACH ROW WHILE TERM IS NOT EMPTY
System.out.println(table[i][j].get(0).num);
}//end for j
}//end for i
}
public static void createPairing(){ //PAIRS A TERM TO EACH TERM ON THE NEXT ROW
int finalterms=0;
String term_num="";
int found=0;
Vector<String> preResult= new Vector<String>();
for(int x=0;x<=var-1;x++){ // REPEATS PAIRING OF A TERMS OF THE TABLE VAR TIMES TO MAKE SURE WHAT ARE LEFT ARE PRIME IMPLICANTS
preResult=new Vector<String>(); // STORES THE RESULTING TERMS FOR EACH SET OF PAIRING
for(int i=0;i<=var;i++){ //COMPARES A ROW WITH EACH TERMS ON THE NEXT ROW
//Vector <String> rowResult= new Vector<String>(); //STORES RESULTING TERMS ON THAT PARTICULAR TERM OF THE ROW. THIS IS TO AVOID REPETITIONS
for(int j=0;!table[i][j].isEmpty();j++){ // TERM ON THE ROW BEING COMPARED WITH EACH TERM ON NEXT ROW
if(i+1!=var+1) // MAKES SURE THAT THE PROCESS NOT EXCEEDS THE ARRAYBOUND
for(int k=0;!table[i+1][k].isEmpty();k++){ //TERM ON THE NEXT ROW THAT IS BEING COMPARED WITH TERM ON THE CURRENT ROW.
term_num=pair(table[i][j].get(0).num,table[i+1][k].get(0).num); //ASSIGNS RESULT OF PAIRING TO term_num
if(term_num!=null){ // IF PAIRING IS SUCCESSFUL
table[i+1][k].get(0).used=true; // TERM IS PAIRED/USED
/*if(!rowResult.contains(term_num)){ //MAKES SURE THAT TERM IS NOT REPEATE
rowResult.add(term_num);
found=1;
}
*/
if(!preResult.contains(term_num)){ // MAKES SURE THAT TERM IS NOT REPEATED
preResult.add(term_num);
found=1;
finalterms++; // COUNTS THE FINAL/RESULTING TERMS FOR THIS SET OF PAIRING
}//end if !resultingTerms
found=1;
}//end if term_num!=null
}//end for k
if(found==0){ // IF TERM IS NOT SUCCESSFULLY PAIRED/USED, ADD TO THE RESULTING TERMS FOR THIS SET
if(table[i][j].get(0).used!=true){
preResult.add(table[i][j].get(0).num);
}
}
found=0;
}//end for j
}//end for i
table=createTermTable(preResult); // CREATE ANOTHER TABLE FOR NEXT SET. THE NEW TABLE CONTAINS THE RESULTING TERMS OF THIS SET
if(preResult.size()!=0)
resultingTerms=preResult; //IF THE ARE RESULTING TERMS, THEN PRINT AND ASSIGN TO resultingterms. THE END VALUE OF resultingterms WILL BE SIMPLIFIED
printTermTable();
}//end for x
}//end createPairing
public static String pair(String a,String b){
int difference=-1;
char []array1 = new char[a.length()];
char []array2;
for(int i=0;i<var;i++){
array1=a.toCharArray(); //CONVERTS TERMS OF TYPE STRING TO TERMS OF TYPE CHAR
array2=b.toCharArray();
if(array1[i]!=array2[i]){ // IF NOT EQUAL FOR A PARTICULAR CHARACTER FOR THE FIRST TIME, THEN GET THE INDEX CORRESPONDING TO THAT CHARACTER.
if(difference==-1)
difference=i;
else //IF NOT NOT EQUAL FOR THE FIRST TIME, THEN THE TERMS DIFFER IN MORE THAN 1 PLACE
return null;
}//end if
}//end for
if(difference==-1) //THE TERMS ARE INDENTICAL, RETURN NULL, PAIRING UNSUCCESSFUL
return null;
char[] result= a.toCharArray(); //CHARACTER CORRESPONDING TO THE INDEX WHERE TERMS DIFFER ONLY ONCE WILL BE CHANGED TO '-'
result[difference]='-';
String resulTerm= new String(result);
return resulTerm; //RETURNS THE MODIFIED TERM, PAIRING SUCCESSFUL
}//end pair
public static void simplifymore(){ //SIMPLIFY THE RESULTINGTERMS
int primes=resultingTerms.size(); // RESULTING TERMS CORRESPOND TO OUR PRIME IMPLICANTS
int[][] s_table= new int[primes][numbers]; //CREATES A TABLE WITH ROWS EQUAL TO NUMBER OF PRIME IMPLICANTS AND COLUMNS EQUAL TO THE NUMBER OF THE ORIGINAL INPUT
for(int i=0;i<primes;i++){
for(int j=0;j<numbers;j++){
s_table[i][j]=implies(resultingTerms.get(i),inputTerm.get(j));
}//end for j
}//end for i
Vector <String> finalTerms= new Vector<String>(); // STORES THE FINALTERMS
int finished=0;
int index=0;
while(finished==0){ //UNTIL ALL ELEMENTS ARE NOW TURNED TO 0
index=essentialImplicant(s_table);
if(index!=-1)
finalTerms.add(resultingTerms.get(index)); // IF RESULTING TERM IS THE ONLY ONE IMPLYING THE CURRENT ORIGINAL TERM, THEN ADD TO FINAL TERMS
else{ // THOSE THAT HAVE MORE THAN ONE IMPLICATION FOR A PARTICULAR ORIGINAL TERM
index=largestImplicant(s_table);
if(index!=-1)
finalTerms.add(resultingTerms.get(index)); //ADD TO FINAL TERMS IF LARGEST IMPLICANT(ONE WHICH HAS MORE NUMBER OF 1'S. SEE COMMENTS ON largestImplicant.
else
finished=1; //IF INDEX IS -1 THEN ALL ELEMENTS HAVE ALREADY BEEN DELETED OR HAVE MADE VALUE 0
}//end else
}//end while finished
System.out.println("Final Terms :");
for(int x=0;x<finalTerms.size();x++) //PRINTS THE FINAL TERMS IN BINARY FORMAT
System.out.println(finalTerms.get(x));
printSimplified(finalTerms);
}//end simplifymore
public static void printSimplified(Vector <String> finalTerms){
String temp="";
char[] tempArray;
char variables[]= {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; //basis for our variables to printed
int index=0;
int i=0;
int j=0;
System.out.print("F = ");
while(i<finalTerms.size()){ //until all final terms are printed in algebraic form.
temp=finalTerms.get(i); //assigns current final term to temp
tempArray=temp.toCharArray(); //CONVERTS TEMP TO ARRAY
while(j<var){
if(tempArray[j]=='-'){ //IGNORES -
index++;
}
else if (tempArray[j]=='0'){
finalT+=variables[26-var+index]+"'"; // PRINTS THE CORRESPONDING LETTER.IF CHARACTER IS 0 THEN APPEND ' AFTER THE VARIABLE
index++;
}
else if (tempArray[j]=='1'){
finalT+=variables[26-var+index]; // PRINTS CORRESPONDING LETTER
index++;
}
else{};
j++;
}//end while
if(i<finalTerms.size()-1)
finalT+=" + "; // APPENDS +
i++;
temp="";
j=0;
index=0;
}//end while
System.out.println(finalT);
}//print simplified
public static int essentialImplicant(int[][] s_table){ // CHECKS EACH RESULTING TERM IMPLYING A PARTICULAR ORIGINAL TERM
for(int i=0;i<s_table[0].length;i++){ //
int lastImplFound=-1;
for(int impl=0;impl<s_table.length;impl++){
if(s_table[impl][i]==1){ //IF RESULTING TERM IMPLIES ORIGINAL TERM
if(lastImplFound==-1){
lastImplFound=impl;
}else{ // IF MORE THAN ONE IMPLICATION,THEN IT IS NOT AN ESSENTIAL PRIME IMPLICANT.GO TO NEXT ORIGINAL TERM
lastImplFound=-1;
break;
}//end else
}
}
if(lastImplFound!=-1){ // ONE IMPLICATION FOR THE ORIGINAL TERM. THIS IS AN ESSENTIAL PRIME IMPLICANT
implicant(s_table,lastImplFound);
return lastImplFound;
}
}//end for impl
return -1;
}
public static void implicant(int [][] s_table,int impA){ // DELETE OR MAKE VALUE 0 THE ROW WHERE THE ESSENTIAL PRIME IMPLICANT IS AND THE COLUMNS OF ALL THE ORIGINAL TERMS IMPLIED BY IT
for(int i=0;i<s_table[0].length;i++){
if(s_table[impA][i]==1)
for(int impB=0;impB<s_table.length;impB++){
s_table[impB][i]=0;
}
}
}//end implicant
public static int largestImplicant(int[][] s_table){
int maxImp=-1;
int max=0;
for(int imp=0;imp<s_table.length;imp++){ // LOCATES WHICH HAS MORE NUMBER OF 1'C IN EACH PRIME
int num=0;
for(int i=0;i<s_table[0].length;i++){
if(s_table[imp][i]==1)
num++;
}//end for i
if(num>max){ // TERM WITH MORE 1'S AT THE END OF THE LOOP WILL BE ADDED TO THE FINAL TERMS
max=num;
maxImp=imp;
}//end if num>max
}//end for imp
if(maxImp!=-1){ // IF WE HAVE SUCCESSFULLY LOCATED A PRIME IMPLICANT
implicant(s_table,maxImp); // DELETE OR MAKE VALUE 0 THE ROW WHERE THE ESSENTIAL PRIME IMPLICANT IS AND THE COLUMNS OF ALL THE ORIGINAL TERMS IMPLIED BY IT
return maxImp;
}//end if maxImp!=-1
return -1;
}
public static int implies(String term1, String term2){ // RETURNS 1 IF RESULTING TERM IMPLIES THE ORIGINAL TERM, 0 OTHERWISE
char[] term1Array=term1.toCharArray();
char[] term2Array=term2.toCharArray();
//EX. ORG TERM IS 100100, RES TERM IS 1--10- ,RESULTING TERM IMPLIES THE ORIGINAL TERM. SINCE - HERE IS TREATED AS 0 OR 1
for(int i=0;i<var;i++){
if(term1Array[i]!=term2Array[i] && term1Array[i]!='-')
return 0;
}
return 1;
}
//end class
public quine(String name){
super(name); //ASSIGNS LABEL OF THE WINDOW
setLayout(new GridLayout(1, 1)); //SETS THE LAYOUT
setLocation(350,200); //SETS THE LOCATION OF THE WINDOW ON THE SCREEN
GridBagLayout gridbag = new GridBagLayout(); //used to align buttons
GridBagConstraints constraints = new GridBagConstraints();//to specify the size and position for the gridbaglayout
setLayout(gridbag);
constraints.weighty = 1; //distributes spaces among columns
constraints.weightx = 1; //distributes spaces among rows
constraints.gridwidth = GridBagConstraints.REMAINDER; //to specify that the component be the last one in its column
Label label1 = new Label(" How many variables does the function have?"); //CREATES A LABEL
label1.setVisible(true); //MAKES THE LABEL VISIBLE, ASSIGNS NEW FONT AND COLOR THEN ADD TO THE WINDOW
label1.setFont(new Font("Sans Serif", Font.BOLD, 12));
label1.setBackground(Color.CYAN);
add(label1);
text1= new TextField("",10);
gridbag.setConstraints(text1,constraints); //applies constraints to text
text1.setEditable(true); //SO THAT WE COULD STILL HAVE AN UNLIMITED LENGTH OF INPUT
text1.setForeground(Color.black);
text1.setBackground(Color.white);
text1.setVisible(true);
text1.addActionListener(this); //ACTIVATES ACTIONLISTENER FOR THIS FIELD
add(text1);
label1 = new Label(" Please list all the minterms that evaluates to 1:");//CREATES LABEL
gridbag.setConstraints(label1,constraints);
label1.setVisible(true); //MAKES THE LABEL VISIBLE, ASSIGNS NEW FONT AND COLOR THEN ADD TO THE WINDOW
label1.setBackground(Color.green);label1.setFont(new Font("Sans Serif", Font.BOLD, 12));
label1.setBackground(Color.CYAN);
add(label1);
text = new TextField("Enter your numbers here separated by a comma",50);
gridbag.setConstraints(text,constraints); //applies constraints to text
text.setEditable(true); //ENABLES UNLIMITED LENGTH OF INPUT
text.setForeground(Color.black);
text.setBackground(Color.white);
text.setVisible(true);
text.addActionListener(this); //ACTIVATES ACTIONLISTENER FOR THIS FIELD
text.setForeground(Color.blue);
add(text);
JButton enter = new JButton ("Enter"); // CREATES BUTTON NAMED Enter
enter.setVisible(true); //MAKES IT VISIBLE, APPLIES THE CONSTRAINTS, AND ADD TO THE WINDOW
add(enter);
enter.setBackground(Color.green);
enter.addActionListener(this); //ACTIVATES ACTIONLISTENER
gridbag.setConstraints(enter,constraints);
JButton reset = new JButton ("Reset"); // CREATES BUTTON NAMED Reset
reset.setVisible(true); //MAKES IT VISIBLE, APPLIES THE CONSTRAINTS, AND ADD TO THE WINDOW
gridbag.setConstraints(reset,constraints);
reset.setBackground(Color.green);
add(reset);
reset.addActionListener(this);
label1 = new Label(" Result:");
gridbag.setConstraints(label1,constraints);
label1.setVisible(true);
label1.setBackground(Color.cyan);
label1.setFont(new Font("Sans Serif", Font.BOLD, 12));
label1.setBackground(Color.CYAN);
add(label1);
result = new TextField(" ",50);
gridbag.setConstraints(result,constraints); //applies constraints to text
result.setEditable(true);
result.setForeground(Color.black);
result.setBackground(Color.white);
result.setVisible(true);
add(result);
}
public void actionPerformed(ActionEvent e) {
String stringInput="";
String numOfVar="";
String temp="";
String temp1="";
int num=0;
if(e.getActionCommand() == "Enter"){ // IF Enter BUTTON IS CLICKED
stringInput = text.getText(); // GETS STRING INPUT FROM TEXT(MINTERMS)
numOfVar = text1.getText(); //GETS STRING INPUT FROM TEXT1(VARIABLES)
var = Integer.parseInt(numOfVar); //CONVERTS numOfVar TO INTEGER
StringTokenizer token= new StringTokenizer(stringInput," ,"); //TOKENIZE INPUT. ELIMINATE ALL COMMAS AND SPACES
while(token.hasMoreTokens()){ //WHILE THERE ARE MORE TOKENS
temp1=token.nextToken(); //GETS TOKEN
numbers++; //COUNTS THE NUMBER OF INPUTS
num=Integer.parseInt(temp1); //CONVERT INPUT TO INTEGER
temp=Integer.toBinaryString(num); //CONVERTS INTEGER FORM OF INPUT TO BINARY IN ITS PROPER LENGTH BASED ON THE NUMBER OF VARIABLES GIVEN
if(temp.length()!=var){
while(temp.length()!=var){
temp="0"+temp;
}
}
inputTerm.add(temp); //ADDS RESULT(BINARY FORM) TO inputTerm
}//end while
getPrimeImplicants(); //GET PRIMEIMPLICANTS
simplifymore(); // SIMPLIFY MORE
result.setText(finalT); // DISPLAYS THE RESULT (SIMPLIFIED) TO RESULT TEXTFIELD
}//end if
if(e.getActionCommand()== "Reset"){ //RESETS THE VALUES FOR NEXT SET OF INPUTS
var=0;
table=new ArrayList[5][5]; // SERVES AS OUR STORAGE FOR ARRANGING TERMS AND TO OUR RESULTING TERMS THAT ARE BEING COMPARED.
inputTerm= new Vector <String>(); // STORES OUR ORIGINAL INPUT/NUMBERS
resultingTerms= new Vector <String>(); //STORES THE RESULTING TERMS FOR EACH COMPUTATION
finalT=""; //STORES THE FINAL TERMS IN THEIR ALGEBRAIC FORM
numbers=0; //COUNTS THE NUMBER OF INPUTS FROM THE USER
text.setText(""); //ERASE THE TEXTS DISPLAYED ON THE TEXT FIELDS
text1.setText("");
result.setText("");
}
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {qWindow.setVisible(false);} //CLOSES THE WINDOW AFTER PROGRAMS STOPS RUNNING
public void windowClosing(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
}//end class
And this is the second class. Funny (it only consists of less than 10 lines unlike the first one)
public class Term {
public String num;
public boolean used;
}
Can you please help me incorporate these two classes into one (if that's possible)? I tried declaring String num and boolean used inside the first class and removed the Term but it shows lot of errors. I tried separating the initTerm method into two: one returns a string and the other boolean. But it adds error. What else can I do? Can you also advise me some techniques in doing this?
You can put the whole class inside quine so that Term becomes an inner class of quine. The class Term is then visible to the code in quine and you don't need a separate file anymore for the class Term.
1) Don't declare variables as public unless they are static. It's standard practise to use getter/setter methods to access member variables.
2) Have you tried creating a super class that extends JFrame then get quine to extend that class. Add getter/setters in the super class. Then the sub class can access them via getVariable() etc etc.
3) Class names should start with an upper case letter. Variables, methods and package names should all be lower case.
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
public class vasilisTable extends JFrame {
Object[] split_data_l;
Object[][] split_data;
Object [][] split_data_clone;
Object [][] split_data_reverse;
Object [][] split_data_reverse_num;
String[] temp;
private JTable table;
private JPanel bottom_panel;
private JLabel average;
private JLabel max_dr;
public vasilisTable(String name, String data, int choice)
{
super(name);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //the DISPOSE_ON_CLOSE means that when we press the x button is will close only the table window and not the whole programm
//this.getAccessibleContext().setAccessibleName(name);
//System.out.println(this.getAccessibleContext().getAccessibleName());
setSize(800,600);
String[] columnNames = {"Date", "Open","High","Low","Close",
"Volume", "Adjusted" };//defines the column names
//------ Start of making the arrays that will be used as data for the table creation
split_data_l = data.split( "\n" );
int lngth = split_data_l.length;
split_data = new Object[lngth-1][7];
split_data_clone = new Object[lngth-1][7];
split_data_reverse= new Object[lngth-1][7];
split_data_reverse_num= new Object[lngth-1][7];
double sum = 0;
for(int k=1; k<split_data_l.length; k++) //initializing the three arrays with the data we got from the URLReader
{
temp = split_data_l[k].toString().split(",");
for (int l=0; l<temp.length; l++)
{
split_data[k-1][l] = temp[l];
split_data_clone[k-1][l] = temp[l];
split_data_reverse[k-1][l] = temp[l];
split_data_reverse_num[k-1][l] = temp[l];
}
}
for(int k=split_data_l.length-2; k>=1; k--) // making of the clone array that contains all the last column with colours
{
Double temp = Double.parseDouble(split_data[k][6].toString());
Double temp1 = Double.parseDouble(split_data[k-1][6].toString());
double check =temp-temp1;
if (check>0)
{
String color_temp = "<html><span style = 'color:red'>" + split_data_clone[k-1][6] +"</span></html>" ;
split_data_clone[k-1][6] = color_temp;
}
else
{
String color_temp = "<html><span style = 'color:green'>" +split_data_clone[k-1][6]+"</span></html>" ;
split_data_clone[k-1][6] = color_temp;
}
}
int l = split_data_clone.length;
int m = l-1;
for (int i=0; i<l; i++) //making of the reversed array
{
for (int j = 0; j<=6; j++)
{
split_data_reverse[i][j]=split_data_clone[m][j];
}
m--;
}
m = l-1;
for (int i=0; i<l; i++) //making of the reversed array
{
for (int j = 0; j<=6; j++)
{
split_data_reverse_num[i][j]=split_data[m][j];
}
m--;
}
//------ End of making the arrays that will be used as data for the table creation
//------ Start of calculating the average
for (int i=0; i<lngth-1; i++)
{
Double temp = Double.parseDouble(split_data[i][6].toString());
sum = sum+temp;
//System.out.println("turn "+i+" = "+split_data[i][6]);
}
float avg = (float) (sum/(lngth-1));
avg = Round((float) avg,2);
String avg_str;
avg_str = "<html>Average: <b>"+avg+"</b></html>";
//"<html><b>Average: </b></html>"
//------ End of calculating the average
//------ Start of Calculating the Maximal Drawdown
double high=0;
double low=100000000;
double drawdown=0;
double max_drawdown=0;
int last_high=0;
int last_low=0;
for (int i=0; i<lngth-1; i++)
{
Double temp = Double.parseDouble(split_data_reverse_num[i][6].toString());
//Double temp1 = Double.parseDouble(split_data[i+1][6].toString());
if (temp>high)
{
high = temp;
last_high = i;
//System.out.println("max high = "+temp);
}
else
{
low = temp;
last_low = i;
//System.out.println("max low = "+temp);
}
if (last_low>last_high)
{
drawdown = high-low;
//System.out.println("drawdown = "+drawdown);
}
if (drawdown>max_drawdown)
{
max_drawdown = drawdown;
}
}
//System.out.println("max dr = "+max_drawdown);
String max_dr_str = "<html>Maximal Drawdown: <b>"+max_drawdown+"</b></html>";
//------ End of Calculating the Maximal Drawdown
average = new JLabel(avg_str);
max_dr = new JLabel(max_dr_str);
bottom_panel = new JPanel();
String space = " ";
JLabel space_lbl = new JLabel(space);
bottom_panel.add(average);
bottom_panel.add(space_lbl);
bottom_panel.add(max_dr);
//-------- Start of table creation ---------
if(choice==1)
{
table = new JTable(split_data_clone, columnNames);//creates an instance of the table with chronological order
}else
{
table = new JTable(split_data_reverse, columnNames);//creates an instance of the table with reverse chronological order
}
TableColumn column = null;
for (int i = 0; i < 7; i++) {
column = table.getColumnModel().getColumn(i);
if (i == 0) {
column.setPreferredWidth(100); //third column is bigger
} else if (i == 5) {
column.setPreferredWidth(85); //third column is bigger
}
else if (i == 6) {
column.setPreferredWidth(70); //third column is bigger
}
else {
column.setPreferredWidth(50);
}
}
table.setShowGrid(true);
table.setGridColor(Color.black);
//-------- End of table creation ---------
JPanel table_panel = new JPanel (new BorderLayout());
JScrollPane table_container = new JScrollPane(table); // create a container where we will put the table
//table.setFillsViewportHeight(true); // if the information are not enough it still fill the rest of the screen with cells
table_panel.add(table_container, BorderLayout.CENTER);
table_panel.add(bottom_panel, BorderLayout.SOUTH);
//table_panel.add();
setContentPane (table_panel);
pack(); // here i pack the final result to decrease its dimensions
}
public float Round(float Rval, int Rpl) // this functions rounds the number to 2 decimal points
{
float p = (float)Math.pow(10,Rpl);
Rval = Rval * p;
float tmp = Math.round(Rval);
return (float)tmp/p;
}
}
I am making an application which creates various instances of a class. These instances are actually some windows. After having create multiple of these windows, how can I access one of them and bring it in front? I know the .tofront() method, but how can I specify the window that I want to bring in front?
Above is the code that creates every window. My main problem is that after I have create e.g 5 windows, how can I access one of them?
ps
code that creates each window:
if (sData != null) {
//System.out.println("Success, waiting response");
vasilisTable ftable = new vasilisTable(name, sData, choice);
hashMap.put(name, ftable);
ftable.setVisible(true);
//choice=2;
}
My main problem is that after I have create e.g 5 windows, how can I access one of them?
You have to keep a reference to the relevant objects in variables or an array or a collection or something. The "bring it to the front" function needs to:
figure out what domain object needs to be brought to the front,
lookup its corresponding JFrame, and
call toFront() on it.
Java provides no built-in mechanisms for finding previously created instances of objects.
When you create your various instances of the above JFrame, you can keep track of the created instances, may be store them within a HashMap, then you can pick the right JFrame instance basing on its designated name and bring it to the front. Have a look at the below code for more illustration:
HashMap<String, VasilisTable> hashMap = new HashMap<String, VasilisTable>();
JFrame firstWindow = new VasilisTable("firstWindow",data, choice);
hashMap.put("firstWindow", firstWindow);
JFrame secondWindow = new VasilisTable("secondWindow",data, choice);
hashMap.put("secondWindow", secondWindow);
JFrame thirdWindow = new VasilisTable("thirdWindow",data, choice);
hashMap.put("thirdWindow", thirdWindow);
// To bring a certain window to the front
JFrame window = hashMap.get("firstWindow");
window.setVisible(true);
window.toFront();
Are these JFrame or JWindow objects? If they are you can call -
jframe.setVisible(true);
jframe.toFront();
This is something interesting I found at the API doc.
Places this Window at the top of the stacking order and shows it in
front of any other Windows in this VM. No action will take place if
this Window is not visible. Some platforms do not allow Windows which
own other Windows to appear on top of those owned Windows. Some
platforms may not permit this VM to place its Windows above windows of
native applications, or Windows of other VMs. This permission may
depend on whether a Window in this VM is already focused. Every
attempt will be made to move this Window as high as possible in the
stacking order; however, developers should not assume that this method
will move this Window above all other windows in every situation.
I would recommend you to check out these answers as well.
Java Swing: JWindow appears behind all other process windows, and will not disappear
Java: How can I bring a JFrame to the front?