Question: How can I get my Java Blackjack table to put a new card on the table when I select the hit button? import java.awt.Color; import
How can I get my Java Blackjack table to put a new card on the table when I select the hit button?
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.ImageIcon; import javax.imageio.ImageIO; import javax.swing.JButton; import java.awt.BorderLayout; import javax.swing.JPanel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class BlackjackTable3 extends JFrame {
JButton stayButton = new JButton("STAY"); JButton hitButton = new JButton("HIT"); JPanel mainPanel = new JPanel();
public BlackjackTable3() { this.setTitle("Blackjack Test Table"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //JPanel tablePanel = new JPanel(); ImageIcon pic = new ImageIcon("blackjackTableCanvas.jpg"); mainPanel.add(new JLabel(pic)); this.add(mainPanel, BorderLayout.PAGE_START); this.setSize(1400,1400); this.setVisible(true); JPanel buttonPanel = new JPanel(); ActionListener pressChoice = new DecisionListener(); hitButton.addActionListener(pressChoice); stayButton.addActionListener(pressChoice); buttonPanel.setSize(300,150); buttonPanel.add(hitButton,BorderLayout.WEST); buttonPanel.add(stayButton,BorderLayout.EAST); buttonPanel.setVisible(true); this.add(buttonPanel, BorderLayout.SOUTH); // this.add(hitButton, BorderLayout.WEST); // this.add(stayButton, BorderLayout.EAST); }
class DecisionListener implements ActionListener{ public void actionPerformed(ActionEvent a){ //code for hit/stay to go here if(a.getSource() == hitButton){ System.out.println("YOU CHOSE HIT!"); add(new CardAdder3(new Card())); } else if(a.getSource() == stayButton){ System.out.println("YOU CHOSE STAY!"); } } } public class CardAdder3 extends JComponent{
String val; String suit; public CardAdder3(Card card){ this.val = card.value.face; this.suit = card.suit.toString(); String filename = this.fetchCardFileLabel(); JPanel p = new JPanel(); ImageIcon i = new ImageIcon("card deck\\" + filename + ".png"); p.add(new JLabel(i));
}
public String fetchCardFileLabel(){ String first = null; String second = suit.substring(0,1); if(!val.equals("10")) first = val.substring(0,1); else first = val.substring(0,2); return "" + first + second; } } public static void main(String[] args){ BlackjackTable3 b = new BlackjackTable3();
} }
if I put the CardRender3 inside a frame, then I get a new frame, and I want the new card on the table image. How can I do that?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
