IF Else statement in java gui using eventHandler - java

I am trying to replicate this code that I did in the console in GUI.
if (age <18 && feelings.toUpperCase().equals("SAD")){
System.out.println(phrase.sadQuotesBefore18[rand.nextInt(2)]);
} else if (age >= 18 && feelings.toUpperCase().equals("SAD") ) {
System.out.println(phrase.sadQuotesAfter18[rand.nextInt(3)]);
} else if (age <18 && feelings.toUpperCase().equals("ANGRY")){
System.out.println(phrase.angryQuotesBefore18[rand.nextInt(2)]);
} else if (age >= 18 && feelings.toUpperCase().equals("ANGRY")){
System.out.println(phrase.angryQuotesAfter18[rand.nextInt(8)]);
} else if (age < 18 && feelings.toUpperCase().equals("HAPPY")){
System.out.println(phrase.happyQuotesBefore18[rand.nextInt(2)]);
} else if (age >= 18 && feelings.toUpperCase().equals("HAPPY")){
System.out.println(phrase.happyQuotesAfter18[rand.nextInt(4)]);
} else {
System.out.println("You have entered and unknown combination, please try again");
}
As shown below. I realise in the code below I cannot get the else if and else statements as shown above to work. Only the if statement as shown below works. I am new to this so any assistance would be appreciated.
submitButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
if (agebox.getText().equals("<=18") && feelings.equals("Sad")) {
}
showAlert(AlertType.CONFIRMATION, gridPane.getScene().getWindow(), "Your Quote!", phrase.sadQuotesBefore18[rand.nextInt(3)]);
}
}

not sure what is your problem, but at least you can categorize your if statements:
submitButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
String quoteStr="";
if (agebox.getText().equals("<18")) {
if (feelings.equals("SAD")) {
quoteStr = phrase.sadQuotesBefore18[rand.nextInt(3)];
} else if (feelings.equals("ANGRY")) {
quoteStr = phrase.angryQuotesBefore18[rand.nextInt(2)];
} else if (feelings.equals("HAPPY")) {
quoteStr = phrase.angryQuotesBefore18[rand.nextInt(2)];
}
}
else if (agebox.getText().equals(">=18")) {
if (feelings.equals("SAD")) {
quoteStr = phrase.sadQuotesAfter18[rand.nextInt(3)];
} else if (feelings.equals("ANGRY")) {
quoteStr = phrase.angryQuotesAfter18[rand.nextInt(8)];
} else if (feelings.equals("HAPPY")) {
quoteStr = phrase.angryQuotesAfter18[rand.nextInt(4)];
}
}
showAlert(AlertType.CONFIRMATION, gridPane.getScene().getWindow(), "Your Quote!", quoteStr);
}
}

Related

Java graphics slow down after adding objects

I am still new to java and I've been trying to make a SpaceInvaders copy in Java's native graphics. I've finished the basic mechanics, however the game slows down significantly when the player or aliens shoot a bullet. I have tried to find out what was causing the game to slow down, but was unable to.
public class Board extends JPanel implements KeyListener,ActionListener{
Ship ship;
Alien[] row1 = new Alien[10];
Alien[] row2 = new Alien[10];
Alien[] row3 = new Alien[10];
private Timer timer;
int DELAY = 16;
boolean reachedend = false;
Bullet bullet;
int lives;
public Board() {
setBackground(Color.BLACK);
addKeyListener(this);
lives = 3;
ship = new Ship();
fillAliens();
timer = new Timer(DELAY, this);
timer.start();
}
void fillAliens() {
for(int x=1;x<11;x++) {
row1[x-1] = new Alien(x*30+40,30);
row2[x-1] = new Alien(x*30+40,70);
row3[x-1] = new Alien(x*30+40,110);
}
}
void shoot() {
if(bullet==null) {
bullet = new Bullet(ship.x + ship.width/2,ship.y - ship.height/2,-1);
}
}
void drawAliens(Graphics2D g) {
g.setColor(Color.red);
for(int x=1;x<11;x++) {
if(row1[x-1].alive == true) {
g.fillRect(row1[x-1].x_loc, row1[x-1].y_loc, row1[x-1].width, row1[x-1].width);
}
if(row2[x-1].alive == true) {
g.fillRect(row2[x-1].x_loc, row2[x-1].y_loc, row2[x-1].width, row2[x-1].width);
}
if(row3[x-1].alive == true) {
g.fillRect(row3[x-1].x_loc, row3[x-1].y_loc, row3[x-1].width, row2[x-1].width);
}
}
}
public void addNotify() {
super.addNotify();
requestFocus();
}
void drawShip(Graphics2D g) {
g.fillRect(ship.x, ship.y, ship.width, ship.height);
}
#Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(lives>0) {
if (key == KeyEvent.VK_SPACE) {
shoot();
}
if (key == KeyEvent.VK_LEFT) {
if(ship.x>=5) {
ship.x -= 5;
}
}
if (key == KeyEvent.VK_RIGHT) {
if(ship.x+ship.width<434) {
ship.x += 5;
}
}
}
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
drawShip(g2);
drawAliens(g2);
drawBullet(g2);
drawAlienBullet(g2);
drawLives(g2);
}
private void drawLives(Graphics2D g2) {
g2.setColor(Color.white);
g2.drawString("Lives:", 10, 300);
for(int x=1; x<=lives;x++) {
g2.fillRect(15*x+30, 290, 10, 10);
}
}
private void drawBullet(Graphics2D g2) {
g2.setColor(Color.GREEN);
if(bullet!=null) {
g2.fillRect(bullet.x_loc, bullet.y_loc, bullet.width, bullet.height);
}
}
private void drawAlienBullet(Graphics2D g2) {
g2.setColor(Color.BLUE);
for(int x=0;x<10;x++) {
if(row3[x].bullet != null) {
g2.fillRect(row3[x].bullet.x_loc, row3[x].bullet.y_loc, row3[x].bullet.width, row3[x].bullet.height);
}
if(row2[x].bullet != null) {
g2.fillRect(row2[x].bullet.x_loc, row2[x].bullet.y_loc, row2[x].bullet.width, row2[x].bullet.height);
}
if(row1[x].bullet != null ) {
g2.fillRect(row1[x].bullet.x_loc, row1[x].bullet.y_loc, row1[x].bullet.width, row1[x].bullet.height);
}
}
}
private void bulletHitPlayer() {
for(int x=0;x<10;x++) {
if(row3[x].bullet != null) {
if(row3[x].bullet.y_loc >= 350) {
row3[x].bullet = null;
break;
}
}
if(row2[x].bullet != null) {
if(row2[x].bullet.y_loc >= 350) {
row2[x].bullet = null;
break;
}
}
if(row1[x].bullet != null) {
if(row1[x].bullet.y_loc >= 350) {
row1[x].bullet = null;
break;
}
}
if(row3[x].bullet != null && row3[x].bullet.y_loc + 10 >= ship.y && row3[x].bullet.y_loc <= ship.y + ship.height && row3[x].bullet.x_loc >= ship.x && row3[x].bullet.x_loc <= ship.x+ship.width) {
row3[x].bullet = null;
lives--;
break;
}
if(row2[x].bullet != null && row2[x].bullet.y_loc + 10 >= ship.y && row2[x].bullet.y_loc <= ship.y + ship.height && row2[x].bullet.x_loc >= ship.x && row2[x].bullet.x_loc <= ship.x+ship.width) {
row2[x].bullet = null;
lives--;
break;
}
if(row1[x].bullet != null && row1[x].bullet.y_loc + 10 >= ship.y && row1[x].bullet.y_loc <= ship.y + ship.height && row1[x].bullet.x_loc >= ship.x && row1[x].bullet.x_loc <= ship.x+ship.width) {
row1[x].bullet = null;
lives--;
break;
}
}
}
private void bulletHit() {
if(bullet!=null) {
for(int x=0;x<10;x++) {
if(row3[x].alive == true && bullet.y_loc >= row3[x].y_loc && bullet.y_loc <= row3[x].y_loc+row3[x].width && bullet.x_loc >= row3[x].x_loc && bullet.x_loc <= row3[x].x_loc+row3[x].width) {
row3[x].alive = false;
bullet=null;
break;
}
if(row2[x].alive == true && bullet.y_loc >= row2[x].y_loc && bullet.y_loc <= row2[x].y_loc+row2[x].width && bullet.x_loc >= row2[x].x_loc && bullet.x_loc <= row2[x].x_loc+row2[x].width) {
row2[x].alive = false;
bullet=null;
break;
}
if(row1[x].alive == true && bullet.y_loc >= row1[x].y_loc && bullet.y_loc <= row1[x].y_loc+row1[x].width && bullet.x_loc >= row1[x].x_loc && bullet.x_loc <= row1[x].x_loc+row1[x].width) {
row1[x].alive = false;
bullet=null;
break;
}
}
}
if(bullet!=null) {
if(bullet.y_loc<0) {
bullet=null;
}
}
}
#Override
public void keyTyped(KeyEvent e) {}
#Override
public void keyReleased(KeyEvent e) {}
#Override
public void actionPerformed(ActionEvent e) {
bulletHitPlayer();
bulletHit();
repaint();
}
}
Problem:
You draw your scene, whenever you press a key; and you draw your scene once on startup (see API and Tutorials) - But you should continuously redraw your scene by using the Timer properly.
How to solve:
When you create your Timer let it
either repeat itself timer.setRepeats(true);
or restart the Timer manually:
public void actionPerformed(ActionEvent e) {
bulletHitPlayer();
bulletHit();
repaint();
timer.restart(); //manually restart the timer
}

