aspose file tools*
The moose likes Beginning Java and the fly likes Someone Please Help! Graphics 2D Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Someone Please Help! Graphics 2D" Watch "Someone Please Help! Graphics 2D" New topic
Author

Someone Please Help! Graphics 2D

Brittany Ansteatt
Greenhorn

Joined: Nov 06, 2008
Posts: 6
I am such a beginner Could some one please help me with this program??...

Write a class PieChart with methods


public void add (double value)
public void draw(Graphics2D g2)

that displays a pie chart of the values in data. You may assume that all data values are positive.

Here is a sample program output:

It just looks like and empty pie chart without numbers or anything!!


Use the following class as your main class:


import javax.swing.JFrame;

/**
Displays a pie chart.
*/
public class ChartViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();

final int FRAME_WIDTH = 300;
final int FRAME_HEIGHT = 300;

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("ChartViewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

PieChartComponent component = new PieChartComponent();
frame.add(component);

frame.setVisible(true);
}
}


Use the following class in your solution:


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

/**
Component that displays a pie chart.
*/
public class PieChartComponent extends JComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
PieChart p = new PieChart(getWidth(), getHeight());
p.add(1.1);
p.add(3.6);
p.add(4.0);
p.add(3.7);
p.add(2.1);
p.add(2.7);
p.add(2.6);
p.draw(g2);
}
}

[edit]Get rid of CAPITAL LETTERS. CR[/edit]
[ November 07, 2008: Message edited by: Campbell Ritchie ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Hi, welcome to the ranch
You may not be used to posting questions at the ranch, so you should take a look at this FAQ. Especially this part.

Also, when posting some code, please use the icon and write the code inside the "code" tags.


[My Blog]
All roads lead to JavaRanch
Brittany Ansteatt
Greenhorn

Joined: Nov 06, 2008
Posts: 6
Got it.
SORRY!
How do I access the "code" icon?
Brittany Ansteatt
Greenhorn

Joined: Nov 06, 2008
Posts: 6
nevermind about the code icon question...do you know how I can find info on the question I am looking for?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39868
    
  28
Originally posted by Brittany Ansteatt:
nevermind about the code icon question...do you know how I can find info on the question I am looking for?


I beg your pardon?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42632
    
  65
What does your PieChart class do so far?


Ping & DNS - my free Android networking tools app
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

We are happy to HELP you with your program, but nobody here is going to write it for you. We work best if you ask specific, focused questions, rather than make "these 200+ lines don't work" statements.

What, exactly, works, doesn't work, or works differently than you expect? Where exactly are you stuck?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14350
    
  22

A good place to start is the API documentation. Lookup the class Graphics2D there. The documentation lists all the methods that you can call in the class. See what methods you could use to draw a pie chart.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Brittany Ansteatt
Greenhorn

Joined: Nov 06, 2008
Posts: 6
This is the assignemnt that I am to do. I have looked in the java API and online all over this forum was a last resort for me. that is all the information that the instructor gave me to start with. I dont know where to begin really!!! I dont understand how I am supposed to use the add method...I dont know what it is referring to.
Thanks

Use the following class in your solution:


[edit]Corrected code tags. CR[/edit]
[ November 09, 2008: Message edited by: Campbell Ritchie ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18996
    
  40

This is the assignemnt that I am to do. I have looked in the java API and online all over this forum was a last resort for me. that is all the information that the instructor gave me to start with. I dont know where to begin really!!! I dont understand how I am supposed to use the add method...I dont know what it is referring to.


Hmmmm... Generally, instructors teach stuff that can be used for homework assignments. I would suggest going over your class notes. It probably the best place to start.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Brittany Ansteatt
Greenhorn

Joined: Nov 06, 2008
Posts: 6
Actually, it is extra credit on stuff we are supposed to try and teach ourselves in chapter of the book we dont go over.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42632
    
  65
The PieChart class that you're asked to write will need to contain a data structure for the individual values of each pie section. This data structure will need to grow dynamically (as each "add" call adds another data value), so -e.g.- an array would not be a good solution.

You also need to implement a "draw" method that paints the chart onto the Graphics object, respecting the width and height that were passed in through the constructor earlier.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39868
    
  28
Go through the Java� Tutorials; that section will tell you about the sort of data structures you can use.
Michael Dunn said something about building cars on this recent thread. Do as he says, break the task down.
Start by creating a PieChart object, and add 1.1, 3.6, 4.0, 3.7, 2.1, 2.7 and 2.6 to it.
You will have to give it a draw(Graphics2D) method, so go through the Graphics2D class until you find methods suitable for drawing pie charts with.

Then worry about the Component later. And you should have been given protected void paintComponent() not public.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39868
    
  28
Actually, the method I was thinking of seems to be in Graphics, but you can still get at it from a Graphics2D object.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39868
    
  28
You have to add 1.1, 3.6, 4.0, 3.7, 2.1, 2.7 and 2.6 together, then divide by the total, then multiply by 360 to get how many degrees you want for your sector. You may need an (int) cast too.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Someone Please Help! Graphics 2D