Creating simple TicTacToe GUI game - java

I am trying to create a simple Tic-Tac-Toe game using GUI. I have one button, which shows the player's turn (when pressed the button itself changes its text to 0 or 1 (drawing X or O)).
However I am stuck with a problem here, I have no idea why my test within the MouseListener is applied to every single Jcomponent object (my class TicTacToe extends it) and all of the 9 Panels, which are within GridLayout(3,3) are filled with the same drawing...
Here is my TicTacToe class
public class TicTacToe extends JComponent{
private int ovalOrX = 0;
public TicTacToe(){
super.setPreferredSize(new Dimension(300,300));
}
public void paintComponent(Graphics g){
g.setColor(Color.BLACK);
g.drawLine(0, 0, getWidth(), 0);//top line
g.drawLine(0, 0, 0, getHeight());//left line
g.drawLine(0, 300, getWidth(), getHeight());//botton line
g.drawLine(getWidth(),getHeight(), getWidth(), 0);//right line
if(ovalOrX == 1){
g.setColor(Color.RED);
g.drawLine(0,0,100,100);
g.drawLine(0,100,100,0);
}
if(ovalOrX == 2){
g.setColor(Color.BLUE);
g.drawOval(0,0,100,100);
}
}
public void drawX(){
ovalOrX = 1;
repaint();
}
public void drawCircle(){
ovalOrX = 2;
repaint();
}
}
And here is my JFrame class:
public class TicTacToeFrame extends JFrame {
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 300;
private JPanel mainPanel;
private TicTacToe scene;
private TicTacToe s00;
private TicTacToe s01;
private TicTacToe s02;
//finish top raw
private TicTacToe s10;
private TicTacToe s11;
private TicTacToe s12;
//finish middle raw
private TicTacToe s20;
private TicTacToe s21;
private TicTacToe s22;
private int buttonInt = 0;
private JButton OX;
class AddButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
if(buttonInt == 0){
buttonInt++;
}
else if(buttonInt == 1){
buttonInt = 0;
}
OX.setText("Player: " + buttonInt);
}
}
class MyMouseListener extends MouseAdapter{
public void mouseClicked(MouseEvent event){
TicTacToe[] myPanelArray = {s00,s01,s02,s10,s11,s12,s20,s21,s22};
if(buttonInt == 0){
for(int i = 0; i < myPanelArray.length;i++){
if(myPanelArray[i].contains(event.getPoint())){
myPanelArray[i].drawCircle();
}
}
}
}
}
public TicTacToeFrame(){
scene = new TicTacToe();
scene.setPreferredSize(new Dimension(300,300));
add(scene,BorderLayout.CENTER);
OX = new JButton("Player: " + buttonInt);
OX.addActionListener(new AddButtonListener());
add(OX,BorderLayout.NORTH);
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(3,3));
fillScene();
add(mainPanel,BorderLayout.CENTER);
setSize(FRAME_WIDTH,FRAME_HEIGHT);
}
private void fillScene(){
s00 = new TicTacToe();
s00.addMouseListener(new MyMouseListener());
s01 = new TicTacToe();
//s01.addMouseListener(new MyMouseListener());
s02 = new TicTacToe();
s10 = new TicTacToe();
s11 = new TicTacToe();
s12 = new TicTacToe();
s20 = new TicTacToe();
s21 = new TicTacToe();
s22 = new TicTacToe();
//finish bottom raw
mainPanel.add(s00);
mainPanel.add(s01);
mainPanel.add(s02);
mainPanel.add(s10);
mainPanel.add(s11);
mainPanel.add(s12);
mainPanel.add(s20);
mainPanel.add(s21);
mainPanel.add(s22);
}
}

The return value of event.getPoint() is relative to the source component, a TicTacToe in this case, no the entire frame. Therefore, it will always be true for all the TictacToe's since they are all the same size.
Istead, use the Event.getSource() to determine the source object.
Also, it advisable to put all you TicTacToe objects into a TicTacToe[][] instead of listed separately.

Related

Making a character land on a platform

