I'm currently doing a Connect Four game and I would appreciate any help with adding and moving the Game pieces. So I made the game piece into an image and want to add it to the game board which is a JLabel picture and then move the game piece on Y-axis.
here's my code:
import javax.swing.*;
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Graphics;
public class GraphicsConnectFour extends JPanel
{
public static void main(String[] args)throws Exception
{
JFrame frame = new JFrame("ConnectFour");
frame.setPreferredSize (new Dimension (970, 700));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsConnectFour connectFour = new GraphicsConnectFour();
frame.add(connectFour);
frame.add(new gameArea());
frame.pack();
frame.setVisible(true);
}
}
class gameArea extends JPanel
{
Graphics g;
Image redPiece;
ImageIcon red;
JPanel gamePanel = new JPanel();
JLabel gameBackground = new JLabel(new ImageIcon("gameBackground.png"));
public void gamePanel (Graphics g, int xPos, int yPos)
{
gamePanel.setLayout(new OverlayLayout(gamePanel));
gamePanel.add(gameBackground);
add(gamePanel);
}
public void GraphicsConnectFour ()
{
red= new ImageIcon("red.png");
redPiece=red.getImage();
}
public void paintComponent(Graphics g)
{
repaint();
super.paintComponent(g);
GraphicsConnectFour();
gamePanel(g, 200, 200);
}
}
And this is the gameBoard image
This is to the gamePiece:
Thanks again to anyone that could help me! :)