I am making a blackjack game and was wondering if I could make it so that I had two different actions on a single button.
This is what I have so far on the hit button, but instead of having the second card show on a double click, I want it to show the second card when you press the button again.
public void hit(MouseEvent event) {
if (event.getClickCount() == 1) {
card5 = deck.dealCard();
pcard3.setImage(card5.getImage());
}
if (event.getClickCount() == 2) {
card6 = deck.dealCard();
pcard4.setImage(card6.getImage());
}
}
You can have an iterator whose value can be increased on each click. And for different values set different functionalities. See the code
int i =0;
public void hit(MouseEvent event) {
if (i%2== 0) {
card5 = deck.dealCard();
pcard3.setImage(card5.getImage());
} else if (i%2 == 1) {
card6 = deck.dealCard();
pcard4.setImage(card6.getImage());
}
i++;
}
Related
I am making a Tic-Tac-Toe game in Java. I have four classes:
TicTacTester just calls (creates an object) the TicTacToe class.
The TicTacToe class provides the GUI of the game (it's a subclass of JFrame). It also creates the 9 buttons for the JPanel to display and users to click.
The XOButton class defines what the buttons can do and actionPerformed method.
Lastly, the GameEnd class defines what happens when the game ends (a new JFrame is created to display score and give the user 2 buttons: exit and restart).
The problem is when I try to code the contents of what happens when the user clicks "restart". It is suppose to call the resetBoard() method, which is defined in TicTacToe class. The problem is, I do not know the name of the object created from the TicTacToe class (in the tester class' static void main method I just typed "new TicTacToe", didn't need to define a name). I cannot call resetBoard from a static standpoint (i.e. I can't do TicTacToe.resetBoard(); ) because resetBoard needs to be non-static.
What I've tried:
I've tried including in the constructor of the GameEnd class a TicTacToe object. If I do this, the GameEnd object creator has to go into TicTacToe class, so I can use 'this' keyword. This does not work because the GameEnd object needs to be created when the WinCondition is met, which is checked when a button is clicked in the XOButton class.
But if I put the GameEnd object creator in the XOButton class (where is is right now and, supposedly, where it should be), in the constructor for GameEnd(String s, TicTacToe a), I cannot use 'this' keyword for the TicTacToe object.
This is my button class. Most code is not relevant so it has been hidden.
public class XOButton extends JButton implements ActionListener {
//Hidden code
private void winCheck() {
for(int j = 0; j < 3; j++) {
if(board[j][0] == 1 && board[j][1] == 1 && board[j][2] == 1 || board[0][j] == 1 && board[1][j] == 1 && board[2][j] == 1) {
player1Score++;
GameEnd end = new GameEnd("X wins this round!");
finished = true;
break;
}
else if(board[j][0] == 2 && board[j][1] == 2 && board[j][2] == 2 || board[0][j] == 2 && board[1][j] == 2 && board[2][j] == 2) {
player2Score++;
GameEnd end = new GameEnd("O wins this round!");
finished = true;
break;
}
}
if(board[0][0] == 1 && board[1][1] == 1 && board[2][2] == 1 || board[0][2] == 1 && board[1][1] == 1 && board[2][0] == 1) {
player1Score++;
GameEnd end = new GameEnd("X wins this round!");
finished = true;
}
else if(board[0][0] == 2 && board[1][1] == 2 && board[2][2] == 2 || board[0][2] == 2 && board[1][1] == 2 && board[2][0] == 2) {
player2Score++;
GameEnd end = new GameEnd("O wins this round!");
finished = true;
}
if(turn == 9 && !finished) {
GameEnd end = new GameEnd("This round is a Draw");
finished = true;
}
}
public void resetButton() {
this.setIcon(null);
markSpot(0);
this.clicked = false;
}
public static void resetStatics() {
turn = 0;
finished = false;
}
}
This is the TicTacToe class. Most code is not relevant so it has been hidden.
public class TicTacToe extends JFrame {
//Hidden code
public void resetBoard() {
for(int j = 0; j < 3; j++) {
for(int i = 0; i < 3; i++) {
buttons[j][i].resetButton();
}
}
XOButton.resetStatics();
}
}
This is the GameEnd class. An object of this is created when the WinCondition is met. Most code is not relevant so it has been hidden.
public class GameEnd extends JFrame implements ActionListener {
//Hidden code
public void actionPerformed(ActionEvent e) {
if(e.getSource() == exit) {
System.exit(0);
}
else if(e.getSource() == retry) {
TicTacToe.resetBoard();
}
}
}
//This is the Tester class
public class TicTacTester {
public static void main(String[] args) {
new TicTacToe();
}
}
What I expect to happen is the game board to restart.
It seems like you are breaking the project down too far. If you have a class for a game, keep all of its methods inside that class. The only time I can think of to use separate classes are if you are trying to create this using the Model-View-Controller (MVC) architecture, or the like. This is a common approach for an application using GUI, data access, and controllers.
There might be other times, but this is all I can think of on the top of my head.
From what you are showing, I don't think it applies.
I think this would be a better approach:
TicTacToe.java
public class TicTacToe extends JFrame {
private int scorePlayer1;
private int scorePlayer2;
private boolean initialized; // default = false
// Reset the board
// Reset the markers, etc.
public void resetGame() {}
// Check to see if there is a winner
// If so, announce the winner and return true
// Otherwise, return false.
public boolean winCheck() {}
// Set up frame, buttons, score, etc.
public void newGame(){
// Create however many JPanels you need
// Add it to a single JFrame
// When you're ready...
// and this game has not been initialized for the first time.
// In this example jframe is an instance of JFrame
if (!this.initialized)
jframe.setVisible(true);
}
// Play the game
public void play(){
// For each round,
// a player chooses where to put their "X" or "O"
// add scores, etc.
// Then at the end of each round
if (this.winCheck())
this.resetGame();
}
}
TicTacToeTester.java:
public class TicTacToeTester {
public static void main(String[] args){
TicTacToe game = new TicTacToe();
game.play();
}
}
I hope this helps!
Let me know if you have any questions. :)
I am creating a matching card application. That only allows the user to select two cards per round.
Where you see the bricks are where the buttons are and so i have added them to an arraylist, but im not sure how to make it so that they can only press 2 buttons until they press the guess button where it checks if they are a match.
https://pastebin.com/65NqJ0GK
private void card1ButtonActionPerformed(java.awt.event.ActionEvent evt) {
String temp = cards.get(0);
if (temp.equals("0")) {
card1Button.setIcon(a);
} else if (temp.equals("1")) {
card1Button.setIcon(b);
} else if (temp.equals("2")) {
card1Button.setIcon(c);
} else if (temp.equals("3")) {
card1Button.setIcon(d);
} else if (temp.equals("4")) {
card1Button.setIcon(e);
} else if (temp.equals("5")) {
card1Button.setIcon(f);
} else if (temp.equals("6")) {
card1Button.setIcon(g);
} else if (temp.equals("7")) {
card1Button.setIcon(h);
}
count++;
if (count == 1) {
c1 = Integer.parseInt(temp);
change[0] = 0;
} else if (count == 2) {
c2 = Integer.parseInt(temp);
change[0] = 0;
}
}
^^Here is an example of a button code.
Could someone show me some way to do it.
I am trying to make a donkey kong game.
Currently, I have a boolean called jump which tells that game if the player is jumping or not, when the space bar is pressed.
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE){
StartJump();
}
}
StartJump: Makes the sprite start jumping by changing velocity. It also changes the jump boolean to true.
public void StartJump() {
if (onground) {
velocityY = -4;
onground = false;
jump = true;
}
}
KeyReleased:
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE){
EndJump();
}
}
EndJump: Sets jump to false
public void EndJump() {
if(velocityY < -6.0)
velocityY = -6;
jump = false;
}
The problem occurs when you hold down the spacebar, jump is set to true.
Which then causes the forloop which sets the Y position of Mario to not work and he falls through the floor.
Loop: bricks just contains rectangles for Mario to stand on
for(int i = 0; i < bricks.size(); i++) {
if(marHitBox.intersects(bricks.get(i)) && !mario.getClimb() && !mario.getJump() && marHitBox.intersects(jump.get(i))) {
mario.setMarY((int)bricks.get(i).getY()-40);
mario.setVeloY(0f);
mario.setOnGround(true);
}
}
It is the !mario.getJump() that is causing the problem. Is there any way to see if the spacebar is held for more than x milliseconds to make jump = false?
Try measuring the passing time between the spacebar is pressed and released, it will fit your need.
I think just a check would make it go away
public void StartJump(){
if (!jump && onground){
velocityY = -4;
onground = false;
jump = true;
}
}
I have a imageView, button inside an activity, 10 pictures that are named from stage1 to stage9.
I need you help to solve some problem
I would like to use a button click to change picture that is on imageView to the next one.
I mean, I want to press on the button, and image view shows stage2 image, I press the button again and stage3 picture will be shown.
I have done this using counter var which count number of clicks and then running if statment to see which picture should go next, but it is too long and it is not possible to have more or less pictures.
I would like to know is there a way to do this, and if possible please show me how.
Thanks
code
private void changeImage(int counter) {
if (counter == 1) {
image.setImageResource(R.drawable.stage2);
} else if (counter == 2) {
image.setImageResource(R.drawable.stage3);
} else if (counter == 3) {
image.setImageResource(R.drawable.stage4);
} else if (counter == 4) {
image.setImageResource(R.drawable.stage5);
} else if (counter == 5) {
image.setImageResource(R.drawable.stage6);
} else if (counter == 6) {
image.setImageResource(R.drawable.stage7);
} else if (counter == 7) {
image.setImageResource(R.drawable.stage8);
} else if (counter == 8) {
image.setImageResource(R.drawable.stage9);
}
}
bassically this is the code that I am using right now.
It works, but if I want to do it to be more dynamic.
You'll need to tell the vm to update -> 'invalide()'.
Or use Picasso,
Picasso.with(Statics.context).load(R.drawable.stageX).error(R.drawable.error_img).resize(width, height).priority(Priority.HIGH).into(image);
Just get resource id by name, use like,
private void changeImage(int counter) {
if(counter >= 1 && counter <= 9) // Always check counter value before accessing as resource id
{
int counterValue = counter+1;
int resourceId = Resources.getSystem().getIdentifier("stage"+counterValue, "drawable", this.getPackageName()); // Use application context to get package name
image.setImageResource(resourceId);
}
}
Here is my method code I use in onTouchEvent:
public void touch(MotionEvent event) {
// starta = "klicka för att börja";
if (event.getAction() == MotionEvent.ACTION_DOWN) {
starta = "Click to start";
fN.aktivitetNer.start();
if (event.getAction() == MotionEvent.ACTION_CANCEL) {
if (y == -300) {
touch(event);
}
else if (y > -300) {
fN.aktivitetNer.interrupt();
fU.aktivitetUpp.start();
p++;
}
}
else if (y2 <= y + xMax) {
fN.aktivitetNer.interrupt();
fU.aktivitetUpp.start();
p = 0;
}
}
}
What should happening is a knife coming down very fast with help of a Thread(fN.aktivitetNer) and if the knife slice the finger(y2) you loose, if you're fast enough you score one point(p), when you loose or win the knife will go up again with another Thread(fU.aktivitetUpp)
I know it's not perfect but it's now working at all!
ps. if you click two times on the screen the programm will crash.
Well it certainly won't work in it's current form, because the code will never enter your second if statement.
Your first if statement asserts that event.getAction() == MotionEvent.ACTION_DOWN. If this is true, then atevent.getAction() cannot possibly be equal to MotionEvent.ACTION_CANCEL and you will never enter that block of code.
Since I don't know what your thread does I can't be certain, but you probably want something more like this:
public void touch(MotionEvent event)
{
// starta = "klicka för att börja";
int actionCode = event.getAction();
if (actionCode == MotionEvent.ACTION_DOWN)
{
//Start to drop knife
}
else if (actionCode == MotionEvent.ACTION_CANCEL || actionCode == MotionEvent.ACTION_UP)
{
if (Knife is falling, and touch point is below knife)
{
//Add one to score
}
else if (Knife is falling, and touch point is above knife)
{
//Reset score to 0
}
}
else if (Knife has already fallen and still touching screen)
{
//Reset score to 0
}
}