Question: How can I do the same program using JAVA FX? This is programs emulate a tic tac toe, but I would like to use Java
How can I do the same program using JAVA FX? This is programs emulate a tic tac toe, but I would like to use Java FX. Can someone help me? import javax.swing.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; import java.util.*; public class ShowTicTac { public static void main(String[] args) { JFrame win = new JFrame(); win.setVisible(true); win.setLayout(new GridLayout(3,3, 5,5)); MyPanel [] tic = new MyPanel[9]; for(int i = 0; i < 9 ; i++) { tic[i] = new MyPanel();tic[i].myPanel.setBackground(new Color((int)(Math.random()*256), (int)(Math.random()*256),(int)(Math.random()*256))); win.add(tic[i]); } win.setSize(550,550); win.setResizable(false); } } class MyPanel extends JPanel { public CustomPanel myPanel; private JPanel jpButton; private JButton square; private JButton circle; private Color c; public void setColor(Color cc) { c = cc; myPanel.setBackground(c); } public MyPanel() { myPanel = new CustomPanel(); circle = new JButton("Circle"); square = new JButton("Square"); circle.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { myPanel.draw(CustomPanel.CIRCLE); } } ); square.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { myPanel.draw(CustomPanel.SQUARE); } } ); jpButton = new JPanel(); jpButton.setSize(150,25); jpButton.setLayout(new GridLayout(1,2)); jpButton.add(circle);jpButton.add(square); this.setLayout(new BorderLayout()); this.add(myPanel,BorderLayout.CENTER); this.add(jpButton,BorderLayout.SOUTH); } public Dimension getPreferredSize() { return new Dimension(150,175); } public Dimension getMinimumSize() { return getPreferredSize(); } public void paintComponent (Graphics g) { super.paintComponent(g); } } class CustomPanel extends JPanel { public static final int CIRCLE = 0; public static final int SQUARE = 1 ; private int shape ; public CustomPanel() { } public Dimension getPreferredSize() { return new Dimension(150,150); } public Dimension getMinimumSize() { return getPreferredSize(); } public void paintComponent (Graphics g) { super.paintComponent(g); g.setColor(Color.BLUE); if(shape == 0) { g.fillOval(50 , 10 ,80, 80); } else g.fillRect(50,10,80,80); } public void draw(int choice) { shape = choice; repaint(); } }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
