1

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

img

This is to the gamePiece:

img

Thanks again to anyone that could help me! :)

Spektre
  • 49,595
  • 11
  • 110
  • 380
Forrest Y
  • 11
  • 2
  • I dont know the game nor code in JAVA but in case you do not find any lib/api for it see [Does anyone know of a low level (no frameworks) example of a drag & drop, re-order-able list?](https://stackoverflow.com/a/20924609/2521214) which is how it is done on your own... (if you want to select/add/del and move stuff on the screen by mouse or what ever) – Spektre Jan 16 '18 at 09:10

0 Answers0