I am working on a knight's tour problem with basic gui,i want to take user input in the two text fields which make up the (x,y) from user and then in one text box i print if solution is posiible and in the other i write the path adopted by the knight.My algorithm works fine and i have problem in gui.i have given a few default values to (x,y) for that i get correct output .But when i change the value of (x,y) in the textfield,no change occurs.this is the main file ,there is another event handler file,which is below it.Your help will be sincerely appreciated.I am working on processing 2.2.1.This is how the output screen looks like
Main file/project.pde
// Need G4P library
import g4p_controls.*;
Maxim maxim;
AudioPlayer player;
int x=-1;
int y=-1;
String solution="";
PImage img;
int count=0;
public void setup(){
size(480, 320, JAVA2D);
maxim=new Maxim(this);
player=maxim.loadFile("song.wav");
player.setLooping(true);
img=loadImage("chess.jpg");
createGUI();
customGUI();
// Place your setup code here
}
int t[]=new int[25];
boolean visited[][]=new boolean[5][5];
int check[][]=new int[5][5];
boolean b;
int counter=-1;
boolean move(int x,int y , int m){
boolean result=false;
if (x<0 || x>=5 || y<0 || y>=5 || visited[x][y]==true)
{
return false;
}
visited[x][y]=true;
if (m==24)
{
visited[x][y]=true;
return true;
}
else
{
String xstring=String.valueOf(x);
String ystring=String.valueOf(y);
solution=solution+xstring+","+ystring+" ";
print (x);
print(",");
print(y);
check[x][y]=counter+1;
if (move(x+2,y+1,m+1) || move(x+2,y-1,m+1)
|| move(x-2,y+1,m+1) || move(x-2,y-1,m+1)
|| move(x+1,y+1,m+1) || move(x+1,y-1,m+1)
|| move(x-1,y+1,m+1) || move(x-1,y-1,m+1)){
print (x);
print(",");
print(y);
//check[x][y]=1;
return true;
}
return false;
}
}
public void draw(){
counter=counter+1;
background(0,128,128);
image(img,0,0,480,320);
player.play();
textarea2.setText(solution);
String txt1 = textfield1.getText();
x = Integer.parseInt(txt1);
String txt2 = textfield2.getText();
y= Integer.parseInt(txt2);
print(solution);
if(x>=0 && y>=0)
{
b=move(x,y,0);
if(b==false)
{
textarea1.setText("Solution is not possible,enter other coordinates");
}
if(b==true)
{
textarea1.setText("Congratulations solution is possible");
}
}
if(count%8==0)
{
delay(1000);
println(counter);
}
}
void keyPressed()
{
if (key==13)
{
solution="";
print(solution);
textarea2.setText(solution);
String txt1 = textfield1.getText();
x = Integer.parseInt(txt1);
String txt2 = textfield2.getText();
y= Integer.parseInt(txt2);
}
if(x>=0 && y>=0)
{
b=move(x,y,0);
if(b==false)
{
textarea1.setText("Solution is not possible,enter other coordinates");
}
if(b==true)
{
textarea1.setText("Congratulations solution is possible");
}
}
}
// Use this method to add additional statements
// to customise the GUI controls
public void customGUI(){
}
this is the event handlers file
/* =========================================================
* ==== WARNING ===
* =========================================================
* The code in this tab has been generated from the GUI form
* designer and care should be taken when editing this file.
* Only add/edit code inside the event handlers i.e. only
* use lines between the matching comment tags. e.g.
void myBtnEvents(GButton button) { //_CODE_:button1:12356:
// It is safe to enter your event code here
} //_CODE_:button1:12356:
* Do not rename this tab!
* =========================================================
*/
public void tf1(GTextField source, GEvent event) { //_CODE_:textfield1:418637:
println("textfield1 - GTextField >> GEvent." + event + " # " + millis());
} //_CODE_:textfield1:418637:
public void tf2(GTextField source, GEvent event) { //_CODE_:textfield2:859413:
println("textfield2 - GTextField >> GEvent." + event + " # " + millis());
} //_CODE_:textfield2:859413:
public void ta1(GTextArea source, GEvent event) { //_CODE_:textarea1:252891:
println("textarea1 - GTextArea >> GEvent." + event + " # " + millis());
} //_CODE_:textarea1:252891:
public void ta2(GTextArea source, GEvent event) { //_CODE_:textarea2:483845:
println("textarea2 - GTextArea >> GEvent." + event + " # " + millis());
} //_CODE_:textarea2:483845:
public void slider1_change1(GSlider source, GEvent event) { //_CODE_:slider1:280049:
println("slider1 - GSlider >> GEvent." + event + " # " + millis());
} //_CODE_:slider1:280049:
public void slider2_change1(GSlider source, GEvent event) { //_CODE_:slider2:362722:
println("slider2 - GSlider >> GEvent." + event + " # " + millis());
} //_CODE_:slider2:362722:
// Create all the GUI controls.
// autogenerated do not edit
public void createGUI(){
G4P.messagesEnabled(false);
G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
G4P.setCursor(ARROW);
if(frame != null)
frame.setTitle("Sketch Window");
textfield1 = new GTextField(this, 210, 32, 160, 30, G4P.SCROLLBARS_NONE);
textfield1.setText("1");
textfield1.setPromptText("Enter x-Cordinate");
textfield1.setOpaque(true);
textfield1.addEventHandler(this, "tf1");
textfield2 = new GTextField(this, 204, 96, 160, 30, G4P.SCROLLBARS_NONE);
textfield2.setText("1");
textfield2.setPromptText("Enter y Cordinate");
textfield2.setLocalColorScheme(GCScheme.PURPLE_SCHEME);
textfield2.setOpaque(true);
textfield2.addEventHandler(this, "tf2");
textarea1 = new GTextArea(this, 53, 196, 160, 80, G4P.SCROLLBARS_NONE);
textarea1.setLocalColorScheme(GCScheme.GREEN_SCHEME);
textarea1.setOpaque(true);
textarea1.addEventHandler(this, "ta1");
textarea2 = new GTextArea(this, 288, 192, 160, 80, G4P.SCROLLBARS_NONE);
textarea2.setLocalColorScheme(GCScheme.YELLOW_SCHEME);
textarea2.setOpaque(true);
textarea2.addEventHandler(this, "ta2");
slider1 = new GSlider(this, 96, 276, 264, 40, 10.0);
slider1.setLimits(0.5, 0.0, 1.0);
slider1.setNumberFormat(G4P.DECIMAL, 2);
slider1.setOpaque(false);
slider1.addEventHandler(this, "slider1_change1");
slider2 = new GSlider(this, 348, 240, 100, 36, 10.0);
slider2.setLimits(0.5, 0.0, 1.0);
slider2.setNumberFormat(G4P.DECIMAL, 2);
slider2.setOpaque(false);
slider2.addEventHandler(this, "slider2_change1");
}
// Variable declarations
// autogenerated do not edit
GTextField textfield1;
GTextField textfield2;
GTextArea textarea1;
GTextArea textarea2;
GSlider slider1;
GSlider slider2;
There are some problems with above constructions.
You do recursive move() method inside draw() method. But in Processing draw() is called many times in each second by animation thread.
Common desing for such cases is:
you should have variables that hold application state (logical state)
what your draw() method draws depends only on state
state may be modified by animation thread or by any other thread
I suggest changing your code a little:
First - state variables:
// 1 - display status, wait to enter values
// 2 - check if x and y are correct
// 3 - solve problem
int state=1;
boolean solutionExists=false;
public void setup() {
...
}
next draw() method:
void draw() {
counter=counter+1;
background(0,128,128);
String coords = "(" + x + "," + y + ")";
if (state == 1) {
if(solutionExists) {
textarea1.setText("Congratulations solution is possible " + coords);
} else {
textarea1.setText("Solution is not possible,enter other coordinates " +coords);
}
return;
}
if (state == 2) {
readXY();
return;
}
if (state == 3) {
println("find solution for: " + coords);
solutionExists = move(x,y,0);
state = 1;
return;
}
}
public void readXY() {
try {
x = Integer.parseInt(textfield1.getText().trim());
y = Integer.parseInt(textfield2.getText().trim());
state = 3;
} catch(Exception e) {
state = 1;
}
}
and finally textfields handlers:
public void tf1(GTextField source, GEvent event) {
if (event.getType().equals("LOST_FOCUS")) {
state=2;
}
}
public void tf2(GTextField source, GEvent event) {
if (event.getType().equals("LOST_FOCUS")) {
state=2;
}
}
As you can see:
if state==1 - draw() only updates message
if state==2 - draw() checks if x and y are valid, if valid -> change state to 3
if state==3 - draw() performs recursive algo, update solutionExists variable, change state to 1
Anytime when your textfield1 or textfield2 loose focus, it changes state to 2.
draw() is driven only by application state.
application state is modified by other events.
For best results, recursive algo should be performat in another thread, not in animation thread.
One fine note: when you edit textfield it may contains string such as "" (empty) or " 3" (leading space) or " 3 " etc - such text cannot be parsed with Integer.parseInt - you need to trim such text and be sure that NumberFormatException is not thrown - see readXY() method.
Related
I'm currently setting up a quiz program right now, where the code will loop 20 times. There are 20 question, 80 answers (4 to each question) and the program will set JButtons and action listeners to listen for the user's answer for each question, then call the original quiz loop again from the correct or incorrect answer.
However, the program I'm having is that the program will display question 1, then question 2, but then question 4, 8, 16 and finally 20 since it cannot double anymore.
I've tried:
Breakpointing the value of the currentQuestion variable, as I believe this is the one which is doubling and causing the problem.
Creating a new method named updateQuestionAndAnswer to ensure that it's not a misclick.
Removing the other three action listeners, which helped me to find that it was whatever action listener that I clicked that was repeatedly being called. E.g if it was on Question 8 and I clicked listener 3, it would call listener 3 another 8 times.
I've enclosed the code below I think will be useful. All variables that you cannot see defined within here, are defined to 0 or "" in a static context outside of methods at the top of my program.
* Defines and places all of the content for the Game page.
* #throws IOException
*/
public void setupGamePanel() throws IOException {
// Game page card constructor
card2 = new JPanel() {
// Make the window the specified sizes
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
size.width += extraWindowWidth;
size.height += extraWindowHeight;
return size;
}
};
// JAVA COMPONENT DEFINITIONS
String[] openingDialogue = new String[]{"<html><span style='font-size:20px;'>We need your help. After Junko Enoshima took over Hope’s Peak Academy, she trapped the entire of Class 78 inside a monstrous killing game! My creator, Chihiro Fujisaki, is one of them.", "<html><span style='font-size:20px;'>I managed to patch into the systems that the Ultimate ******** setup when he fell into despair, but we’ve been hit with a security system implemented by Jin Kirigiri before he…", "<html><span style='font-size:20px;'>Anyway, we need to get a good enough score out of twenty quiz questions in order to get inside the network, where my creator should be waiting for me.", "<html><span style='font-size:20px;'>Let’s do this. Answer the questions as best as you can. Good luck.", ""};
// Score counter
scoreCounter.setText("<html><span style='font-size:30px;'>Score: " + String.valueOf(score) + "</html>");
// Alter ego dialogue text
dialogueText = new JLabel("");
dialogueText.setText("<html><span style='font-size:20px;'>You’re...actually here? You survived. You're inside the first versions of the Neo World Program!</html>");
// Buttons
beginButton = new JButton("BEGIN THE GAME!");
continueButton = new JButton("Continue Dialogue");
// Images
alterEgoImage = ImageIO.read(new File("AlterEgo/alterego.png"));
BufferedImage alterEgoNeutral = ImageIO.read(new File("AlterEgo/neutral.jpeg"));
alterEgoImageLabel = new JLabel(new ImageIcon(alterEgoImage));
// X, Y. Width, Height
alterEgoImageLabel.setBounds(510, 150, 550, 300);
scoreCounter.setBounds(15, 15, 250, 40);
beginButton.setBounds(650, 480, 250, 40);
continueButton.setBounds(650, 480, 250, 40);
dialogueText.setBounds(500, 490, 750, 300);
// Allows for a more dynamic and fluid layout of the page where bounds are specified.
card2.setLayout(null);
// Add the components to the page
card2.add(beginButton);
card2.add(continueButton);
card2.add(dialogueText);
card2.add(scoreCounter);
card2.add(alterEgoImageLabel);
continueButton.setVisible(false);
dialogueText.setVisible(false);
// BEGIN THE QUIZ GAME
// Game begins when the button is clicked
beginButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
beginButton.setVisible(false);
continueButton.setVisible(true);
dialogueText.setVisible(true);
}
});
// Continue the opening dialogue with the continue button, and immediately begin the quiz after the dialogue has finished.
continueButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
dialogueText.setText(openingDialogue[openDialoguePos]);
openDialoguePos++;
if(openDialoguePos == openingDialogue.length) {
continueButton.setVisible(false);
dialogueText.setVisible(false);
// SET THE ALTER EGO IMAGE TO THE NEUTRAL ONE TO BEGIN THE GAME
try {
quizProcess();
} catch(IOException excep) {
// Handle the error here
}
//quizProcess();
}
}
});
} // end setupGamePanel()
/**
* Defines and places all of the content for the leaderboard page.
* #throws IOException
*/
public void setupLeaderboardPanel() throws IOException {
// Leaderboard page card constructor
card3 = new JPanel();
card3.add(new JTextField("TextField", 20));
} // end setupLeaderboardPanel()
/**
* The top level method for the quiz game.
* #throws IOException
*/
public void quizProcess() throws IOException {
// TOP LEVEL METHOD BEGINS
setupQuizUI(answerButtons);
readData(questions, answers);
quizLoop(questions, answers, answerButtons);
}
public void quizLoop(String questions[], String[] answers, JButton[] answerButtons) {
System.out.println("A quiz loop is being initiated");
moveOntoNextQuestion(questions, answers, answerButtons);
//assignData();
checkCorrectQuestion(answerButtons);
}
/**
* Initialise and setup the answer buttons and question text.
*/
public void setupQuizUI(JButton[] answerButtons) {
// Alter ego dialogue text
questionText.setText("<html><span style='font-size:20px;'<</html>");
questionText.setBounds(550, 450, 350, 150);
card2.add(questionText);
//System.out.println("Fifth read: " + currentQuestion + 25);
for(int i = 0; i < answerButtons.length; i++) {
answerButtons[i] = new JButton("Test");
}
// Bound setting (X, Y, Width, Height)
answerButtons[0].setBounds(150, 580, 200, 100);
answerButtons[1].setBounds(550, 580, 200, 100);
answerButtons[2].setBounds(950, 580, 200, 100);
answerButtons[3].setBounds(1350, 580, 200, 100);
// Adding to the JFrame
card2.add(answerButtons[0]);
card2.add(answerButtons[1]);
card2.add(answerButtons[2]);
card2.add(answerButtons[3]);
}
/**
* Read in the data from the database to the arrays.
* #param
*/
public void readData(String[] questions, String[] answers) {
//System.out.println("First read: " + currentQuestion + 5);
questions[0] = "Question 1";
questions[1] = "Question 2";
for(int i = 0; i < questions.length; i++) {
questions[i] = "Question " + (i + 1);
}
for(int i = 0; i < answers.length; i++) {
answers[i] = "Answer" + (i + 1);
}
for(int i = 0; i < correctAnswers.length; i+= 4) {
correctAnswers[i] = 0;
correctAnswers[i + 1] = 1;
correctAnswers[i + 2] = 2;
correctAnswers[i + 3] = 3;
}
/*answers[0] = "Answer 1";
answers[1] = "Answer 2";
answers[2] = "Answer 3";
answers[3] = "Answer 4";
answers[4] = "Answer 5";
answers[5] = "Answer 6";
answers[6] = "Answer 7";
answers[7] = "Answer 8";*/
}
/**
*
* Moves onto the next question and prepares the data for read-in
* Assign the next question to the text field
* Assign the next answers to the buttons
*/
public void moveOntoNextQuestion(String questions[], String[] answers, JButton[] answerButtons) {
//System.out.println("-=-=-=-=-=-=-moveOntoNextQuestion-=-=-=-=-=-=-=-");
String nextQuestion = questions[currentQuestion];
questionText.setText(questions[currentQuestion]);
//System.out.println("The current question is " + questions[currentQuestion]);
//System.out.println("The current answer number is " + currentAnswer);
answerButtons[0].setText(answers[currentAnswer]);
answerButtons[1].setText(answers[currentAnswer + 1]);
answerButtons[2].setText(answers[currentAnswer + 2]);
answerButtons[3].setText(answers[currentAnswer + 3]);
//System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
}
/**
* #param answer
* Check if the clicked answer was correct.
*/
public void checkCorrectQuestion(JButton[] answerButtons) {
int correctAnswer = correctAnswers[currentQuestion];
//System.out.println("Current correct answer is " + correctAnswer);
answerButtons[0].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(correctAnswer == 0) {
System.out.println("Button 1 is correct!");
score += 10;
updateScore(scoreCounter);
try {
correctAnswerAlterEgo(alterEgoImage, alterEgoImageLabel);
} catch(IOException excep) {
// Handle the error here
}
} else {
System.out.println("Button 1 is wrong!");
score -= 5;
updateScore(scoreCounter);
}
updateQuestionAndAnswer();
//currentQuestion += 1;
//System.out.println("currentQuestion value is " + currentQuestion);
//currentAnswer += 4;
//System.out.println("This is the quizLoop for button 1");
quizLoop(questions, answers, answerButtons);
}
});
**WITHIN HERE ARE THE OTHER THREE ACTION LISTENERS FOR THE BUTTONS. THEY ARE THE EXACT SAME EXCEPT [1], [2] and [3].**
}
public void updateQuestionAndAnswer() {
currentQuestion = currentQuestion + 1;
currentAnswer = currentAnswer + 4;
}
public void updateScore(JLabel scoreCounter) {
scoreCounter.setText("<html><span style='font-size:30px;'>Score: " + String.valueOf(score) + "</html>");
}
public void correctAnswerAlterEgo(BufferedImage alterEgoImage, JLabel alterEgoImageLabel) throws IOException {
System.out.println("Correct answer alter ego method called!");
scoreCounter.setText("<html><span style='font-size:30px;'>TEST: </html>");
// Images
BufferedImage[] happyImages = new BufferedImage[]{ ImageIO.read(new File("AlterEgo/excited.jpeg")), ImageIO.read(new File("AlterEgo/happy.jpeg")), ImageIO.read(new File("AlterEgo/shocked.jpeg"))};
alterEgoImage = ImageIO.read(new File("AlterEgo/class78.jpg"));
alterEgoImageLabel = new JLabel(new ImageIcon(alterEgoImage));
card2.add(alterEgoImageLabel);
card2.remove(alterEgoImageLabel);
}
public void wrongAnswerAlterEgo() {
}```
Thank you for your help,
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
So I am making a simple tic tac toe game and ran into a problem at the last minute
I am trying to draw a line at the win location but on the final win location(index), the line gets hidden behind the JButton not entirly sure why it is doing this.
I know alot of people say do not use getGraphics(), and I am wondering if that is the source of my issues they say to override the paintComponent method but that is not working for me either
I have attached a pic of what the result is looking like and code snips of how I am trying to perform these actions
PS I am using a JFrame, if any more code is needed I will be glad to show it
if(win[i] == 264){ // if one of the the combinations equal 'X','X','X' which equals 264, then there is a winner
System.out.println("X is the winner!!!");
System.out.println("Game Over!");
number = i;
draw(); }// call draw method
private void draw(){ // drawing a line at winning location
Graphics2D g1 = (Graphics2D) GUI.getFrame().getGraphics(); // declaring graphics on our Jframe
Stroke stroke3 = new BasicStroke(12f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER); // make our strokes cap off round
if(number == 0){ // statements will determine the win location, so at win0, XXX,
g1.setStroke(stroke3); // we will add stroke to our line
g1.drawLine(0,104,500,104); // draw the line starting at the 0,104 and end it at coordinates 500,104
}
here is a more runnable code, it is alot though
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class tic implements Runnable {
final static int row = 3; // our rows
final static int col = 3; // our col
final static int sizeOfBoard = row * col;
// the size of our board is not going to change so we make it final
static JButton[] clickButton;
char[] templateOfBoard; // our board, TicTacToe field, static
char userTurn; // users turn , only one letter, tracks whether it is a X or O
int count; // keeps track of user moves
static JFrame frame; // our JFrame
int number;
public tic(JFrame frame) {
tic.frame = new JFrame("TicTacToe GAME");
clickButton = new JButton[9];
count = 0; // number of turns starts at 0;
number = 0;
setUserTurn('X'); // first turn will always be X
setTemplateOfBoard(new char[sizeOfBoard]); // size of the board we are going to make it
try{
for(int spaces=0; spaces<sizeOfBoard; spaces++){ // size of Board is in the GUI class
getTemplateOfBoard()[spaces] = ' '; // the board is being created, looping through all rows and col
//every index of the board not has a char value equal to a space
//determine if everything came out correctly
//should equal of a total of 9
// 3x3
}
System.out.println("Board template created"); // means the board now has all spaces
}
catch(Exception e){
System.out.println("Could not initalize the board to empty char");
e.printStackTrace();
}
}
public static void main(String[] args){
try{
SwingUtilities.invokeLater(new tic(frame)); // run
}
catch(Exception e){ // wanted to test to ensure that Runnable could be invoked
System.out.println("Could not excute Runnable application");
e.printStackTrace();
}
}
public void run() {
setup(); // going to run out setup method, what our game is made out of
}
public void setup() {
// setting up the Board
// board is composed of JButton
// and a 3x3 frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when the user closes the window JFrame will exit
//going to design the board now
//the dimensations of the board = sizeOfBoard
getFrame().setLayout(new GridLayout(row, col)); // this is the outline rows * col
// sizes out row * col based on what we define those numbers as
//i.e 3x3
getFrame().setBounds(0,0,500,500); // location at 0,0, size 500 x 500
Border border = new LineBorder(Color.DARK_GRAY, 2); // color of JButton border
System.out.println("Your board game is being created!");
try{
getFrame().setVisible(true); // shows the board,
// this is going to display everything to the screen
System.out.println("Board is now visable");
}
catch(Exception e){
System.out.println("Board was not displayed");
}
// 9 different buttons, for every index there will be a button
for(int i =0; i<sizeOfBoard;i++){ // going to fill the board with clickableButtons by looping through every index and placing a button there
final int move = i;
clickButton[i] = new JButton(); // at a certain index there is a new button
clickButton[i].setSize(250,250); // size of each button
clickButton[i].setBackground(Color.WHITE); // color of the JButton
getFrame().add(clickButton[i]); // we are going to add the actual the button at that index on the frame
clickButton[i].setFont(new Font("Arial", Font.BOLD, 70)); // size of the text
clickButton[i].setBorder(border); // adding border
clickButton[i].getModel().addChangeListener(new ChangeListener() { //going to overRide what happens when we rollover and press a Butotn
public void stateChanged(ChangeEvent e) {
ButtonModel button = (ButtonModel) e.getSource(); // manages the state of the button, i.e lets me control what happens to the button
if(clickButton[move] != null){ // if we do not include this argument
// the buttons are not made yet on the new game, meaning clickButton[i] = null
//so boolean(!button.isRollover()) will return true, since on the new game you can not have your mouse hovered over
// but when it returns true, it will return a null value, giving a null pointer exception
// so best thing to do, is to only run these cases below when the buttons are not null
if (button.isRollover()) { // when the mouse hovers over the index
clickButton[move].setBackground(Color.BLACK); // color will equal black
}
else if(!button.isRollover()){ // when the button is not hovered over
clickButton[move].setBackground(Color.WHITE); // color will be whte, just like our background
}
}
}
});
clickButton[i].addActionListener(new ActionListener() {
//our click events, going to override to let it know what we want to happen
//once we click on the button
public void actionPerformed(ActionEvent e) {
clickButton[move].setEnabled(false); //going to disable the button after it is clicked
//ORDER: button gets clicked first, then the test is added
mouseListener(e, move); // our mouseListenerEvent in game class
//
}
});
}
}
public static void playAgain() {
try{
System.out.println("NEW GAME");
SwingUtilities.invokeLater(new tic(frame)); // run the run(class) again
}
catch(Exception e){ // wanted to test to ensure that Runnable could be invoked
System.out.println("Could not excute Runnable application");
e.printStackTrace();
}
}
public static JFrame getFrame() {
return frame;
}
public tic userMove(int moveMade){
getTemplateOfBoard()[moveMade] = getUserTurn();
// index of the board, or in simpler terms, where the user
// inserts there turn i.e X or O, 0-8
//System.out.println(userMove);
//boolean statement to determine the turns
// So user X starts first
//if the turn is X, the nextTurn is now O,
if(getUserTurn() == 'X'){
setUserTurn('O');
}
else {
setUserTurn('X');
}
count++;
return this; // going to return the userTurn
// issue actually entering the userTurn is not giving right value, but using 'this' does
}
// for some odd reason the toString is causing some issues, keep getting #hash code
//saw online to override it like this
// will make the board out of emepty strings
// going to return a string representation of an object
public String toString(){
return new String(getTemplateOfBoard());
}
public void mouseListener(ActionEvent e, int moveMade){
// mouse click events
// what happens after a button is clicked
if(getTemplateOfBoard()[moveMade] == ' '){ // the user can only space a click, so an letter on the field if it is empty
((JButton)e.getSource()).setText(Character.toString(getUserTurn())); // when the button is clicked, we want an X placed there
if (getUserTurn() == 'X'){
UIManager.getDefaults().put("Button.disabledText",Color.RED); // when the but gets disabled the test will turn red
}
else{
UIManager.getDefaults().put("Button.disabledText",Color.BLUE);
}
//calling the method userTurn to determine who goes next
//problem is that is expects a String
//going to override the toString method
userMove(moveMade); // calling userMove in moveMade, moveMade is the index at which the user put either an X or a O
winner(); // we want to check each time to ensure there was/was not a winner
}
}
public tic winner() { // determines who is the winner
//list below defines all the possible win combinations
// the index of where a X or O can be place
// placed the locations to a int value
int win1 = templateOfBoard[0] + templateOfBoard[1] + templateOfBoard[2];
int win2 = templateOfBoard[3] + templateOfBoard[4] + templateOfBoard[5];
int win3 = templateOfBoard[6] + templateOfBoard[7] + templateOfBoard[8];
int win4 = templateOfBoard[0] + templateOfBoard[3] + templateOfBoard[6];
int win5 = templateOfBoard[1] + templateOfBoard[4] + templateOfBoard[7];
int win6 = templateOfBoard[2] + templateOfBoard[5] + templateOfBoard[8];
int win7 = templateOfBoard[0] + templateOfBoard[4] + templateOfBoard[8];
int win8 = templateOfBoard[2] + templateOfBoard[4] + templateOfBoard[6];
int[] win = new int[]{win1,win2,win3,win4,win5,win6,win7,win8};
// making a array to go through all the possibile wins
//possible total of wins is 8
for(int i = 0;i<win.length;i++){
// looping through the win possibilities
if(win[i] == 264){ // if one of the the combinations equal 'X','X','X' which equals 264, then there is a winner
System.out.println("X is the winner!!!");
System.out.println("Game Over!");
number = i;
draw(); // call draw method
return this; // if statement is true, it will return this(gameOver)
}
else if(win[i] == 237 ){ // if one of the the combinations equal 'O','O','O' which equals 234, then there is a winner
System.out.println("O is the winner!!!");
System.out.println("Game Over!");
number = i;
//draw(); // call draw method
return this;
}
if (count == 9) {
// if none of the statements above are true, it automatically comes done to here
//so if there is nine moves and no win, it is a draw
}
}
return this;
// going to return this method ;
}
private void draw(){ // drawing a line at winning location
Graphics2D g1 = (Graphics2D) getFrame().getGraphics(); // declaring graphics on our Jframe
Stroke stroke3 = new BasicStroke(12f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER); // make our strokes cap off round
if(number == 0){ // statements will determine the win location, so at win0, XXX,
g1.setStroke(stroke3); // we will add stroke to our line
g1.drawLine(0,104,500,104); // draw the line starting at the 0,104 and end it at coordinates 500,104
}
else if(number == 1){
g1.setStroke(stroke3);
g1.drawLine(0,257,500,257);
}
else if(number == 2){
g1.setStroke(stroke3);
g1.drawLine(0,411,500,411);
}
else if(number == 3){
g1.setStroke(stroke3);
g1.drawLine(88,0,88,500);
}
else if(number == 4){
g1.setStroke(stroke3);
g1.drawLine(250,0,250,500);
}
else if(number == 5){
g1.setStroke(stroke3);
g1.drawLine(411,0,411,500);
}
else if(number == 6){
g1.setStroke(stroke3);
g1.drawLine(-22,0,500,500);
}
else if(number == 7){
g1.setStroke(stroke3);
g1.drawLine(520,0,0,500);
}
}
// want to be able to access the private variables
//so we will make getter and setter methods for the ones that we need
public char getUserTurn() { // getter method for userTurn
return userTurn;
}
public void setUserTurn(char userTurn) { // setter method
this.userTurn = userTurn;
}
public char[] getTemplateOfBoard() { //getter method
return templateOfBoard;
}
public void setTemplateOfBoard(char[] templateOfBoard) { // setter method
this.templateOfBoard = templateOfBoard;
}
}
Painting over the top of components can be troublesome, you can't override the paintComponent method of the container which contains the components, because this paints in the background, you can't override the paint method of the container, as child components can be painted without the parent container been notified...
You could add a transparent component over the whole lot, but this just introduces more complexity, especially when a component already already exists ...
public class ConnectTheDots {
public static void main(String[] args) {
new ConnectTheDots();
}
public ConnectTheDots() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
PaintPane pp = new PaintPane();
JFrame frame = new JFrame("Test");
frame.setGlassPane(pp);
pp.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DotsPane(pp));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PaintPane extends JPanel {
private List<JButton[]> connections;
private JButton lastSelected;
public PaintPane() {
setOpaque(false);
connections = new ArrayList<>(25);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (lastSelected != null) {
g2d.setColor(Color.RED);
int x = lastSelected.getX() + ((lastSelected.getWidth() - 8) / 2);
int y = lastSelected.getY() + ((lastSelected.getHeight() - 8) / 2);
g2d.fillOval(x, y, 8, 8);
}
for (JButton[] group : connections) {
g2d.setColor(Color.BLUE);
Point startPoint = group[0].getLocation();
Point endPoint = group[1].getLocation();
startPoint.x += (group[0].getWidth() / 2);
startPoint.y += (group[1].getHeight()/ 2);
endPoint.x += (group[0].getWidth() / 2);
endPoint.y += (group[1].getHeight()/ 2);
g2d.draw(new Line2D.Float(startPoint, endPoint));
}
g2d.dispose();
}
protected void buttonClicked(JButton btn) {
if (lastSelected == null) {
lastSelected = btn;
} else {
connections.add(new JButton[]{lastSelected, btn});
lastSelected = null;
}
revalidate();
repaint();
}
}
public class DotsPane extends JPanel {
private PaintPane paintPane;
public DotsPane(final PaintPane pp) {
paintPane = pp;
ActionListener al = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
paintPane.buttonClicked(btn);
}
};
setLayout(new GridLayout(6, 6));
for (int index = 0; index < 6 * 6; index++) {
JButton btn = new JButton(".");
add(btn);
btn.addActionListener(al);
}
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
Take a look at How to Use Root Panes for more details
package combatframe;
import javax.swing.*;
import java.awt.event.*;
import static java.lang.Math.PI;
import javax.swing.border.*;
public class CombatFrame extends JFrame
{
String[] Ships = {"Battleship", "Cruiser", "Frigate"};
JComboBox attackCombo = new JComboBox(Ships);
JComboBox targetCombo = new JComboBox(Ships);
JButton calculate = new JButton();
JPanel mainPanel = new JPanel();
JPanel outPanel = new JPanel();
JPanel shipPanel = new JPanel();
JTextArea outText = new JTextArea("Results: ", 11, 30);
JTextArea attackStats = new JTextArea("Attacker stats.", 10,10);
JTextArea targetStats = new JTextArea("Target stats.", 10,10);
public static void main(String[] args)
{
CombatFrame combatFrame = new CombatFrame();
}
public CombatFrame()
{
this.setSize(500,500);
this.setTitle("OTG Combat Simulator");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
EventListener c1 = new EventListener();
EventListener c2 = new EventListener();
EventListener calc = new EventListener();
calculate.setText("Calculate!");
Border b1 = BorderFactory.createTitledBorder("Result");
outPanel.setBorder(b1);
Border b2 = BorderFactory.createTitledBorder("Ship Classes");
shipPanel.setBorder(b2);
mainPanel.add(shipPanel);
mainPanel.add(outPanel);
shipPanel.add(attackStats);
shipPanel.add(attackCombo);
shipPanel.add(targetCombo);
shipPanel.add(targetStats);
outPanel.add(calculate);
outPanel.add(outText);
attackCombo.addActionListener(c1);
attackCombo.setSelectedItem("Battleship");
targetCombo.addActionListener(c2);
targetCombo.setSelectedItem("Battleship");
calculate.addActionListener(calc);
this.add(mainPanel);
this.setVisible(true);
}
public double hitProbability(double factor)
{
double hitCount = (double)(Math.random() * 1.001 * factor );
return hitCount;
}
public double Combat(double[] turnArray)
{
double rRange = turnArray[0];
double rAngular = turnArray[1];
double rRadius = turnArray[2];
double damage = turnArray[3];
double appliedDamage = 0;
int[] attHit =
{
0, 0, 0
};
int[] strikes =
{
0, 0, 0, 0, 0
};
double[] damageMod =
{
0,0.4,0.75,1.0,1.25
};
double roll1, roll2, roll3;
roll1 = hitProbability(rRadius);
roll2 = hitProbability(rAngular);
roll3 = hitProbability(rRange);
if (roll1 <=1)
attHit[0]++;
if (roll2 <=1)
attHit[1]++;
if (roll3 <=1)
attHit[2]++;
switch (attHit[0]+attHit[1]+attHit[2])
{
case 3:
double wrecker = Math.random();
if (wrecker >= 0.95 - Math.random())
strikes[4]++;
else
strikes[3]++;
break;
case 2:
strikes[2]++;
break;
case 1:
strikes[1]++;
break;
case 0:
strikes[0]++;
break;
}
for (int x=0; x<5; x++)
{
appliedDamage += strikes[x]*Damage*
(Math.random()+Math.random())*damageMod[x];
}
return appliedDamage;
}
private class EventListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource()== attackCombo)
{
switch ((String)attackCombo.getSelectedItem())
{
case "Battleship":
Attacker attackBS = new Attacker(50000.0, 400.0, 0.01, 45000.0, 400.0,
300.0, 10.0, 10000.0, 250.0);
break;
case "Cruiser":
Attacker attackCr = new Attacker(25000.0, 125.0, 0.23, 22500.0, 150.0,
600.0, 5.0, 5000.0, 75.0);
break;
case "Frigate":
Attacker attackFr = new Attacker(2500.0, 40.0, 0.365, 1000.0, 39.0,
900.0, 2.0, 1200.0, 25.0);
break;
}
attackStats.setText("Optimal: " + Attacker.Optimal
+ "\n Weapon Sig: " +Attacker.Attack_Signature
+ "\n Tracking: " + Attacker.Tracking
+ "\n Range: " + Attacker.Range
+ "\n Ship Sig: " + Attacker.Target_Signature
+ "\n Velocity: " + Attacker.Orbital
+ "\n Cycle Time" + Attacker.Period
+ "\n Health: " + Attacker.Health
+ "\n Damage: " + Attacker.Damage);
}
if (e.getSource()==targetCombo)
{
switch ((String)targetCombo.getSelectedItem())
{
case "Battleship":
Target targetBS = new Target(50000.0, 400.0, 0.01, 45000.0, 400.0,
300.0, 10.0, 10000.0, 250.0);
break;
case "Cruiser":
Target targetCr = new Target(25000.0, 125.0, 0.23, 22500.0, 150.0,
600.0, 5.0, 5000.0, 75.0);
break;
case "Frigate":
Target targetFr = new Target(2500.0, 40.0, 0.365, 1000.0, 39.0,
900.0, 2.0, 1200.0, 25.0);
break;
}
targetStats.setText("Optimal: " + Target.Optimal
+ "\n Weapon Sig: " + Target.Attack_Signature
+ "\n Tracking: " + Target.Tracking
+ "\n Range: " + Target.Range
+ "\n Ship Sig: " + Target.Target_Signature
+ "\n Velocity: " + Target.Orbital
+ "\n Cycle Time" + Target.Period
+ "\n Health: " + Target.Health
+ "\n Damage: " + Target.Damage);
}
if (e.getSource()==calculate)
{
double[] attackArray =
{//RRange,RAngular,RRadius,Attacker.Damage
0,0,0,0
};
double[] targetArray =
{//RRange,RAngular,RRadius,Target.Damage
0,0,0,0
};
double attackDamage = 0, targetDamage = 0;
for (int i=1; i<1000; i++)
{
if ((i+1)%Attacker.Period==0)
{
double rRange = Attacker.Optimal/Target.Range;
double rAngular =
((Math.sin(PI/4)*(Target.Orbital/Target.Range))
/Attacker.Tracking);
double rRadius = Attacker.Attack_Signature/
Target.Target_Signature;
attackArray[0] = rRange;
attackArray[1] = rAngular;
attackArray[2] = rRadius;
attackArray[3] = Attacker.Damage;
attackDamage += Combat(attackArray);
if (attackDamage >= Target.Health)
{
outText.setText("Attacker wins!");
break;
}
}
if (i%Target.Period==0)
{
double rRange = Target.Optimal/Attacker.Range;
double rAngular =
((Math.sin(PI/4)*(Attacker.Orbital/Attacker.Range))
/Target.Tracking);
double rRadius = Target.Attack_Signature/
Attacker.Target_Signature;
targetArray[0] = rRange;
targetArray[1] = rAngular;
targetArray[2] = rRadius;
targetArray[3] = Target.Damage;
targetDamage += Combat(targetArray);
if (targetDamage >= Attacker.Health)
{
outText.setText("Target wins!");
break;
}
}
}
}
}
}
}
I'm attempting to build a very simple combat simulation frame. It builds 2 combo-boxes and assigns stats based upon the selections therein.
User then hits the calculate button, and out pops the result into the Results field. That all works fine.
I'd like the GUI to append each 'turn' of combat into the Results textfield outText - and I think I could do that easily enough. I'd like it to do so with a slight delay on any turn there is combat - I'd use a Thread.sleep(10) in each if-statement to do with an active turn for each ship.
The only problem with all that is I can't work out how to hold the GUI on one thread - which updates when prompted by the combat calculations after the button is clicked - and the thread handling all the calculations with delays.
I know if I try and run both on the same thread, the GUI will simply freeze up, do all the calculations with applicable delays, then throw out the entire appended lump at once into the results field.
Can anyone offer me any pointers, advice or cyanide? Anything, anything to make the pain go away...
Suspect that SwingWorker would be a good idea, but still can't work out how to actually implement that within my code.
Clarification: My biggest concern is that I can't work out how to implement SwingWorker, or some similar multi-thread process, in order to run the GUI and background calculations in parallel.
I will just take a stab at this since I think I understand what you are trying to do. Maybe what you are looking for is to create a new runnable each time the button is clicked:
class Turn implements Runnable {
private final Object source;
private final String attackComboMove;
private final String targetComboMove;
public Turn(Object src, String acm, String tcm) {
source = src;
attackComboMove = acm;
targetComboMove = tcm;
}
#Override public void run() {
// may want to disable GUI buttons here
if (source == attackCombo) {
switch (attackComboMove) {
// ...
// ...
// ...
}
append("attack combo results");
} else if (source == targetCombo) {
switch (targetComboMove) {
// ...
// ...
// ...
}
append("target combo results");
} else if (source == calculate) {
// ...
for (int i = 1; i < 1000; i++) {
// ...
attackDamage += combat(attackArray);
append("combat results: " + attackDamage + " total damage");
if (attackDamage >= target.health) {
append("Attacker wins!");
break;
}
}
}
// and enable the buttons again when combat is over
}
private void append(String text) {
SwingUtilities.invokeLater(new Runnable() {
#Override public void run() {
outTextArea.append("\n" + text);
}
});
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
}
}
It sounds like you just want a scrolling list of the attacks as they happen and that's what this will do for you. IMO it is the right way to do it and ultimately the simplest.
When the user clicks the button you do:
new Thread(new Turn(e.getSource(), (String)attackCombo.getSelectedItem(), (String)targetCombo.getSelectedItem())).start();
(Or make a variable for the new thread if you need to be able to end it early for some reason or alter parameters.)
This pattern can easily be adapted to SwingWorker if it is long-running, just extend SwingWorker and do the same thing but override doInBackground() instead of run().
Otherwise if you are intending to make a real-time simulation and need a background thread you will need to make run/doInBackground be a while(running) loop and use flags and/or some kind of wait/notify scheme when you need to change parameters or calculate a turn.
You could use javax.swing.Timer to schedule something to happen N milliseconds in the future. Note that 10 in Java time usually means 10 milliseconds, which is a nearly imperceptible amount of time. If you want to sleep or delay for a second, use 1000 milliseconds, 10 seconds = 10000 milliseconds, etc. Example:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class GameFrame extends JFrame
{
private JPanel _contentPane;
private GameFrame _mainFrame;
private JButton _jButton;
private JTextArea _jTextArea;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
GameFrame frame = new GameFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(640/2, 480/2);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public GameFrame()
{
_mainFrame = this;
_contentPane = new JPanel();
_mainFrame.setContentPane(_contentPane);
_contentPane.setLayout(new BorderLayout());
_jTextArea = new JTextArea();
_contentPane.add(_jTextArea, BorderLayout.CENTER);
_jButton = new JButton("Go");
_jButton.addActionListener(new GoButtonListener());
_contentPane.add(_jButton, BorderLayout.SOUTH);
}
class GoButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
_jTextArea.append("Please wait...\n");
Timer timer = new Timer(10000, new LaterListener());
timer.start();
}
}
class LaterListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
_jTextArea.append("Ten seconds later!\n");
}
}
}
I am trying to develop a hex editor in which the panel for editor is provided with key listeners and the input through keyboard is converted into corresponding input. The problem however is regarding the saving the coverted input.only one key stroke value is converted at a time as for now. I would like to use this converted input() in a string as a bunch and use it for other purposes. I tried to save converted text in a byte array but it is returning the first value I typed irrespective of what the input afterwards is.
package gui.hex;
import javax.swing.*;
import cryptool.Test;
import java.awt.*;
import java.awt.event.*;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
public class JHexEditorASCII extends JComponent implements MouseListener,
KeyListener {
private static final long serialVersionUID = 5636121664420538046L;
private JHexEditor he;
private static CharsetEncoder encoder = Charset.forName("UTF-8")
.newEncoder();
public static String modifiedText;
public static byte temp[];
public static byte[] getTemp() {
return temp;
}
public void setTemp(byte[] temp) {
this.temp = temp;
}
protected JHexEditorASCII(JHexEditor he) {
this.he = he;
addMouseListener(this);
addKeyListener(this);
addFocusListener(he);
}
public Dimension getPreferredSize() {
debug("getPreferredSize()");
return getMinimumSize();
}
public Dimension getMinimumSize() {
debug("getMinimumSize()");
Dimension d = new Dimension();
FontMetrics fn = getFontMetrics(JHexEditor.font);
int h = fn.getHeight();
int nl = he.getLines();
d.setSize((fn.stringWidth(" ") + 1) * (16) + (he.border * 2) + 1, h
* nl + (he.border * 2) + 1);
return d;
}
public void paint(Graphics g) {
debug("paint(" + g + ")");
debug("cursor=" + he.cursor + " buff.length=" + he.buff.length);
Dimension d = getMinimumSize();
// sets the color to the background.
g.setColor(Color.orange);
g.fillRect(0, 0, d.width, d.height);
g.setColor(Color.black);
g.setFont(JHexEditor.font);
// ascii data
int ini = he.getInitial() * 16;
int fin = ini + (he.getLines() * 16);
if (fin > he.buff.length)
fin = he.buff.length;
int x = 0;
int y = 0;
for (int n = ini; n < fin; n++) {
if (n == he.cursor) {
// sets color for the focus cursor it used to be in blue color.
g.setColor(Color.red);
if (hasFocus())
he.background(g, x, y, 1);
else
he.table(g, x, y, 1);
if (hasFocus())
g.setColor(Color.white);
else
g.setColor(Color.black);
} else {
g.setColor(Color.black);
}
// builds a character object
String s;
if (encoder.isLegalReplacement(new byte[] { he.buff[n] }))
s = "" + new Character((char) he.buff[n]);
else
s = ".";
// compares the input supplied
// if ((he.buff[n] < 20) || (he.buff[n] > 126))
// converts the input entered into string
// s = ".";
// displaying of the text goes here.
// displays the input
System.out.println("the typed in string is:" + s);
temp = s.getBytes();
setTemp(temp);
setModifiedText(s);
he.printString(g, s, (x++), y);
if (x == 16) {
x = 0;
y++;
}
}
}
private void debug(String s) {
if (he.DEBUG)
System.out.println("JHexEditorASCII ==> " + s);
}
public void dispaly(Graphics g) {
int x = 0;
int y = 0;
he.printString(g, Test.getK(), (x++), y);
if (x == 16) {
x = 0;
y++;
}
}
// calculate the position of the mouse
public int calculateMousePosition(int x, int y) {
FontMetrics fn = getFontMetrics(JHexEditor.font);
x = x / (fn.stringWidth(" ") + 1);
y = y / fn.getHeight();
debug("x=" + x + " ,y=" + y);
return x + ((y + he.getInitial()) * 16);
}
// mouselistener
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
// double clicked
he.setNewTextListener.actionPerformed(new ActionEvent(this, 0,
"NEWASCII"));
}
debug("mouseClicked(" + e + ")");
he.cursor = calculateMousePosition(e.getX(), e.getY());
this.requestFocus();
he.repaint();
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
// KeyListener
public void keyTyped(KeyEvent e) {
debug("keyTyped(" + e + ")");
// only add content, if buffer is large enough
if (he.buff.length > he.cursor) {
he.buff[he.cursor] = (byte) e.getKeyChar();
if (he.cursor != (he.buff.length - 1))
he.cursor++;
// change this
// System.out.println( he.buff.toString());
he.repaint();
}
}
public void keyPressed(KeyEvent e) {
debug("keyPressed(" + e + ")");
he.keyPressed(e);
}
public void keyReleased(KeyEvent e) {
debug("keyReleased(" + e + ")");
}
public boolean isFocusTraversable() {
return true;
}
public static String getModifiedText() {
return modifiedText;
}
public void setModifiedText(String modifiedText) {
this.modifiedText = modifiedText;
}
}
develop a hex editor (encoder.isLegalReplacement...)
use JTextField with DocumentFilter, or easiest could be JFormatterTextField instead of paint chars by using paint() to JComponent
use paintComponent (instead of paint()) for Swing JComponents
add DocumentListener to the JTextField with DocumentFilter or JFormatterTextField
interesting way could be to built / prepare / create a ArrayList of hex () and to use AutoComplete JTextField
I am using NETBEANS and have several JLabels that need to display a specific icon/image based on a decision/boolean. I do not want to have to add a mouse listener for every JLabel and then copy and paste the code for each one. Rather I would prefer to use the x,y as the name of the JLabel and then set icon based on the x,y. I have no problems getting the x.y but cannot seem to figure out how to do something like this (xy.setIcon(new ImageIcon(Hit)); here is my code.
/**
* Mathematical coordinates of player1Fleet.
* Used to realign ships from Ship Class
* for use on game board.
*/
public void mouseClicked(MouseEvent e) {
Launch1();
}
public void FleetP1() {
for (Ship s : player1Fleet) {
int size = s.getSize();
for (int i = 0; i < size; i++) {
player1Ships.add((((s.getXCoordinate(i) + 1) * 45) + 90) + "" + (((s.getYCoordinate(i) + 1) * 45) + 180));
}
}
// Verification of Math
System.out.println(player1Ships);
}
/**
* Determine Hit or Miss based on location of Cross-hairs
* for player 1/West on game board.
* #return
*/
//public boolean setStrike1(){
public boolean Launch1() {
w93.setIcon(null);
player1Ships.clear();
FleetP1();
boolean strike1 = false;
boolean Launch = false;
for (int i = 0; i < this.player1Ships.size(); i++) {
if (this.player1Ships.get(i).equals(LblCrossHairs.getX() + "" + LblCrossHairs.getY())) {
strike1 = true;
//break;
}
if (strike1) {
strike1 = true;
(LblCrossHairs.getX() + "" + LblCrossHairs.getY()).setIcon(new ImageIcon(Hit));
//Launch = theAttack.Strike1(strike1);
//w93.setIcon(new ImageIcon(Hit));
URL url = this.getClass().getResource("MissleHit.au");
AudioClip ac = Applet.newAudioClip(url);
ac.play();
System.out.println("HIT");
break;
} else {
strike1 = false;
(LblCrossHairs.getX() + "" + LblCrossHairs.getY()).setIcon(new ImageIcon(Hit));
//w93.setIcon(new ImageIcon(Miss));
URL url = this.getClass().getResource("MissileMiss.au");
AudioClip ac = Applet.newAudioClip(url);
ac.play();
System.out.println("MISS");
}
}
TxtClick.setText(LblCrossHairs.getX() + "" + LblCrossHairs.getY() + ".setIcon");
return strike1;
}
Thank you in advance for all your help. This has been driving me crazy for the last two days
I do not want to have to add a mouse listener for every JLabel and then copy and paste the code for each one.
You don't have to create a separate listener. You can share the same listener with every JLabel. The basic code is:
public void mouseClicked(MouseEvent e)
{
JLabel label = (JLabel)e.getSource();
label.setIcon(...);
}