Checkers 2D array - Checkers not moving

I'm programming Checkers and having an issue where my checkers aren't moving. I'm using a Java Applet. It registers the fact that I click, but it's not moving the checker itself.
Here's my MouseClicked, I think that's where I'm having the problem:
public void mouseClicked(MouseEvent e) {
mouseX=e.getX();
mouseY=e.getY();
col = (mouseX-115)/100;
row = (mouseY-115)/100;
if(board[row][col].getPlayer()==player)
{ oldCol = col;
oldRow = row;
if(board[row][col].getPlayer()==0)
{
if(board[oldRow][oldCol].getPlayer()==1 && board[oldRow][oldCol].getKing()==false)
{
if(row==oldRow-1)
{
if(col==oldCol-1 || col==oldCol+1)
{
board[oldRow][oldCol].setPlayer(0);
board[row][col].setPlayer(1);
board[row][col].setKing(false);
}
}
}
else if(board[oldRow][oldCol].getPlayer()==1 && board[oldRow][oldCol].getKing()==true)
{
if(row==oldRow-1 || row==oldRow+1)
{
if(col==oldCol-1 || col==oldCol+1)
{
board[oldRow][oldCol].setPlayer(0);
board[row][col].setPlayer(1);
board[row][col].setKing(true);
}
}
}
else if(board[oldRow][oldCol].getPlayer()==2 && board[oldRow][oldCol].getKing()==true)
{
if(row==oldRow-1 || row==oldRow+1)
{
if(col==oldCol-1 || col==oldCol+1)
{
board[oldRow][oldCol].setPlayer(0);
board[row][col].setPlayer(2);
board[row][col].setKing(true);
}
}
}
else if(board[oldRow][oldCol].getPlayer()==2 && board[oldRow][oldCol].getKing()==false)
{
if(row==oldRow+1)
{
if(col==oldCol-1 || col==oldCol+1)
{
board[oldRow][oldCol].setPlayer(0);
board[row][col].setPlayer(2);
board[row][col].setKing(false);
}
}
}
}
}
repaint();
}

Java Gett variable from within a loop in another class?

I was wondering how it is possible to get the value of a variable/boolean that is inside of a loop in a different class.
I would have a variable in one class and want get that in another one:
Class1:
public void mainLoop()
{
while(!Display.isCloseRequested)
{
frames++
if(frames == 200)
{
key = 5
run = false;
}
if(frames == 400)
{
key = 10
run = true;
}
}
}
and in my other Class2 I want to acess the changed varibles:
public Class2()
{
public void printVariables(int key)
{
if(key == 5) { System.out.println("KEY 5"); }
if(key == 10) { System.out.println("KEY 10"); }
if(run == false) { System.out.println("RUN FALSE"); }
if(run == true) { System.out.println("RUN TRUE"); }
}
}
How?
Thanks for any Help!
Add it as a parameter to the method:
public Class2()
{
public void printVariables(int key)
{
if(key == 5) { System.out.println("KEY 5"); }
if(key == 10) { System.out.println("KEY 10"); }
if(run == false) { System.out.println("RUN FALSE"); }
if(run == true) { System.out.println("RUN TRUE"); }
}
}
And then call that method with an instance of the class:
public void mainLoop()
{
Class2 cls2 = new Class2();
while(someCondition == true)
{
frames++
if(frames == 200)
{
key = 5
run = false;
}
if(frames == 400)
{
key = 10
run = true;
}
cls2.printVariables(key);
}
}
Or, if you can, make the method static and call it statically (i.e. Class2.printVariables(key)).

