5 Replies - 10755 Views - Last Post: 25 January 2008 - 07:07 AM Rate Topic: -----

#1 clatcho  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 14-February 07

X & Y Coordinates

Posted 24 January 2008 - 02:53 PM

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

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...

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

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


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

Is This A Good Question/Topic? 0
  • +

Replies To: X & Y Coordinates

#2 clatcho  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 14-February 07

Re: X & Y Coordinates

Posted 24 January 2008 - 03:11 PM

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


   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 ....


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
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: X & Y Coordinates

Posted 24 January 2008 - 05:03 PM

View Postclatcho, on 24 Jan, 2008 - 05:11 PM, said:

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! ;) 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.

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.
Was This Post Helpful? 0
  • +
  • -

#4 clatcho  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 14-February 07

Re: X & Y Coordinates

Posted 25 January 2008 - 12: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, 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:
Was This Post Helpful? 0
  • +
  • -

#5 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: X & Y Coordinates

Posted 25 January 2008 - 04:53 AM

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.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: X & Y Coordinates

Posted 25 January 2008 - 07:07 AM

View Postclatcho, on 25 Jan, 2008 - 02:40 AM, said:

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.

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);
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1