Welcome to Dream.In.Code
Become a Java Expert!

Join 150,386 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,123 people online right now. Registration is fast and FREE... Join Now!




X & Y Coordinates

 
Reply to this topicStart new topic

X & Y Coordinates

clatcho
24 Jan, 2008 - 01:53 PM
Post #1

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 32


My Contributions
Hi All,

Having a bit of bother with a little project.

Basic description: A JPanel when clicked paints an oval.

The problem: Seems like the the X coordinate is a little to the left and as for the Y, its too dam high.

I have four classes

Start Life // Just starts the program

CODE

package lifeo;

/**
*
* @author andy
*/
public class StarLife
{
  
   /** Creates a new instance of StarLife */
   public StarLife()
   {
   }
  
   /**
    * @param args the command line arguments
    */
   public static void main(String[] args)
   {
      Planet earth = new Planet("LifO Project");
      earth.setVisible(true);
   }
  
}


Planet // This is where the JPanel is...

CODE

package lifeo;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
/**
*
* @author andy
*/
public class Planet extends JFrame
{
   // Fields
  
   private JPanel thePlanet;
   private ArrayList<LifeObjects> lifeContainer;
  
  
   /** Creates a new instance of Planet */
   public Planet(String title)
   {
      // Set the title, size, location and default close operation
      this.setTitle(title);
      this.setSize(500, 480);
      this.setLocation(200,200);
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);
      
    
      // Create the ArrayList
      lifeContainer = new ArrayList<LifeObjects>();
      
      thePlanet   = new JPanel();
      thePlanet.setBackground(Color.WHITE);
      thePlanet.addMouseListener(new MouseWatcher());
      
      
      Container cp = getContentPane();

      cp.add(thePlanet, BorderLayout.CENTER);
      
   }
  
   public void paint(Graphics g)
   {
      super.paint(g);
      for (LifeObjects output : lifeContainer)
      {
         g.setColor(output.getColour());
         g.fillOval(output.getX(), output.getY() , output.getWidth(), output.getHeight());      
      }
   }
  
   public class MouseWatcher extends MouseAdapter
   {
      public void mouseClicked(MouseEvent e)
      {
         Blue blueObject = new Blue(Color.BLUE,e.getX(),e.getY());
         lifeContainer.add(blueObject);
         repaint();
      }
   }
  
}


LifeObjects // This is a class that others inherit from - should have made this abstract but never mind for now

CODE

package lifeo;
import java.awt.*;
/**
*
* @author andy
*/
public class LifeObjects
{
   // Fields
   private Color lifeColour;
   private int width, height, X, Y;
  
   /** Creates a new instance of LifeObjects */
   public LifeObjects(Color colour, int X, int Y)
   {
      lifeColour  = colour;
      width       = width;
      height      = height;
      setX(X);
      setY(Y);
   }
  
   public void setX(int X)
   {
      this.X = X;
   }
  
   public void setY(int Y)
   {
      this.Y = Y;
   }
  
   public int getX()
   {
      return this.X;
   }
  
   public int getY()
   {
      return this.Y;
   }
  
   public Color getColour()
   {
      return lifeColour;
   }
  
   public int getWidth()
   {
      return width;
   }
  
   public int getHeight()
   {
      return height;
   }
  
   public void setWidth(int width)
   {
      this.width = width;
   }
  
   public void setHeight(int height)
   {
      this.height = height;
   }

}


Blue // Finally one of the object that are created when clicked

CODE


package lifeo;
import java.awt.*;
/**
*
* @author andy
*/
public class Blue extends LifeObjects
{
   // Fields
   private int width = 20;
   private int height = 20;
  
   /** Creates a new instance of Blue */
   public Blue(Color colour, int X, int Y)
   {
      super(colour,X,Y);
      super.setWidth(width);
      super.setHeight(height);
   }
  
}


As i say when you click on the JPanel the Blue ball just is off mark, and i just cant figure why
User is offlineProfile CardPM
+Quote Post

clatcho
RE: X & Y Coordinates
24 Jan, 2008 - 02:11 PM
Post #2

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 32


My Contributions
Its ok i solved it, this has happened everytime i post here, after posting i solve it

What i did was changed the paint method in my Planet class like this

CODE


   public void paint(Graphics g)
   {
      super.paint(g);
      g = thePlanet.getGraphics();
      
      for (LifeObjects output : lifeContainer)
      {
         g.setColor(output.getColour());
         g.fillOval(output.getX(), output.getY() , output.getWidth(), output.getHeight());      
      }
   }




Adding in the line ....

CODE


g = thePlanet.getGraphics();



But i don't really understand why this fixed it?


Got this from the Java API
getGraphics() = Creates a graphics context for this component.


Could someone explain in layman's terms what this does
User is offlineProfile CardPM
+Quote Post

baavgai
RE: X & Y Coordinates
24 Jan, 2008 - 04:03 PM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,290



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
QUOTE(clatcho @ 24 Jan, 2008 - 05:11 PM) *

Could someone explain in layman's terms what this does


In order to draw on anything, you need it's graphics context. Basically, you need the graphics object associated with it. Doing a getGraphics(); call is expensive in terms of time. Most Java apps use the one handed to them in the paint method. Which brings us to back you your problem.

