Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: GCanvas in ACM Graphics Not Working Correctly

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Thanked 1 Time in 1 Post

    Question GCanvas in ACM Graphics Not Working Correctly

    I am trying to implement a game using the ACM graphics. For the game, I am trying to make the main frame a Grid of Cells.

    I made a Grid class which extends GCanvas:

            import acm.graphics.GCanvas;
            public class Grid extends GCanvas{
    	private final static int WIDTH = 300;
    	private final static int HEIGHT = 300;
    	private final int DIMENSION = 5;
    	Cell[][] grid;
    	public Grid(){
    		setSize(WIDTH, HEIGHT);
    		this.setLocation(50, 50);
    		grid = new Cell[DIMENSION][DIMENSION];
    		int dimension = Cell.getDimension();
    		int rWidth = dimension;
    	    int rHeight = dimension;
    	    for (int i=0; i<DIMENSION; i++) {
    	        for (int j=0; j<DIMENSION; j++) {
    	            grid[i][i] = new Cell();
    	            add(grid[i][i], rWidth+dimension, rHeight+dimension);
    	        rWidth = dimension;

    I made a Cell class which extends GRect:

            import java.awt.Color;
            import acm.graphics.GRect;
            public class Cell extends GRect{
    	private final static int WIDTH =50;
    	private final static int HEIGHT = 50;
    	public Cell(){
    		super(WIDTH, HEIGHT);
    	public static int getDimension(){
    		return WIDTH;

    I was able to make the Grid be a grid of Cells (a grid of colored GRects).

    However, when I try to add a Grid to my main frame in the run() method, the Grid is added so that the left side of it is about in the middle of the frame.

    Here is the code for my main frame:

             public class Game extends GraphicsProgram{
    	private static final long serialVersionUID = 1L;
    	public static final int WIDTH = 160;
    	public static final int HEIGHT = WIDTH/12 *9;
    	public static final int SCALE = 5;
    	public static final String NAME = "Game";
    	public void run(){
    		Grid grid = new Grid();

    So, keeping in mind that a Grid is basically a GCanvas, I tried to just add a GCanvas to the main frame instead of a Grid. Unfortunately, I received the same problem.

    Does anyone know why the GCanvas will only add to the middle of the main frame?
    I tried doing something like:

             GCanvas canvas = new GCanvas();

    But that did not change anything.

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 698 Times in 680 Posts

    Default Re: GCanvas in ACM Graphics Not Working Correctly

    Perhaps it has a default layout and a specific part of that layout is where items are placed by default, similar to BorderLayout and JFrame.

    A few ACM questions pass through here, but I don't believe there are any resident ACM 'experts.' Some believe the ACM approach simplifies or accelerates a student's understanding of Java graphical programming, but I haven't seen evidence that it is used outside the academic environment. You should consider making the transition to Swing. If you find that transition difficult, you should give that feedback to those who chose to teach you the ACM way.

    Good luck!

Similar Threads

  1. Replies: 1
    Last Post: March 18th, 2014, 02:56 AM
  2. My text input file is not working correctly?
    By MLeclerc182 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 17th, 2012, 08:34 PM
  3. My code is not working correctly
    By shavek911 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 18th, 2012, 05:49 PM
  4. [SOLVED] For Loop not working correctly.
    By chrisob in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 15th, 2012, 03:50 PM
  5. Hey guys, my While loop isn't working correctly.
    By metanoia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 26th, 2011, 11:15 PM

Tags for this Thread