I am creating a simple game involving buttons to play. My code is split into two separate programs, a GraphicsMain and GraphicsPanel. In the main, I have built my Jframe and buttons. In the panel, is my graphics. My issue is trying to to get the paintComponent and graphics to work in my main program.
I tried to place the libraries and code into the first program, the main, but it didn’t work…
`//Here is my main program:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
public class GraphicsMain extends JFrame{
public static void main(String[] args){
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
GraphicsMain window = new GraphicsMain();
JPanel p = new JPanel();
p.add(new GraphicsPanel()); // add a class that extends JPanel
window.setTitle("Arrays Project");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setContentPane(p);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
//Buttons for Players to select their move:
JButton button = new JButton("Add 1:");
button.setBounds(29, 400, 75, 50);
JButton button2 = new JButton("Add 2:");
button2.setBounds(101, 400, 75, 50);
JButton button3 = new JButton("Add 1:");
button3.setBounds(329, 400, 75, 50);
JButton button4 = new JButton("Add 2:");
button4.setBounds(401, 400, 75, 50);
window.setLayout(null);
//Adds buttons to the frame:
window.add(button);
window.add(button2);
window.add(button3);
window.add(button4);
//Makes the frame visible:
window.setVisible(true);
//Action that occurs when the players click on their buttons:
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
g2.fillOval(20, 20, 20, 20);
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Display a message dialog when the button is clicked
JOptionPane.showMessageDialog(window, "Button Clicked!");
}
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Display a message dialog when the button is clicked
JOptionPane.showMessageDialog(window, "Button Clicked!");
}
});
button4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Display a message dialog when the button is clicked
JOptionPane.showMessageDialog(window, "Button Clicked!");
}
});
}
}
}
//Here is my panel program:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import java.awt.Font;
public class GraphicsPanel extends JPanel{
public GraphicsPanel(){
setPreferredSize(new Dimension(500, 500));
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
//1x10 board for the game:
int count = 0;
int [] board = new int [10];
for (int i = 0; i < board.length; i++) {
g2.drawRect(0 + count, 250, 50, 50);
count += 50;
}
//Labels for each square 1-10:
g2.setFont(new Font("TimesRoman", Font.PLAIN, 20));
g2.drawString("1", 20, 325);
g2.drawString("2", 70, 325);
g2.drawString("3", 120, 325);
g2.drawString("4", 170, 325);
g2.drawString("5", 220, 325);
g2.drawString("6", 270, 325);
g2.drawString("7", 320, 325);
g2.drawString("8", 370, 325);
g2.drawString("9", 420, 325);
g2.drawString("10", 465, 325);
//Title and instructions for players:
g2.drawString("1, 2, …, 10 Game", 175, 25);
g2.drawString("To Play: Players alternate placing either one or two pieces", 10, 60);
g2.drawString("on the leftmost open squares. In this game, we don’t", 30, 85);
g2.drawString("distinguish between players’ pieces, so we’ll call", 40, 110);
g2.drawString("them left and right. Left will go first.", 85, 135);
g2.drawString("The first player to place the tenth piece on the board wins!", 15, 170);
//Labels for players buttons:
g2.drawString("Player 1", 67, 385);
g2.drawString("Player 2", 367, 385);
}
}`
New contributor
Panagiotis Georgopanos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1