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

    Question Simple shapes class

    I'm new to JAVA, migrating over from Processing, and I looked at a few ways to draw shapes. After reading about them, I was able to make a shape, but I was wondering how I would implement the shape drawing in each class I create (Player, Enemy, Environment, ect classes).

    I read this tutorial, and I was able to run it perfectly, and understand most of the code, but I don't know how I would make it work for multiple independent objects.

    What I want is to make a static class that I can use to easily draw shapes, like this:
    Java Code:
    for (int i = 0; i < num; i++) {
       Shapes.rect(200,i*10,5,5);
    }
    It is also important that each individual class is as independent as possible.

    Is this, or something similar, possible?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Simple shapes class

    The tutorial you linked doesn't appear to be of good quality. At a glance, I see an example of overriding paint(...) instead of paintComponnet(...) in a JPanel. Bad. But then, that's true of many advertisement-driven sites; all the authors care about is the click-throughs, not whether their information is authentic. edit: Also bad: misuse of inheritance (extending JFrame) and constructing an ImageIcon only to obtain its Image.

    Graphics / Graphics2D already have various methods for drawing. I suggest you go through these Oracle tutorials:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Trail: 2D Graphics (The Java™ Tutorials)

    If animation is part of the mix, you'll find correct approaches in this Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    Last edited by DarrylBurke; 02-22-2012 at 09:56 AM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Simple shapes class

    Thanks Darryl, I did suspect the tutorial wasn't very good, but I had no idea it was ad-driven (ad-block). I'll check out those links, and see what I can do.

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

    Default Re: Simple shapes class

    I followed those tutorials, but I am still confused on how an 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?

Similar Threads

  1. Drawing simple shapes using loops
    By Quinn in forum New To Java
    Replies: 2
    Last Post: 05-29-2011, 09:05 AM
  2. Simple Tic Tac Toe Class
    By daendoonge in forum AWT / Swing
    Replies: 1
    Last Post: 01-29-2011, 07:19 PM
  3. Simple Tic Tac Toe class
    By hydride in forum New To Java
    Replies: 3
    Last Post: 01-26-2011, 05:44 AM
  4. simple exercise for class, help please?
    By Boomer1 in forum Other IDEs
    Replies: 10
    Last Post: 11-02-2009, 06:12 AM
  5. A simple generic class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:41 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
  •