Im trying to code a simple 2D game. I have a Square that you can move with a/w/d. I got jumping working with a sort of gravity but now I cant get myself to land on a platform. I know how to detect collision between two things but even still idk what to do. My gravity always pulls me down until I reach groundLevel which is part of the problem. Here is my code so far. There's a lot of experimenting happing so its pretty messy.
import javax.swing.Timer;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
/**
* Custom Graphics Example: Using key/button to move a line left or right.
*/
public class PlatformingGame extends JFrame implements ActionListener, KeyListener{
// Name-constants for the various dimensions
public static final int CANVAS_WIDTH = 800;
public static final int CANVAS_HEIGHT = 400;
public static final Color LINE_COLOR = Color.BLACK;
public static final Color CANVAS_BACKGROUND = Color.WHITE;
public int GRAVITY = 10;
public int TERMINAL_VELOCITY = 300;
public int vertical_speed = 0;
public int jumpSpeed;
public int groundLevel = CANVAS_HEIGHT;
int Shapes;
private DrawCanvas canvas; // the custom drawing canvas (extends JPanel)
public enum STATE {
PLAYING,
PAUSED,
ONGROUND,
INAIR
};
JButton btnStartRestat, btnExit;
Timer timer;
Rectangle2D.Double guy, platform, platform2;
Shape[] shapeArr = new Shape[10];
int dx, dy;
/** Constructor to set up the GUI */
ArrayList myKeys = new ArrayList<Character>();
public static STATE gameState = STATE.PAUSED;
//public static STATE playerState = STATE.ONGROUND;
public PlatformingGame() {
dx = 3;
dy = 3;
guy = new Rectangle2D.Double( CANVAS_WIDTH/2 - 20, CANVAS_HEIGHT, 30, 20);
platform2 = new Rectangle2D.Double( CANVAS_WIDTH/4, CANVAS_HEIGHT/2+130, 50, 10);
platform = new Rectangle2D.Double( CANVAS_WIDTH/3, CANVAS_HEIGHT/2+50, 50, 10);
timer = new Timer(10, this);
// Set up a panel for the buttons
JPanel btnPanel = new JPanel();
// btnPanel.setPreferredSize(new Dimension(CANVAS_WIDTH/2, CANVAS_HEIGHT));
btnPanel.setLayout(new FlowLayout());
btnStartRestat = new JButton("Start/Restart");
btnExit = new JButton("Exit");
btnPanel.add(btnStartRestat);
btnPanel.add(btnExit);
btnStartRestat.addActionListener(this);
btnExit.addActionListener(this);
// Set up a custom drawing JPanel
canvas = new DrawCanvas();
canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
// Add both panels to this JFrame
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(canvas, BorderLayout.CENTER);
cp.add(btnPanel, BorderLayout.SOUTH);
// "this" JFrame fires KeyEvent
addKeyListener(this);
requestFocus(); // set the focus to JFrame to receive KeyEvent
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Handle the CLOSE button
pack(); // pack all the components in the JFrame
setVisible(true); // show it
}
public void generateSpikes(){
Rectangle2D.Double spikes = null;
for (int i = 0; i < 10; i++) {
spikes = new Rectangle2D.Double (CANVAS_WIDTH - 300 + i*20 , CANVAS_HEIGHT - 30 , 20, 30);
shapeArr[i] = spikes;
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==btnStartRestat)
{
generateSpikes();
dx = 3;
dy = 3;
guy = new Rectangle2D.Double( 100, CANVAS_HEIGHT - guy.height, 15, 30);
platform = new Rectangle2D.Double( CANVAS_WIDTH/3, CANVAS_HEIGHT/2+50, 50, 10);
platform2 = new Rectangle2D.Double( CANVAS_WIDTH/4, CANVAS_HEIGHT/2+130, 50, 10);
gameState = STATE.PLAYING;
}
else if (e.getSource()==btnExit)
{
// requestFocus(); // change the focus to JFrame to receive KeyEvent
}
else if (e.getSource()== timer){
if (isHitDetected(platform2, guy)){
// playerState = STATE.ONGROUND;
guy.y = platform2.y;
}
if (myKeys.contains('a')
&& (guy.x > 0)){
guy.x = guy.x - 5; }
if (myKeys.contains('d')
&& (guy.x < CANVAS_WIDTH - guy.width)){
guy.x = guy.x + 5; }
{
updateGuyPosition();
}
requestFocus();
canvas.repaint();
}
}
public void updateGuyPosition(){
double guyHeight = guy.height;
if (guy.x >= CANVAS_WIDTH - guy.width){
}
if(gameState == STATE.PLAYING) {
guy.y += jumpSpeed;
if (guy.y < groundLevel - guyHeight) {
// if(playerState == STATE.INAIR) {
jumpSpeed += 1;
}
// }
else {
// if(playerState == STATE.INAIR) {
//playerState = STATE.ONGROUND;
jumpSpeed = 0;
guy.y = groundLevel - guyHeight;
}
// }
if (myKeys.contains('w') == true && guy.y == groundLevel - guyHeight) {
jumpSpeed = -15;
// playerState = STATE.INAIR;
}
}
}
public static boolean isHitDetected(Shape shapeA, Shape shapeB) {
Area areaA = new Area(shapeA);
areaA.intersect(new Area(shapeB));
return !areaA.isEmpty();
}
public void keyPressed(KeyEvent evt) {
if (!myKeys.contains(evt.getKeyChar())){
myKeys.add(evt.getKeyChar());
}
}
public void keyReleased(KeyEvent evt) {
myKeys.remove(myKeys.indexOf(evt.getKeyChar()));
}
public void keyTyped(KeyEvent evt) {
}
/** The entry main() method */
public static void main(String[] args) {
PlatformingGame myProg = new PlatformingGame(); // Let the constructor do the job
myProg.timer.start();
}
/**
* DrawCanvas (inner class) is a JPanel used for custom drawing
*/
class DrawCanvas extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(CANVAS_BACKGROUND);
g.setColor(LINE_COLOR);
Graphics2D g2d = (Graphics2D)g;
if(gameState == STATE.PLAYING) {
g2d.fill(guy);
g.setColor(Color.lightGray);
g2d.fill(platform);
g2d.fill(platform2);
for (int i = 0; i < 10; i++) {
g2d.fill(shapeArr[i]);
}
}
if(gameState == STATE.PAUSED) {
g.drawString("Game Paused", CANVAS_WIDTH/2, CANVAS_HEIGHT/2);
}
}
}
}

Null pointer exception

I'm fairly new to StackOverFlow. I have a years experience with Java, but I can't seem to find an the line(s) in this code that triggers the Null pointer exception. Imports are all ready done.
public class BallShooter extends JFrame{
private JFrame ballshooter;
private BallShooter bs;
private MenuPanel mp;
private MainPanel mep;
private GamePanel gp;
private Balls balls;
private CardLayout card;
private int[] leaderboard;
private boolean ballHitWall;
public BallShooter()
{
mep = new MainPanel();
mp = new MenuPanel();
gp = new GamePanel();
ballshooter = new JFrame();
ballshooter.setLocation(0, 0);
ballshooter.setSize(800, 700);
ballshooter.setDefaultCloseOperation(EXIT_ON_CLOSE);
ballshooter.setBackground(Color.GRAY);
ballshooter.setResizable(false);
ballshooter.getContentPane().add(mep);
ballshooter.setVisible(true);
card = (CardLayout)(mep.getLayout());
}
public static void main(String [] args)
{
BallShooter balls = new BallShooter();
}
class MainPanel extends JPanel
{
public MainPanel()
{
setSize(800,700);
setVisible(true);
setBackground(Color.GRAY);
setLayout(new CardLayout());
add(mp); <-- line 52
add(gp);
}
}
class MenuPanel extends JPanel implements ActionListener
{
private JButton startGame;
private JButton leaderboard;
private JButton instructions;
public MenuPanel()
{
setLayout(null);
setSize(800,700);
setBackground(Color.GRAY);
startGame = new JButton("Start the GAME.");
leaderboard = new JButton("Go to LEADERBOARD.");
instructions = new JButton("Instructions.");
startGame.addActionListener(this);
leaderboard.addActionListener(this);
instructions.addActionListener(this);
startGame.setBounds(300,100,200,150);
leaderboard.setBounds(300,250,200,150);
instructions.setBounds(300,400,200,150);
add(startGame);
add(leaderboard);
add(instructions);
}
public void actionPerformed(ActionEvent e) {
String in = e.getActionCommand();
if(in.equals("Start the GAME."))
{
card.next(mep);
}
}
}
class GamePanel extends JPanel implements ActionListener
{
private JButton stats;
private int pos1,pos2,pos3,pos4,pos5,pos6,pos7,pos8,pos9,pos10,pos11,pos12,pos13,pos14,pos15,pos16,pos17,pos18,pos19,pos20 ;
private boolean onePos,twoPos,threePos,fourPos,fivePos,sixPos,sevenPos,eightPos,ninePos,tenPos,elevenPos,twelvePos,thirteenPos,fourteenPos,fifteenPos,sixteenPos,seventeenPos,eighteenPos,ninteenPos,twentyPos = true;
private int x,y;
public GamePanel()
{
balls = new Balls();
onePos = true;
setSize(800,700);
setBackground(Color.GRAY);
setLayout(null);
if(onePos)
{
pos1 = 1;
x = 100;
y = 100;
balls.setBounds(x,y, 50,50);
gp.add(balls);
}
}
public void actionPerformed(ActionEvent e) {}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.WHITE);
g.drawRect(100, 100, 600, 500);
}
}
class Balls extends JPanel {
private int color;
private int vX, vY;
private int ballW = 50, ballH = 50;
private int x,y;
public Balls()
{
setSize(50,50);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
color = (int)(Math.random()*4+1);
if(color == 1)
{
g.setColor(Color.YELLOW);
g.drawOval(0, 0, ballW, ballH);
}
else if(color == 2)
{
g.setColor(Color.GREEN);
g.drawOval(0, 0, ballW, ballH);
}
else if(color == 3)
{
g.setColor(Color.RED);
g.drawOval(0, 0, ballW, ballH);
}
else if(color == 4)
{
g.setColor(Color.BLUE);
g.drawOval(0, 0, ballW, ballH);
}
}
}
}
I have no idea what's going on. It's just flying over my head. The error:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1097)
at java.awt.Container.add(Container.java:417)
at BallShooter$MainPanel.<init>(BallShooter.java:52)
at BallShooter.<init>(BallShooter.java:24)
at BallShooter.main(BallShooter.java:42)
If anyone could help me with this, that would be greatly appreciated.
EDIT First time posting, so there's an error with the formatting. The class header didn't go into the code section and the last bracket as well.
General rule - when you're reading a stack trace like this, look for the first line that's one of YOUR classes, as opposed to a JDK or library class. In this case, the problem can be found on line 52 of BallShooter.java.
When that line runs, mp is null, so you're trying to add a null to your container, hence the exception.
To fix this, create the other two panels first, and the MainPanel last.
You're initializing the non-static nested class MainPanel, which uses your mp and gp fields from the BallShooter class before they're initialized (see the lines which call #add in the MainPanel constructor). Try calling mep#add after you've created all the menus, or consider something along the lines of an #init method (or even a different hierarchy/design).

Try Again Button When Pressed Resets Everything But Everything Becomes Quicker

So, I had a problem concerning a Try Again button that would show on Game Over screen and when clicked on reset everything to initial value. Now that problem is partially solved but every time I click on it everything becomes crazy fast and with each click faster and faster.
Here is my code from class which does initialization:
public class game extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
private Menu menu;
private GameOver gameover;
//variables
public static int cyclesToWait = 50;
public static int enemiesKilled = 0;
public static int life = 3;
int roundCount = 0;
Timer gamelooptimer;
//objects
Player p;
ControllerEnemy c;
ControllerRocket r;
ControllerExplosion e;
//is it new round or not --> for enemy control
public static enum ROUND{
INPROGRESS,
NEW
}
public static enum STATE{
MENU,
GAME,
OVER
};
public static STATE State = STATE.MENU;
public static ROUND Round = ROUND.INPROGRESS;
public game(){
setFocusable(true);
initGame();
}
public void resetGame(){
initGame();
}
private void initGame(){
enemiesKilled = 0;
life = 3;
roundCount = 0;
gamelooptimer = new Timer(10, this);
gamelooptimer.start();
p = new Player(MainClass.width / 2 - 60, 400);
c = new ControllerEnemy();
r = new ControllerRocket();
e = new ControllerExplosion();
menu = new Menu();
gameover = new GameOver();
addKeyListener(new KeyInput(p));
addMouseListener(new MouseInput());
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(getBackgroundImage(), 0, 0, null);
Font fnt = new Font("arial", Font.BOLD, 22);
g.setFont(fnt);
if(State == STATE.GAME){
p.draw(g2d);
c.draw(g2d);
r.draw(g2d);
e.draw(g2d);
g.drawString("Round: " + roundCount, MainClass.width - 125, 22);
g.drawString("Enemies Killed: " + enemiesKilled, MainClass.width/2 - 95, 22);
if(cyclesToWait < 50)
cyclesToWait++;
}
else if (State == STATE.OVER)
gameover.render(g);
else
menu.render(g);
}
#Override
public void actionPerformed(ActionEvent e) {
repaint();
if(State == STATE.GAME){
p.update();
c.update();
r.update();
}
if(Round == ROUND.NEW)
roundCount += 1;
}
private Image getBackgroundImage(){
ImageIcon i = new ImageIcon(getClass().getResource("/images/Background.png"));
return i.getImage();
}
resetGame() is called from another class.

Java Swing Horse Simulation

I am having problem getting my horse position to update within my swing application. I have tried multiple methods to update the xPosition of the horses as they refresh across the swing panel that represents the racetrack.
public class HorseModel{
/*Horse dimensions*/
private int x;
private int y;
private final int horsePerimeter = 20;
public HorseModel(int xT, int yT){
/*Constructor*/
x = xT;
y = yT;
}
public void setDimensions(int dimX, int dimY){
/*Sets program dimensions*/
x = dimX;
y = dimY;
}
public void createHorse(Graphics2D h){
/*Paints HorseModel on screen as 2 dimensional object*/
Ellipse2D.Double horseModel = new Ellipse2D.Double(x, y, horsePerimeter, horsePerimeter);
h.setColor(Color.RED);
h.fill(horseModel);
h.setColor(Color.YELLOW);
h.draw(horseModel);
}
}
public class HorseMovement implements Runnable{
public final int xStartPos = 10; //change
public final int yStartPos = 20;
private RaceTrack hRaceTrack;
private HorseModel Horse2D;
private int xPos, yPos;
public HorseMovement(RaceTrack r, int yPos_Spacing){
/*Constructor*/
xPos = xStartPos;
yPos = yStartPos * yPos_Spacing;
Horse2D = new HorseModel(xPos, yPos);
hRaceTrack = r;
}
public HorseModel moveHorse(HorseModel horseObject){
/*Updates horse positon*/
horseObject = new HorseModel(xPos++, yPos);
return horseObject;
}
public void paintComponent(Graphics h){
/*paints the new horse after movement*/
this.Horse2D = moveHorse(Horse2D);
Graphics2D hMod = (Graphics2D) h;
Horse2D.createHorse(hMod);
}
public void run(){
/*Repaints the horse models as they increment movement across the screen*/
hRaceTrack.repaint();
hRaceTrack.revalidate();
}
}
public class RacePanel extends JFrame{
/*Frame Buttons*/
private JPanel mPanel;
private JButton startRace = new JButton("Start Race");
private JButton stopRace = new JButton("Stop Race");
private JButton startOver = new JButton("Start Over");
/*Panel to fill with HorseModels for race*/
private RaceTrack rTrack;
/*Window dimensions*/
public int Window_Height = 1024;
public int Window_Width = 768;
public RacePanel(){
/*Constructor*/
initGui();
initRace();
initQuit();
setSize(Window_Width, Window_Height);
}
public void initGui(){
/*Initializes the main race panel and sets button positions and layouts*/
mPanel = new JPanel(new BorderLayout());
rTrack = new RaceTrack();
JPanel horsePanel = new JPanel(); //panel to house horse objects before running across screen
horsePanel.setLayout(new GridLayout(1, 3));
positionJPanels(horsePanel, mPanel);
}
public void initRace(){
/*implements action listener for start race button*/
class StartRace implements ActionListener{
public void actionPerformed(ActionEvent e){
startRace.setEnabled(true);
rTrack.initTrack();
}
}
ActionListener event = new StartRace();
stopRace.addActionListener(event);
}
public void initQuit(){
/*Implements the action listener for stop race button*/
class StopRace implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);//exits program if race is stopped
}
}
ActionListener event = new StopRace();
stopRace.addActionListener(event);
}
public void positionJPanels(JPanel h, JPanel p){
/*Handles adding buttons to a JPanel*/
h.add(startRace);
h.add(startOver);
h.add(stopRace);
p.add(h, BorderLayout.NORTH); //sets the horse panel buttons to the top of the layout
p.add(rTrack, BorderLayout.CENTER); //sets
add(p);
}
}
public class RaceController {
public static void main(String[] args){
new RaceController();
}
public RaceController(){
/*Constructor*/
JFrame mFrame = new RacePanel();
mFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mFrame.setVisible(true);
}
}
public class RaceTrack extends JPanel{
/*Sets horses within race track*/
private int numOfHorseObjects = 5;// change this to a dynamic
private int numOfThreads = 25;
/*Holds horse thread from HorseObject class*/
private ArrayList<HorseMovement> horses = new ArrayList<>();
private ArrayList<Thread> threads = new ArrayList(numOfThreads);
public RaceTrack(){
/*Constructor*/
setBackground(Color.black);
reset();
}
public void initTrack(){
/*Starts the RaceTrack simulation*/
threads.clear(); //clears the thread arraylist still residing
for(int i = 0; i < horses.size(); i++){
Thread T = new Thread(horses.get(i));
T.start();
threads.add(T);
}
}
public void reset(){
/*resets horse position within screen*/
horses.clear();
for(int i = 0; i < numOfHorseObjects; i++){
horses.add(new HorseMovement(this, i + 1));
}
}
public void paintComponent(Graphics g){
/*overrides graphics paint method in order to paint the horse movements
* through the arraylist of HorseMovements*/
super.paintComponent(g);
for(HorseMovement h : horses){
h.paintComponent(g);
}
}
}
Issues with your code:
You never give the startRace JButton an ActionListener, so how is button going to have any affect, and how is the race ever going to start? Note that you're adding the StartRace ActionListener object to the stopRace JButton, and I'm guessing that this was done in error.
Even if you added that Listener to the startRace button, the action listener will only advance all the horses one "step" and no more -- there are no loops in within your background threads to perform actions repetitively.
You seem to be creating new Horse2D objects needlessly. Why not simply advance the location of the existing Horse2D object?
Myself, I'd use a single Swing Timer rather than a bunch of Threads in order to simplify the code.

Simple Java game using grid

This picture is edited with photoshop to show what I want to do:
Basically, what I want to do is to move the black-stick-man by clicking on it and move to available positions(gray boxes).
This is what I've done so far:
Arena.java
public class Arena extends JFrame{
JPanel a = new JPanel();
Player players[][] = new Player[10][10];
private ImageIcon player = new ImageIcon("player.jpg");
private ImageIcon player2 = new ImageIcon("player2.jpg");
private ImageIcon poop = new ImageIcon("poop.jpg");
private ImageIcon moves = new ImageIcon("moves.jpg");
public static void main(String args[]){
new Arena();
}
public Arena(){
setTitle("The Arena");
setSize(500,500);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
a.setLayout(new GridLayout(10,10));
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
players[i][j] = new Player();
a.add(players[i][j]);
}
}
//player position
players[9][4].setIcon(player);
//player 2 position
players[9][7].setIcon(player2);
//poop
players[7][2].setIcon(poop);
players[5][7].setIcon(poop);
add(a);
setVisible(true);
}
}
Player.java
public class Player extends JButton{
private int xPos;
private int yPos;
public Player(){
}
public int getXPos(){
return xPos;
}
public int getYPos(){
return yPos;
}
public void setXPos(){
this.xPos = x;
}
public void setYPos(){
this.yPos = y;
}
}
Thanks!

Categories

Resources