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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 3 of 3

Thread: ACM and another class

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ACM and another class

    I'm curious why ACM isn't letting me create an object from a different class, maybe I am missing something?

    my main class:
    import java.awt.Color;
    import acm.program.*;
    import acm.graphics.*;
     
    public class MyClass extends GraphicsProgram {
    	private static final int WINDOW_W = 500;
    	private static final int WINDOW_H = 500;
     
    	public void run() {
    		setSize(WINDOW_W, WINDOW_H);
    		//setBackground(Color.BLACK);
     
    		Player p;
    		p = new Player();
    		p.createPlayer();
    	}
    }

    and my class to create an object:
    import acm.graphics.*;
    import acm.program.*;
    import java.awt.Color;
     
    public class Player extends GraphicsProgram {
    	private static final double STARTX = -50;
    	private static final double STARTY = -50;
    	private static final double PLAYER_W = 100;
    	private static final double PLAYER_H = 100;
     
    	public void createPlayer() {
    		GRect player = new GRect(PLAYER_W, PLAYER_H);
    		player.setFillColor(Color.RED);
    		player.setFilled(true);
    		add(player);
    	}
    }

    Any help is appreciated. I have also noticed when trying to extend 'ConsoleProgram' it does the same thing with line printing (except when using system.out) so i'm not sure what is going on


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,292
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: ACM and another class

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

    ACM experience is rare here, although we've gotten 3 or 4 ACM questions in the last 6 months. It seems few people use ACM after they have to, and it's not clear that ACM as a learning tool has any benefit or whether its use encourages or discourages users to continue programming beyond the academic environment.

    Perhaps your question can be generalized to Java, but we'll need to see the specific error message. What does, "ACM isn't letting me create an object from a different class," mean? If you're getting an error, post that exactly as it appears at your end. Perhaps from that we can help.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ACM and another class

    Well it's not giving me an error, but it's just not creating the graphics object.

    The first code is my main program (where the run() method is). At the bottom of it I define 'p' to create my object from my other class (the second code) and it's not appearing.

    It all works fine if I create my graphics from the main class, but I thought doing everything from the main class is bad style. I don't think I'm referencing my second class wrong, so idk what to change

    Edit: is there a more common graphics library I should be using to get familiar with using? I'm still learning java so I don't want to get really experienced with a library nobody uses anymore
    Last edited by unknown21; April 17th, 2014 at 03:38 PM.

Similar Threads

  1. GCanvas in ACM Graphics Not Working Correctly
    By Midgar77 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2014, 03:10 PM
  2. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 09:03 PM
  3. Replies: 3
    Last Post: June 17th, 2012, 07:22 PM
  4. [SOLVED] acm.util.ErrorException: Cannot find image named
    By srs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2010, 10:16 PM
  5. [SOLVED] Finding acm package
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 12th, 2010, 12:50 PM