JFormattedTextfield in an applet is inserting spaces

I've got an applet that pops up a dialog to enter credit card details.
I'm using four JFormattedTextField classes to allow the user to input each 4 digit fragment of the credit card. I've got code in place to auto tab to the next field as the user is typing out the CC number. The problem is that when the dialog is launched from an applet running in IE8 using ( I'm restricted to using Java 7 update 13), if the user types in the number quickly, spaces appear to be inserted apparently at random even though a mask is applied to only allow alpha-numerics.
The code below is are the listeners on the the JFormattedTextField class where I suspect the issue resides.
super.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent event) {
EntryField field = (EntryField)event.getSource();
if (_initialPosition > 0) {
// if the initial position is set externally. This will happen when the caret is being set during
// the initial load of the dialog.
field.select(0, 0);
field.setCaretPosition(_initialPosition);
_initialPosition = -1;
}
else if (event.getOppositeComponent() == field.getNextControl()) {
field.select(0, 0);
field.setCaretPosition(field.getMaxCharacters()-1);
}
else {
field.select(0, 0);
field.setCaretPosition(0);
}
}
#Override
public void focusLost(FocusEvent event) {}
});
this.addKeyListener(new KeyListener() {
#Override
public void keyPressed(KeyEvent keyEvent) {
if(keyEvent.getKeyChar() == KeyEvent.VK_BACK_SPACE) {
EntryField field = (EntryField) keyEvent.getSource();
if (field.getCaretPosition() == 0 && field.hasPreviousControl()) {
field.backtrackFocus(true);
}
}
else if(Character.isLetterOrDigit(keyEvent.getKeyChar())) {
EntryField field = (EntryField) keyEvent.getSource();
if (field.getCaretPosition() == field.getMaxCharacters() - 1) {
field.moveFocus();
}
}
else if(keyEvent.getKeyCode() == KeyEvent.VK_LEFT) {
EntryField field = (EntryField) keyEvent.getSource();
if (field.getCaretPosition() == 0 && field.hasPreviousControl()) {
field.backtrackFocus(false);
}
}
else if(keyEvent.getKeyCode() == KeyEvent.VK_RIGHT) {
EntryField field = (EntryField) keyEvent.getSource();
if(field.getCaretPosition() == field.getMaxCharacters() - 1 && field.hasNextControl()) {
field.moveFocus();
}
}
else if(keyEvent.isControlDown() && keyEvent.getKeyChar() == 'c') {
EntryField field = (EntryField) keyEvent.getSource();
field.paste();
}
}
#Override
public void keyTyped(KeyEvent keyEvent) {
if (_checkAmEx && Character.isLetterOrDigit(keyEvent.getKeyChar())) {
EntryField field = (EntryField)keyEvent.getSource();
int pos = field.getCaretPosition();
if (pos == 2 && creditCardIsAmex()) {
field.moveFocus();
}
}
else if(keyEvent.getKeyChar() == KeyEvent.VK_DELETE) {
EntryField field = (EntryField) keyEvent.getSource();
field.setCaretPosition(field.getCaretPosition() - 1);
field.shiftTextLeft();
}
else if(keyEvent.getKeyChar() == KeyEvent.VK_BACK_SPACE) {
EntryField field = (EntryField) keyEvent.getSource();
field.shiftTextLeft();
}
}
#Override
public void keyReleased(KeyEvent keyEvent) {}
});

Why does my import file not work?

I am trying to make my file ClickingButtons.java to work, but I am getting an error that it cant find the symbols playerHasItem & freeSlots. Now I tried importing my ItemAssistant.java file, which has the public ints playerHasItem & freeSlots, but after saving and compiling it is still giving me the same errors! :(
This is my import statement in ClickingButtons.java:
import server.model.items.ItemAssistant;
And these are the public ints in my ItemAssistant:
public int freeSlots()
{
int freeS = 0;
for (int i=0; i < c.playerItems.length; i++){
if (c.playerItems[i] <= 0){
freeS++;
}
}
return freeS;
}
public boolean playerHasItem(int itemID, int amt, int slot) {
itemID++;
int found = 0;
if (c.playerItems[slot] == (itemID)) {
for (int i = 0; i < c.playerItems.length; i++) {
if (c.playerItems[i] == itemID) {
if(c.playerItemsN[i] >= amt) {
return true;
} else {
found++;
}
}
}
if(found >= amt) {
return true;
}
return false;
}
return false;
}
public boolean playerHasItem(int itemID, int amt) {
itemID++;
int found = 0;
for (int i = 0; i < c.playerItems.length; i++) {
if (c.playerItems[i] == itemID) {
if(c.playerItemsN[i] >= amt){
return true;
} else{
found++;
}
}
}
if(found >= amt) {
return true;
}
return false;
}
The code that calls fo these ints:
if (c.dialogueAction == 717)
{
if (freeSlots() > 0)
{
if (c.tempRune == 1)//air
{
if (c.omniQuest == 2)
{
if (c.getItems().playerHasItems(995,10000) && c.getItems().playerHasItems(556,10))
{
c.getItems().deleteItems(995,10000);
c.getItems().deleteItems(556,10);
c.getItems().addItems(13599, 1);
c.getDH().sendDialogues(749,57);
} else
if (!c.getItems().playerHasItems(995,10000))
{
c.getDH().sendDialogues(750, 57);
} else
if (!c.getItems().playerHasItems(556,10))
{
c.getDH().sendDialogues(751, 57);
}
} else
if (c.getItems().playerHasItems(995,20000) && c.getItems().playerHasItems(556,20))
{
c.getItems().deleteItems(995,20000);
c.getItems().deleteItems(556,20);
c.getItems().addItems(13599, 1);
c.getDH().sendDialogues(749,57);
} else
if (!c.getItems().playerHasItems(995,20000))
{
c.getDH().sendDialogues(750, 57);
} else
if (!c.getItems().playerHasItems(556,20))
{
c.getDH().sendDialogues(751, 57);
}
} else
if (c.tempRune == 6)//body
{
if (c.omniQuest == 2)
{
if (c.getItems().playerHasItems(995,10000) && c.getItems().playerHasItems(559,10))
{
c.getItems().deleteItems(995,10000);
c.getItems().deleteItems(559,10);
c.getItems().addItems(13604, 1);
c.getDH().sendDialogues(749,57);
} else
if (!c.getItems().playerHasItems(995,10000))
{
c.getDH().sendDialogues(750, 57);
} else
if (!c.getItems().playerHasItems(559,10))
{
c.getDH().sendDialogues(751, 57);
}
} else
if (c.getItems().playerHasItems(995,20000) && c.getItems().playerHasItems(559,20))
{
c.getItems().deleteItems(995,20000);
c.getItems().deleteItems(559,20);
c.getItems().addItems(13604, 1);
c.getDH().sendDialogues(749,57);
} else
if (!c.getItems().playerHasItems(995,20000))
{
c.getDH().sendDialogues(750, 57);
} else
if (!c.getItems().playerHasItems(559,20))
{
c.getDH().sendDialogues(751, 57);
}
} else
if (c.tempRune == 13)//astral
{
if (c.omniQuest == 2)
{
if (c.getItems().playerHasItems(995,10000) && c.getItems().playerHasItems(9075,10))
{
c.getItems().deleteItems(995,10000);
c.getItems().deleteItems(9075,10);
c.getItems().addItems(13611, 1);
c.getDH().sendDialogues(749,57);
} else
if (!c.getItems().playerHasItems(995,10000))
{
c.getDH().sendDialogues(750, 57);
} else
if (!c.getItems().playerHasItems(9075,10))
{
c.getDH().sendDialogues(751, 57);
}
} else
if (c.getItems().playerHasItems(995,20000) && c.getItems().playerHasItems(9075,20))
{
c.getItems().deleteItems(995,20000);
c.getItems().deleteItems(9075,20);
c.getItems().addItems(13611, 1);
c.getDH().sendDialogues(749,57);
} else
if (!c.getItems().playerHasItems(995,20000))
{
c.getDH().sendDialogues(750, 57);
} else
if (!c.getItems().playerHasItems(9075,20))
{
c.getDH().sendDialogues(751, 57);
}
}
} else
{
c.getDH().sendDialogues(752, 57);
c.tempRune = 0;
}
}
Also, the import directory is correct.
Why oh why is it not working?
Your declaration describes method with three int parameters:
playerHasItem(int itemID, int amt, int slot)
yet you are calling it with two:
playerHasItems(995,20000)
There is simply no method with two parameters declared.

Categories