You have a perfectly good JFrame that has it's own perfectly good canvas to draw on, and you filled the whole thing up with a JPanel! wink2.gif The Graphics object that the paint method for Planet gets is the one you'd get if you called this.getGraphics();. You can paint on it all you like, but there's a big ole JPanel in front of it. What you did was manually get a Graphics object for the object in fron of your JFrame and painted on that.


By way of example, I messed with your code a bit.

CODE

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;

public class Planet extends JFrame {
    // You do not need another JPanel
    // private JPanel thePlanet;
    private ArrayList<LifeObject> lifeObjects;

    // private classes, no one else needs to share
    private class MouseWatcher extends MouseAdapter {
        public void mouseClicked(MouseEvent e)  {
            lifeObjects.add(new LifeObjectBlue(e.getPoint()));
            repaint();
        }
    }
    
    // might as well make it abstract
    // it's not like it defines a size :p
    private abstract class LifeObject {
        private Color lifeColour;
        protected int width, height, X, Y;
        public LifeObject(Color colour, int X, int Y) {
            this.lifeColour  = colour;
            //this.width       = width;
            //this.height      = height;
            this.X = X;
            this.Y = Y;
        }
        // there will be no set method, you create it, you're stuck with it
        public Color getColour() { return lifeColour; }
        public int getX() { return this.X; }
        public int getY() { return this.Y; }
        public int getWidth() { return width; }
        public int getHeight() { return height; }
    }

    private class LifeObjectBlue extends LifeObject {
        public LifeObjectBlue(Point pt) {
            super(Color.BLUE,pt.x,pt.y);
            this.width = 20;
            this.height = 20;
        }
      
    }
    
    
    public Planet(String title) {
        // Set the title, size, location and default close operation
        this.setTitle(title);
        this.setSize(500, 480);
        this.setLocation(200,200);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        // Create the ArrayList
        this.lifeObjects = new ArrayList<LifeObject>();

        //thePlanet   = new JPanel();
        this.setBackground(Color.WHITE);
        this.addMouseListener(new MouseWatcher());
        //Container cp = getContentPane();
        //cp.add(thePlanet, BorderLayout.CENTER);

    }

    public void paint(Graphics g) {
        for (LifeObject item : this.lifeObjects) {
            g.setColor(item.getColour());
            g.fillOval(item.getX(), item.getY(), item.getWidth(), item.getHeight());      
        }
    }

    public static void main(String[] args) {
        Planet earth = new Planet("LifO Project");
        earth.setVisible(true);
    }
}


Hope this helps.

User is online!Profile CardPM
+Quote Post

clatcho
RE: X & Y Coordinates
24 Jan, 2008 - 11:40 PM
Post #4

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 32


My Contributions
Interesting, my teachings so far have said that the JFrame is like a picture frame, and the Jpanel is the canvas that you paint on, how could you paint without it? but as you demonstrate, you can ! its good to see a different way of doing things, thank you very much. pirate.gif
User is offlineProfile CardPM
+Quote Post

1lacca
RE: X & Y Coordinates
25 Jan, 2008 - 03:53 AM
Post #5

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
You can redefine the paint function of any GUI component, so you can effectively use any of them as a canvas.
However there is a component called Canvas, that I would recommend to check out, because if you paint on the usual components (like JPanel), you can run into some strange problems, notably: they can have borders, so if you deal with mouse actions, you'll have problems with calculating the exact coordinates, etc. You can avoid these if you use the Canvas instead.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: X & Y Coordinates
25 Jan, 2008 - 06:07 AM
Post #6

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,290



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
QUOTE(clatcho @ 25 Jan, 2008 - 02:40 AM) *

Interesting, my teachings so far have said that the JFrame is like a picture frame, and the Jpanel is the canvas that you paint on,


JFrame is a top level display object. Yes, JPanel is the canvas. However, all Swing componets extend component and implement paint. So you can basically paint on anything. ( Interesting fact: in MS windows, all controls are windows with different formatting flags. )

Yes, using a JPanel as your canvas is most common. In which case you override the paint in your extension of the JPanel object. Here's a more standard design pattern for the same program.

CODE

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;

public class Planet extends JPanel implements MouseListener {
    private ArrayList<LifeObject> lifeObjects;
    
    private abstract class LifeObject {
        protected Color lifeColour;
        protected int width, height, x, y;
        public Color getColour() { return lifeColour; }
        public int getX() { return this.x; }
        public int getY() { return this.y; }
        public int getWidth() { return width; }
        public int getHeight() { return height; }
    }

    private class LifeObjectBlue extends LifeObject {
        public LifeObjectBlue(Point pt) {
            this.lifeColour = Color.BLUE;
            this.x = pt.x;
            this.y = pt.y;
            this.width = 20;
            this.height = 20;
        }
    }
    
    
    public Planet() {
        this.lifeObjects = new ArrayList<LifeObject>();
        this.addMouseListener(this);
    }

    public void paint(Graphics g) {
        for (LifeObject item : lifeObjects) {
            g.setColor(item.getColour());
            g.fillOval(item.getX(), item.getY(), item.getWidth(), item.getHeight());      
        }
    }

    public void mouseClicked(MouseEvent e) {
            lifeObjects.add(new LifeObjectBlue(e.getPoint()));
            repaint();
     }
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    
    
    public static void main(String[] args) {
        JFrame frame = new JFrame ("LifO Project");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 480);
        frame.setLocation(200,200);
        frame.setBackground(Color.WHITE);
        frame.add(new Planet(), BorderLayout.CENTER);
        frame.setVisible (true);
    }
}


User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:57PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month