•
•
•
•
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
![]() |
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Rep Power: 3
Solved Threads: 0
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:
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.
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.
The section of the code
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.
java Syntax (Toggle Plain Text)
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); }
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Rep Power: 3
Solved Threads: 0
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
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);
}
}![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- confused about SE + SEO. (Search Engine Optimization)
- Confused about SATA RAID (Storage)
- Confused about loops and switch (Java)
- Confused writing a Java Class (Java)
- confused about shell sort ... (C)
- confused how to begin this program (C++)
- Need help With this C++ Program..Confused.. (C++)
- ftp client issues...im so confused...plz help!!! (Windows Software)
Other Threads in the Java Forum
- Previous Thread: getters&setters
- Next Thread: Paragraph



Linear Mode