User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,524 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,809 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 575 | Replies: 4
Reply
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

confused about paintocomponent

  #1  
Oct 3rd, 2007
One of the things that's been confusing me so far is why we have to override the paintcomponent() function after extending it. For example, this program draws an ellipse:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;

public class DrawEllipseComponent extends JComponent
{
	public void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D)g;
		Ellipse2D.Double ellipse = new Ellipse2D.Double(0,0,this.getWidth(),this.getHeight());
		g2.draw(ellipse);
		g2.setColor(Color.GREEN);
		g2.fill(ellipse);
	}
}

import javax.swing.JFrame;

public class DrawEllipse
{
	public static void main(String[] args)
	{
		DrawEllipseComponent ellipse = new DrawEllipseComponent();

		JFrame frame = new JFrame();
		frame.setSize(300, 400);
		frame.setTitle("An Ellipse");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(ellipse);
		frame.setVisible(true);
	}
}

I can see that you create an instance of the object, create a frame, and put the object into the frame. Outside of that, I'm not quite sure I understand how the ellipse is being drawn here.
Last edited by degamer106 : Oct 3rd, 2007 at 12:47 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2006
Location: in a dream
Posts: 127
Reputation: nschessnerd is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 6
nschessnerd's Avatar
nschessnerd nschessnerd is offline Offline
Junior Poster

Re: confused about paintocomponent

  #2  
Oct 3rd, 2007
The section of the code
  1. public void paintComponent(Graphics g)
  2. {
  3. Graphics2D g2 = (Graphics2D)g;
  4. Ellipse2D.Double ellipse = new Ellipse2D.Double(0,0,this.getWidth(),this.getHeight());
  5. g2.draw(ellipse);
  6. g2.setColor(Color.GREEN);
  7. g2.fill(ellipse);
  8. }
paints the ellipse. The paintComponents() method is already defined in the Graphics class and it is called whenever the frame is loaded. We need to override it to tell it what to do, otherwise you would have a blank frame.
Reply With Quote  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: confused about paintocomponent

  #3  
Oct 3rd, 2007
Thank you, that cleared some confusion up for me. That helped me finish this Bar Chart program.

However, there's one thing I don't understand from here and that is the Graphics2D.

In the BarChart class, I have a method that accepts a parameter of type Graphics2D. In my BarChartComponent class, I create a Graphics2D object by casting g. What I don't quite understand from both of these classes is the need to pass g2 in as an argument for draw.

o_O

import java.awt.Graphics2D;
import java.awt.Rectangle;

public class BarChart
{
	public BarChart(int x, int y, int t, int w, String name)
	{
		xTop = x;
		yTop = y;
		top = t;
		width = w;
		title = name;
	}
	public void draw(Graphics2D g2)
	{
		Rectangle rect = new Rectangle(xTop, yTop, top, width);
		g2.draw(rect);
		g2.drawString(title, xTop + 5, yTop + 20);
	}
	private int xTop;
	private int yTop;
	private int top;
	private int width;
	private String title;
}

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;

public class BarChartComponent extends JComponent
{
	public void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D)g;

		BarChart myBar1 = new BarChart(0, 0, 420, 30, "Golden Gate");
		myBar1.draw(g2);

		BarChart myBar2 = new BarChart(0, 40, 159, 30, "Brooklyn");
		myBar2.draw(g2);

		BarChart myBar3 = new BarChart(0, 80, 215, 30, "Delaware Memorial");
		myBar3.draw(g2);

		BarChart myBar4 = new BarChart(0, 120, 380, 30, "Mackinac");
		myBar4.draw(g2);
	}
}

import javax.swing.JFrame;

public class BarChartViewer
{
	public static void main(String[] args)
	{
		BarChartComponent myBar = new BarChartComponent();

		JFrame frame = new JFrame();
		frame.setSize(500, 200);
		frame.setTitle("BarChartViewer");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(myBar);
		frame.setVisible(true);
	}
}
Reply With Quote  
Join Date: Dec 2006
Location: in a dream
Posts: 127
Reputation: nschessnerd is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 6
nschessnerd's Avatar
nschessnerd nschessnerd is offline Offline
Junior Poster

Re: confused about paintocomponent

  #4  
Oct 3rd, 2007
BarChart uses graphics2d to draw itself, so you pass on g2 and g2 draws a barchart
Last edited by nschessnerd : Oct 3rd, 2007 at 1:20 pm.
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,090
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 307
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: confused about paintocomponent

  #5  
Oct 3rd, 2007
Yes, the draw() method encapsulates how to draw the chart and the Graphics2D parameter is what to draw the chart on. In that way, your method can draw onto anything that can supply a graphics context to it and is not limited to a graphics context from one particular source.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 4:14 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC