Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jim829

Thread: Program will not paint object

  1. #1
    danbwater is offline Member
    Join Date
    Oct 2013
    Location
    Arkansas
    Posts
    10
    Rep Power
    0

    Default Program will not paint object

    I receive the pop up window for the game, but the tic tac toe game is not showing up. There are no errors in eclipse showing. Please help me figure out what is wrong, and why the program willl not paint the object?
    Thanks for your time and effort. Danny
    Main

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    import Games.Board.Board;
    import Games.Board.Cell;
    import Games.Board.Mark;
    public class TicTacToeGUIGame extends JFrame {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    	
    	SwingUtilities.invokeLater( new Runnable () {
    		public void run() { new TicTacToeGUIGame();}
    	});
    	}
    	private Board gb; 
    	private int turn;
    	
    	private void takeTurn(Cell c) {
    	Mark curMark = (turn++ % 2 == 0)? Mark.NOUGHT: Mark.CROSS;
    	gb.setCell(curMark, c.getRow(), c.getColumn());
    	}
    	private TicTacToeGUIGame() {
    		gb = new Board(3, 3, new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				Cell c = (Cell) ae.getSource();
    				takeTurn(c);
    			}
    			
    		});
    	
    		this.add(gb);
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		this.setTitle("TIC-TAC-TOE");
    		this.setSize(300, 300);
    		this.setVisible(true);
    	}
    }
    Cell

    Java Code:
    package Games.Board;
    
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JButton;
    import javax.swing.event.AncestorListener;
    
    public class Cell extends JButton{/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    private Mark content;
    private final int row, column;
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    	}
    
    public Cell(int row, int column) { 
    	this.row = row;
        this.column = column; 
    content = Mark.EMPTY;
    }
    public Mark getContent() { return content; }
    
    public void setContent(Mark content) { this.content = content; } 
    public int getRow() { return row; }
    public int getColumn() { return column; }
    
    public void addActionListener(AncestorListener ancestorListener) {
    	// TODO Auto-generated method stub
    	
    }
    
    public void paintComponent(Graphics g) {
    	//paint the basic button first
    	super.paintComponent(g);
    	int offset = 5;
    	Graphics2D g2 = (Graphics2D) g;
    	g2.setStroke(new BasicStroke(5));
    	switch(content) {
    	case NOUGHT:
    	g2.setColor(Color.RED);
    	//Draw 0
    	g2.drawOval(offset, offset, this.getWidth() - offset * 2, this.getHeight() - offset * 2);
    	break;
    	case CROSS:
    	g2.setColor(Color.BLACK);
    	//Draw X
    	g2.drawLine(offset, offset, this.getWidth() - offset ,this.getHeight() - offset);
    			g2.drawLine(this.getWidth() - offset, offset , offset, this.getHeight()- offset);
    			break;
    	case BLUE:
    		break;
    	case EMPTY:
    		break;
    	case GREEN:
    		break;
    	case MAGENTA:
    		break;
    	case ORANGE:
    		break;
    	case RED:
    		break;
    	case YELLOW:
    		break;
    	default:
    		break;
    	
    	
    			}
    }
    }
    Board

    Java Code:
    package Games.Board;
    
    import java.awt.GridLayout;
    import java.awt.event.ActionListener;
    
    import javax.swing.JPanel;
    import javax.swing.event.AncestorListener;
    
    
    
    
    public class Board extends JPanel {
    /**
        *
        */
       private static final long serialVersionUID = 1L;
           private Cell[][] cells;
    
    int[] ints = new int[3];
    
    public void  getOutOfBoundEx() {
    
    ints[2] = 16;
    ints[3] = 17; //THIS WILL CAUSE THE ERROR as element [3] doesn't not exist
    
    }
    public Board(int rows, int columns, AncestorListener ancestorListener) {
       cells = new Cell[rows][columns];
       this.setLayout(new GridLayout());
       for( int r = 0; r < cells.length; r++ ) {
       for (int c = 0; c < cells[r].length; c++) {
       cells[r][c] = new Cell(r,c);
       this.add(cells[r][c]);
       cells[r][c].addActionListener(ancestorListener);
       }
       }
       }
    public Board(int rows, int columns, ActionListener actionListener) {
    	// TODO Auto-generated constructor stub
    }
    private void add(Cell cell) {
    	   // TODO Auto-generated method stub
    	    
    	}
    public void setCell(Mark mark, int row, int column) throws
    IllegalArgumentException {
    if (cells[row][column].getContent() == Mark.EMPTY)
    cells[row][column].setContent(mark);
    else throw new IllegalArgumentException("Player already there!");
    }
    public Cell getCell(int row, int column) {
    return cells[row][column];
    }
           @Override
           public String toString() {
    StringBuilder str = new StringBuilder();
               for (Cell[] cell : cells) {
                   str.append("|");
       for (Cell cell1 : cell) {
           switch (cell1.getContent()) {
               case NOUGHT:
                   str.append("O");
                   break;
           case CROSS:
               str.append("X");
               break;
           case YELLOW:
               str.append("Y");
               break;
           case RED:
               str.append("R");
               break;
           case BLUE:
               str.append("B");
               break;
           case GREEN:
               str.append("G");
               break;
           case MAGENTA:
               str.append("M");
               break;
           case ORANGE:
               str.append("M");
               break;
           default: //Empty
               str.append("");
           }
           str.append("|");
       }
                   str.append("\n");
               }
       return str.toString();
    
    }
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,950
    Rep Power
    4

    Default Re: Program will not paint object

    Could be a number of things. I don't see where you called pack(). Could be a layout problem. I don't see any calls to repaint(). I am not familiar or have access to your imported packages so I can't offer much help.

    Regards,
    Jim
    gimbal2 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,108
    Rep Power
    4

    Default Re: Program will not paint object

    Second thing to try is to give the board a preferred size.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Paint program
    By aarti in forum AWT / Swing
    Replies: 14
    Last Post: 05-06-2011, 06:55 AM
  2. Help with paint program
    By michcan in forum Java 2D
    Replies: 1
    Last Post: 02-04-2011, 06:26 AM
  3. Paint Program Help
    By ngiannini in forum AWT / Swing
    Replies: 12
    Last Post: 05-10-2010, 04:24 PM
  4. How to make a MouseClick paint an object
    By Devilsfutbol17 in forum New To Java
    Replies: 6
    Last Post: 06-05-2008, 12:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •