Move method from one class to another - java

So I have a method makeBoard. I want to create a class called Board and move the makeBoard method into class Board, where it still adds the returned panel to the content pane of JFrame. Im not sure how to get the JPanel from Board class onto the JFrame content pane on the reversi class. Not sure how to proceed about this.
package reversi;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.awt.*;
import javax.swing.*;
public class Reversi
{
public JFrame frame;
private JLabel userName1 = new JLabel("Enter First player: ");
private JLabel userName2 = new JLabel("Enter Second player: ");
private JTextField textUsername1 = new JTextField(15);
private JTextField textUsername2 = new JTextField(15);
private JButton startButton = new JButton("START");
private JButton [][] squares = new JButton[8][8];
public Reversi()
{
makeFrame();
makeMenuBar();
}
private void makePlayerPanel()
{
JPanel userInterface = new JPanel(new GridBagLayout());
userInterface.setBackground(Color.LIGHT_GRAY);
userInterface.setBorder(BorderFactory.createBevelBorder(0));
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
userInterface.add(userName1, c);
c.gridx = 1;
userInterface.add(textUsername1, c);
c.gridx = 0;
c.gridy = 1;
userInterface.add(userName2, c);
c.gridx = 1;
userInterface.add(textUsername2, c);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 2;
c.anchor = GridBagConstraints.SOUTH;
userInterface.add(startButton, c);
userInterface.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Players/Scoreboard"));
JPanel wrapper = new JPanel();
wrapper.add(userInterface);
frame.add(wrapper, BorderLayout.LINE_END);
frame.pack();
}
private void makeBoard()
{
JPanel board = new JPanel();
board.setBackground(Color.GRAY);
board.setBorder(BorderFactory.createBevelBorder(1));
board.setPreferredSize(new Dimension(750, 700));
board.setLayout(new GridLayout(8,8));
for(int i = 0; i< 8; i++){
for (int j = 0; j < 8; j++)
{
squares[i][j] = new JButton();
squares[i][j].setBackground(Color.GREEN);
board.add(squares[i][j]);
}
}
frame.add(board, BorderLayout.CENTER);
frame.pack();
}
public void makeFrame()
{
frame = new JFrame("Reversi Game");
frame.setSize(800,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
makeBoard();
makePlayerPanel();
}
}

i did it like this:
public class Board {
private JButton [][] squares = new JButton[8][8];
public JFrame frame;
public JPanel makeBoard()
{
JPanel board = new JPanel();
board.setBackground(Color.GRAY);
board.setBorder(BorderFactory.createBevelBorder(1));
board.setPreferredSize(new Dimension(750, 700));
board.setLayout(new GridLayout(8,8));
for(int i = 0; i< 8; i++){
for (int j = 0; j < 8; j++)
{
squares[i][j] = new JButton();
squares[i][j].setBackground(Color.GREEN);
board.add(squares[i][j]);
}
}
return board;
}
}
in Reversi do like this:
Board b = new Board();
frame.add(b.makeBoard(), BorderLayout.CENTER);
frame.pack();

public class Board {
public void makeBoard(JPanel board)
{
board.setBackground(Color.GRAY);
board.setBorder(BorderFactory.createBevelBorder(1));
board.setPreferredSize(new Dimension(750, 700));
board.setLayout(new GridLayout(8,8));
for(int i = 0; i< 8; i++){
for (int j = 0; j < 8; j++)
{
squares[i][j] = new JButton();
squares[i][j].setBackground(Color.GREEN);
board.add(squares[i][j]);
}
}
frame.add(board, BorderLayout.CENTER);
frame.pack();
}
}
In Reversi class
public class Reversi
{
....
Board board = new Board();
board.makeBoard(new JPanle());
}

Related

JLabel not showing in my grid of JButtons

Creating a game called reversi also known as Othello and I am trying to add the starting position of my Black and Whites counters (using JLabel, labelled 'W' and 'B') in the middle of the board diagonally opposite from each other but for some reason only 2 show up and the other 2 don't show, which I don't understand why.
How do I go about fixing it?
import java.io.*;
import java.util.*;
import java.lang.*;
import java.awt.*;
import javax.swing.*;
public class Reversi
{
// instance variables - replace the example below with your own
private JFrame frame;
private JLabel userName1;
private JLabel userName2;
private JTextField textUsername1;
private JTextField textUsername2;
private JButton startButton;
private JButton [][] squares = new JButton[8][8];
private JLabel whites = new JLabel("W");
private JLabel blacks = new JLabel("B");
/**
*
*/
public Reversi()
{
// initialise instance variables
gui();
}
/**
*
*/
public void gui()
{
frame = new JFrame("Reversi Game");
frame.setSize(800,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setLayout(new BoxLayout(frame, BoxLayout.X_AXIS));
JPanel userInterface = new JPanel(new GridBagLayout());
userInterface.setBackground(Color.LIGHT_GRAY);
userInterface.setBorder(BorderFactory.createBevelBorder(0));
//userInterface.setPreferredSize(new Dimension(350,700));
GridBagConstraints c = new GridBagConstraints();
//c.anchor = GridBagConstraints.NORTH;
JPanel board = new JPanel();
board.setBackground(Color.GRAY);
board.setBorder(BorderFactory.createBevelBorder(1));
board.setPreferredSize(new Dimension(750, 700));
userName1 = new JLabel("Enter First player: ");
userName2 = new JLabel("Enter Second player: ");
textUsername1 = new JTextField(15);
textUsername2 = new JTextField(15);
startButton = new JButton("START");
c.gridx = 0;
c.gridy = 0;
userInterface.add(userName1, c);
c.gridx = 1;
userInterface.add(textUsername1, c);
c.gridx = 0;
c.gridy = 1;
userInterface.add(userName2, c);
c.gridx = 1;
userInterface.add(textUsername2, c);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 2;
c.anchor = GridBagConstraints.SOUTH;
userInterface.add(startButton, c);
userInterface.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Players/Scoreboard"));
board.setLayout(new GridLayout(8,8));
for(int i = 0; i< 8; i++){
for (int j = 0; j < 8; j++)
{
squares[i][j] = new JButton();
squares[i][j].setBackground(Color.GREEN);
board.add(squares[i][j]);
}
}
squares[4][3].add(blacks);
squares[4][4].add(whites);
squares[3][4].add(blacks);
squares[3][3].add(whites);
frame.add(board, BorderLayout.CENTER);
JPanel wrapper = new JPanel();
wrapper.add(userInterface);
frame.add(wrapper, BorderLayout.LINE_END);
makeMenuBar();
frame.pack();
frame.setVisible(true);
}
private void makeMenuBar()
{
//Finish coding later
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu fileMenu = new JMenu("File");
menubar.add(fileMenu);
JMenuItem saveFile = new JMenuItem("Save File");
fileMenu.add(saveFile);
JMenuItem quitGame = new JMenuItem("Quit");
fileMenu.add(quitGame);
quitGame.addActionListener(ev -> {quit(); });
}
private void quit()
{
System.exit(0);
}
}
Each component (i.e. your JLabels (whites and blacks)) can only be added to a container once, if you need to add more labels, even if they have the same String inside, you have to create a new object for those, otherwise these will be shown in the last container you've added them.
squares[4][3].add(blacks);
squares[4][4].add(whites);
squares[3][4].add(blacks); // Only this one is added
squares[3][3].add(whites); // Only this one is added
You'll need something like this, or have an array of JLabels and add them to all your squares, then just call yourJLabelArray[i][j].setText("W") (or "B")
squares[4][3].add(new JLabel("B"));
squares[4][4].add(new JLabel("W"));
squares[3][4].add(new JLabel("B"));
squares[3][3].add(new JLabel("W"));

Remove gap between GridLayout elements

I am trying to create a 9x9 grid of JTextFields using GridLayout, however I am unable to remove the gap between the JTextFields within the GridLayout.
I have tried setting the horizontal and vertical gap to 0 using setHgap and setVgap methods, but the gap still exists, where am I going wrong ?
This is what I have tried so far,
class BoardGUI extends JFrame implements ActionListener {
private JPanel centerPanel, bottomPanel;
private JTextField grid[][];
private JButton loadBtn, saveBtn, solveBtn;
private GridLayout gridLayout;
private final int N = 9;
private final int MAX_HEIGHT = 450;
private final int MAX_WIDTH = 500;
public BoardGUI() {
super("Solver v2.0");
centerPanel = new JPanel();
bottomPanel = new JPanel();
grid = new JTextField[N][N];
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
grid[i][j] = new JTextField(1);
loadBtn = new JButton("Load");
saveBtn = new JButton("Save");
solveBtn = new JButton("Solve");
gridLayout = new GridLayout(N,N);
gridLayout.setVgap(0);
gridLayout.setHgap(0);
initLayout();
registerEventListeners();
}
private void initLayout() {
centerPanel.setLayout(gridLayout);
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
centerPanel.add(grid[i][j]);
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
bottomPanel.add(loadBtn);
bottomPanel.add(saveBtn);
bottomPanel.add(solveBtn);
this.setLayout(new BorderLayout());
this.getContentPane().add(centerPanel, BorderLayout.CENTER);
this.getContentPane().add(bottomPanel, BorderLayout.SOUTH);
this.setSize(MAX_WIDTH, MAX_HEIGHT);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private void registerEventListeners() {
loadBtn.addActionListener(this);
saveBtn.addActionListener(this);
solveBtn.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
}
}
Image:

Drawing & Changing an Image within a JPanel (from another class)

So yeah, I'm trying to write a rock paper scissors program with images, but I'm having trouble at putting in the pictures. There seems to only be a small white square on the JPanel.
Here's the code for my Main Class:
public class Practice extends JPanel implements ActionListener {
// Images
final ImageIcon rockPic = new ImageIcon("D:\\JOVO\\RPS\\src\\rock.png");
final ImageIcon paperPic = new ImageIcon("D:\\JOVO\\RPS\\src\\paper.png");
// variables
String results = null;
Image pic = null;
//Image Panel
ImagePanel youPic = new ImagePanel();
public Practice() {
// FRAME
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("RPS");
frame.setLayout(new BorderLayout());
frame.setVisible(true);
// SUPER JPANEL
setLayout(new GridBagLayout());
setBackground(Color.WHITE);
GridBagConstraints c = new GridBagConstraints();
frame.add(this);
// JPANELS - RPS title, JButton, PICTURES
JPanel rpsTitle = new JPanel();
c.fill = GridBagConstraints.CENTER;
c.gridy = 0;
c.gridx = 1;
rpsTitle.setBackground(Color.WHITE);
add(rpsTitle, c);
JPanel jbuts = new JPanel();
c.gridy = 3;
jbuts.setLayout(new BorderLayout());
add(jbuts, c);
// ImagePanels
c.gridy = 2;
c.gridx = 0;
add(youPic,c);
c.gridy = 0;
c.gridx = 1;
// JBUTTONS
JButton rock = new JButton(" Rock ");
JButton paper = new JButton(" Paper ");
JButton scissors = new JButton("Scissors");
jbuts.add(rock, BorderLayout.WEST);
jbuts.add(paper, BorderLayout.CENTER);
jbuts.add(scissors, BorderLayout.EAST);
paper.addActionListener(this);
JButton resultB = new JButton("RES");
c.gridy = 2;
c.weighty = 10;
add(resultB, c);
c.weighty = 0;
// FONTS
Font rps = new Font(null, Font.BOLD, 28);
Font labels = new Font(null, Font.ITALIC, 18);
// JLABELS
JLabel rpsTit = new JLabel("Rock, Paper, Scissors");
rpsTit.setFont(rps);
rpsTitle.add(rpsTit);
JLabel you = new JLabel("You: ");
you.setFont(labels);
JLabel com = new JLabel("Com: ");
com.setFont(labels);
c.gridy = 1;
c.gridx = 0;
add(you, c);
c.gridx = 2;
add(com, c);
// PICTURES
// comPic.setPic(rockPic);
// youPic.setPic(pic);
}
public String test(String name) {
// test the game
return results;
}
public static void main(String[] args) {
// main stuffs
Practice prac = new Practice();
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Works");
ImageIcon pic = null;
pic = paperPic;
youPic.setPic(pic);
}
}
And my other class:
public class ImagePanel extends JPanel {
Image pic = null;
public ImagePanel() {
repaint();
}
public void setPic(ImageIcon pic) {
// set picture for painting
this.pic = pic.getImage();
System.out.println("Set Pic");
repaint();
}
public void paintComponent(Graphics g) {
// paint
super.paintComponent(g);
System.out.println("Painting");
g.drawImage(pic, 0, 0, this);
}
}
Thanks in advance! :)

Controlling multiple JPanels visibility in a JFrame

So what I want to do is create multiple jpanels of the different menus that i need. At first, only the visibility of the main menu is set to true. Then depending on which buttons I click, i can go to different menus by turning off the visibility of the previous menu and turning on the visibility of the current one. It's not working though, and I think it's with the way I add my JPanels. Any quick fix to this?
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.opencsv.CSVWriter;
public class justawindow extends JFrame{
public static void main (String[] args){
justawindow w = new justawindow();
}
//JPanels
JPanel mainMenu = new JPanel();
JPanel createUserMenu = new JPanel();
JPanel loginUserMenu = new JPanel();
//mainMenu stuff
JButton createMenu = new JButton("Create new user");
JButton loginMenu = new JButton("Login");
//createUserMenu stuff
JTextField username = new JTextField(15);
JPasswordField password = new JPasswordField(15);
JTextField realname = new JTextField(15);
JButton backFromCreateUser = new JButton("Back");
JButton createUser = new JButton("Create new user");
JLabel usernameLabel = new JLabel("Username:");
JLabel passwordLabel = new JLabel("Password:");
JLabel realnameLabel = new JLabel("Real name:");
JButton login = new JButton("Login");
JButton backFromLoginUser = new JButton ("Back");
public justawindow(){
createMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
mainMenu.setVisible(false);
createUserMenu.setVisible(true);
}
});
loginMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
mainMenu.setVisible(false);
loginUserMenu.setVisible(true);
}
});
GridBagLayout g1 = new GridBagLayout();
GridBagConstraints c1 = new GridBagConstraints();
mainMenu.setLayout(g1);
c1.gridx = 0;
c1.gridy = 0;
mainMenu.add(createMenu,c1);
c1.gridy = 1;
mainMenu.add(loginMenu,c1);
mainMenu.setVisible(true);
GridBagLayout g2 = new GridBagLayout();
GridBagConstraints c2 = new GridBagConstraints();
createUserMenu.setLayout(g2);
c2.gridx = 0;
c2.gridy = 0;
createUserMenu.add(usernameLabel,c2);
c2.gridx = 1;
createUserMenu.add(username,c2);
c2.gridx = 0;
c2.gridy = 1;
createUserMenu.add(passwordLabel,c2);
c2.gridx = 1;
createUserMenu.add(password,c2);
c2.gridx = 0;
c2.gridy = 2;
createUserMenu.add(realnameLabel,c2);
c2.gridx = 1;
createUserMenu.add(realname,c2);
c2.gridx = 0;
c2.gridy = 3;
createUserMenu.add(createUser,c2);
c2.gridy = 4;
createUserMenu.add(backFromCreateUser,c2);
createUserMenu.setVisible(false);
GridBagLayout g3 = new GridBagLayout();
GridBagConstraints c3 = new GridBagConstraints();
loginUserMenu.setLayout(g3);
c3.gridx = 0;
c3.gridy = 0;
loginUserMenu.add(usernameLabel,c3);
c3.gridx = 1;
loginUserMenu.add(username,c3);
c3.gridx = 0;
c3.gridy = 1;
loginUserMenu.add(passwordLabel,c3);
c3.gridx = 1;
loginUserMenu.add(password,c3);
c3.gridx = 0;
c3.gridy = 2;
loginUserMenu.add(login,c3);
c3.gridy = 3;
loginUserMenu.add(backFromLoginUser,c3);
loginUserMenu.setVisible(false);
this.add(createUserMenu);
this.add(loginUserMenu);
this.add(mainMenu);
setSize(400,500);
setTitle("Bomberman");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
The following code may help you
JPanel mainMenu = new JPanel();
createUserMenu.setVisible(false);
JPanel createUserMenu = new JPanel();
createUserMenu.setVisible(false);
JPanel loginUserMenu = new JPanel();
loginUserMenu.setVisible(false);
ActionPerformed
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn1)
{
mainMenu.setVisible(true);
createUserMenu.setVisible(false);
loginUserMenu.setVisible(false);
}
else if(e.getSource()==btn2)
{
mainMenu.setVisible(false);
createUserMenu.setVisible(true);
loginUserMenu.setVisible(false);
}
else if(e.getSource()==btn3)
{
mainMenu.setVisible(false);
createUserMenu.setVisible(false);
loginUserMenu.setVisible(true);
}
}
This is not a complete solution, Just an Example
Using arrayLists
components1 = new ArrayList<Component>();
components1.add(label11);
components1.add(label12);
to set visibility
panel1.setVisible(true);
for (Component component1 : components1) {
component1.setVisible(true);
}
panel2.setVisible(false);
for (Component component2 : components2) {
component2.setVisible(false);
}
panel3.setVisible(false);
for (Component component : components3) {
component.setVisible(false);
}

JComponent Building for Sudoku Board

I am trying to build a sudoku board. For now i am just trying to get the board to draw, i have tried just drawing lines but was told this was better....i have not gotten this to work as of yet. any hints on what i am doing wrong
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.*;
public class SudokuView4 extends JPanel {
int rows = 3;
int col = 3;
public JPanel container = new JPanel(new GridLayout(rows*col,rows*col));
public SudokuView4(SudokuBase sb) {
// TODO Auto-generated constructor stub
for(int r = 0; r < rows; r++){
for(int c = 0; c < col; c++){
//container.add(Region(rows,col));
//add(build);
//build.setSize(50, 50)
Region();
container.setVisible(true);
}
}
}
//class Region extends JPanel {
public void Region( ) {
//setLayout(new GridLayout(3,3));
//JPanel grid = new JPanel(new GridLayout(3,3));
//grid.setSize(50, 50);
for(int r1 = 0; r1 < rows; r1++){
for(int c1 = 0; c1 < col; c1++){
//JPanel grid = new JPanel();
JButton build = new JButton();
container.add(build);
//container.setVisible(true);
}
}
}
}
The JPanel container needs to be placed inside of a JFrame and updated once the buttons are added. This is if your goal is to run a Java application.
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.*;
public class SudokuView4 extends JPanel {
int rows = 3;
int col = 3;
public JPanel container = new JPanel(new GridLayout(rows*col,rows*col));
// added main for testing
public static void main(String [] args){
SudokuView4 sudoku = new SudokuView4();
}
public SudokuView4(/*SudokuBase sb*/) {
// TODO Auto-generated constructor stub
JFrame frame = new JFrame();
frame.add(container);
for(int r = 0; r < rows; r++){
for(int c = 0; c < col; c++){
//container.add(Region(rows,col));
//add(build);
//build.setSize(50, 50)
Region();
container.setVisible(true);
}
}
frame.setSize(300,300);
frame.setVisible(true);
}
//class Region extends JPanel {
public void Region( ) {
//setLayout(new GridLayout(3,3));
//JPanel grid = new JPanel(new GridLayout(3,3));
//grid.setSize(50, 50);
for(int r1 = 0; r1 < rows; r1++){
for(int c1 = 0; c1 < col; c1++){
//JPanel grid = new JPanel();
JButton build = new JButton();
container.add(build);
//container.setVisible(true);
}
}
}
}

Categories