Results 1 to 6 of 6
  1. #1
    stuffses is offline Member
    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0

    Default Simple shape class

    I posted this before, but after being answered, I still had some problems, and then my post was lost to the void of forums.



    My goal is to have an object oriented system for easily creating shapes.
    I followed This tutorial, but I am still confused on how an individual object should create a shape. Here are my classes:

    Main class:
    Java Code:
    package main;
    
    import javax.swing.SwingUtilities;
    import javax.swing.JFrame;
    
    public class Main {
    	Player myPlayer = new Player(500,500); //create player
    	
        public static void main(String[] args) {
    
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    
        private static void createAndShowGUI() {
            System.out.println("Created GUI on EDT? "+
            SwingUtilities.isEventDispatchThread());
            
            JFrame f = new JFrame("Testing shapes!");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            f.add(new RectCreate());
            f.setSize(1200,800);
            f.setVisible(true);
        } 
    
    }

    Drawing Class:
    Java Code:
    package main;
    
    import javax.swing.JPanel;
    import java.awt.Graphics; 
    
    
    class RectCreate extends JPanel {
    	//The function for creating rects goes here:
    	
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.fillRect(100,100,100,100);
        }
    }

    Player class (The one that should call a draw command):
    Java Code:
    package main;
    
    public class Player {
    	int x;
    	int y;
    	int w;
    	int h;
    	
    	Player(int px, int py) {
    		x = px;
    		y = py;
    		w = 30;
    		h = 30;
    	}
    	
    	void draw() {
    		//Here is what I want to do:
    		//RectCreate.rect(x,y,w,h);
    	}
    }

    I have a few ideas, but I don't know how to implement them:
    -When a createRect method is called, a static method draws the shape. This repeats every frame.
    -The RectCreator class is called, it adds a vector of the wanted shape to an ArrayList, and once all objects have requested their shapes, the class draws them all at once. This repeats every frame.
    -Each class that needs a shape drawn has it's own code for drawing, and doesn't rely on a shape class to draw.

    Currently in my code, there is only a RectCreate class. If I find a good way of drawing, I will add ellipse, line, and other shape classes.
    So how do I do this?

    Edit: Here is the original post.
    Last edited by stuffses; 02-24-2012 at 09:08 PM.

  2. #2
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: Simple shape class

    Make the players draw themselves by passing the graphics object from paintComponent .

  3. #3
    stuffses is offline Member
    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0

    Default Re: Simple shape class

    So the player class should extend JPanel and use the PaintComponent(Graphics g) method to draw it's own shapes? If I do that, the Player class has to be added to a JFrame object (you can see in my Main class on line 25 (in the first post)), and then to other classes can access the Player class (for collision detection, movement, and others). I tried the following, but got the error: "Cannot make a static reference to the non-static field myPlayer"


    Main Class:
    Java Code:
    package main;
    
    import javax.swing.SwingUtilities;
    import javax.swing.JFrame;
    
    public class Main {
    	Player myPlayer = new Player(); //player is initilized here, instead of in the f.add() method
    	
        public static void main(String[] args) {
    
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    
        private static void createAndShowGUI() {
            System.out.println("Created GUI on EDT? "+
            SwingUtilities.isEventDispatchThread());
            
            JFrame f = new JFrame("Testing shapes!");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            f.add(myPlayer); //add the player to  the JFrame object
            f.setSize(1000,600);
            f.setVisible(true);
        } 
    
    }


    And the Player class:
    Java Code:
    package main;
    
    import javax.swing.JPanel;
    import java.awt.Graphics; 
    
    public class Player extends JPanel{
    	int x;
    	int y;
    	int w;
    	int h;
    	
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
            g.fillRect(100,100,100,100);
        }
    }

    Note: This code works fine if the Player class is initialized within the f.add() method, but then no other class can access it.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Simple shape class

    got the error: "Cannot make a static reference to the non-static field myPlayer"
    To get rid of this problem, Move most of the code in the static createAndShowGUI method into the constructor for the Main class.

  5. #5
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: Simple shape class

    No I did not mean it like that ... You have one JPanel ... This JPanel has the paintComponent. From the paintComponent you pass the graphics object to each player object you want to paint ex playerObject1.draw(g) then in the player.draw method you draw what you want.

    Or you just draw all of the players directly inn the paintComponent ;)

  6. #6
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: Simple shape class

    player should not extend JPanel if you don't want it to be a JPanel .

Similar Threads

  1. Simple Shape Recognition Java Program
    By TheShadow in forum New To Java
    Replies: 6
    Last Post: 07-31-2013, 10:25 PM
  2. Display shape drawn from another class
    By jj007 in forum Java 2D
    Replies: 5
    Last Post: 01-15-2012, 02:39 AM
  3. A Shape Class
    By GoodApollo in forum AWT / Swing
    Replies: 7
    Last Post: 06-20-2011, 02:13 PM
  4. Simple Tic Tac Toe Class
    By daendoonge in forum AWT / Swing
    Replies: 1
    Last Post: 01-29-2011, 08:19 PM
  5. Replies: 1
    Last Post: 04-02-2010, 01:48 PM

Tags for this Thread

Posting Permissions

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