Hey guys and gals,
So for my Java class, (which I have been struggling all semester with) we are supposed to break down a code and say which park of the code does what and why. I selected a simple program that creates a bar graph. If anyone could give me some general comments on which lines do what so I can have more of an idea for my paper. Any help is very appreciated!

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class ChartPanel extends JPanel {
private double[] values;

private String[] names;

private String title;

public ChartPanel(double[] v, String[] n, String t) {
names = n;
values = v;
title = t;
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
if (values == null || values.length == 0)
return;
double minValue = 0;
double maxValue = 0;
for (int i = 0; i < values.length; i++) {
if (minValue > values[i])
minValue = values[i];
if (maxValue < values[i])
maxValue = values[i];
}

Dimension d = getSize();
int clientWidth = d.width;
int clientHeight = d.height;
int barWidth = clientWidth / values.length;

Font titleFont = new Font("SansSerif", Font.BOLD, 20);
FontMetrics titleFontMetrics = g.getFontMetrics(titleFont);
Font labelFont = new Font("SansSerif", Font.PLAIN, 10);
FontMetrics labelFontMetrics = g.getFontMetrics(labelFont);

int titleWidth = titleFontMetrics.stringWidth(title);
int y = titleFontMetrics.getAscent();
int x = (clientWidth - titleWidth) / 2;
g.setFont(titleFont);
g.drawString(title, x, y);

int top = titleFontMetrics.getHeight();
int bottom = labelFontMetrics.getHeight();
if (maxValue == minValue)
return;
double scale = (clientHeight - top - bottom) / (maxValue - minValue);
y = clientHeight - labelFontMetrics.getDescent();
g.setFont(labelFont);

for (int i = 0; i < values.length; i++) {
int valueX = i * barWidth + 1;
int valueY = top;
int height = (int) (values[i] * scale);
if (values[i] >= 0)
valueY += (int) ((maxValue - values[i]) * scale);
else {
valueY += (int) (maxValue * scale);
height = -height;
}

g.setColor(Color.red);
g.fillRect(valueX, valueY, barWidth - 2, height);
g.setColor(Color.black);
g.drawRect(valueX, valueY, barWidth - 2, height);
int labelWidth = labelFontMetrics.stringWidth(names[i]);
x = i * barWidth + (barWidth - labelWidth) / 2;
g.drawString(names[i], x, y);
}
}

public static void main(String[] argv) {
JFrame f = new JFrame();
f.setSize(400, 300);
double[] values = new double[3];
String[] names = new String[3];
values[0] = 1;
names[0] = "Item 1";

values[1] = 2;
names[1] = "Item 2";

values[2] = 4;
names[2] = "Item 3";

f.getContentPane().add(new ChartPanel(values, names, "title"));

WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
f.addWindowListener(wndCloser);
f.setVisible(true);
}
}

Recommended Answers

All 8 Replies

IOW we should do your work for you?

Why don't we start out this way, you write out, here, what you have and we will help you refine it. How about that?

So you want us to read to you the code line by line. Do you know how methods are called? If yes, it is pretty simple: Objects are created and their methods are called.
Have you tried reading the API or reading a tutorial on how to creat guis?

IOW we should do your work for you?

Why don't we start out this way, you write out, here, what you have and we will help you refine it. How about that?

Sorry, I never meant for you to do the work for me - I was hoping it would give me a good general overview on the code so I could write the paper more intelligently. I know very little java so my attempt at explaining stuff is going to be sad to say the least but here goes:

1-8: Importing packages to be used in the program.
10-11: No idea
13, 20: Defining class names.
14-18: No clue
21-24: Defining variables.
26-42: I couldn't tell you for the life of me.
44-45: Defining typeface of the graph title.
46-47: Defining typeface of the bar labels.
48-73: This is the sorta stuff I'm so stuck on.
74,76: Getting the colors from the imported pallet to be used for the bar graph and outlines/text.
77-82: Defining the dimensions of the bar graph through the other variable created earlier so that its building the graph, not just a picture of one. (I'm prob wrong)
86: Setting size of the art board.
90,93,96: Strings for the labels of each bar.
98: String for bar graph name.

*The other lines from 84-107 that i didn't mention I havent the slightest clue...*

Any help would be awesome...
Thanks.

What do you mean "no idea" by 10-11? Did you suddenly forget what the "import" keyword does? It's the same as those above it, obviously. But do you know what an "import" does? It does not import any code into the class. It simply allows the developer to type Color instead of java.awt.Color everywhere the developer wishes to use a Color.

I'm sorry, but how long have you been in this class?

Judging by the name "paintComponent" alone, what do you think the purpose of that method is?

As far as 14 - 18 are concerned, do you know what variables are? instance, in this case.

And nothing is defining any "typeface".

Take a look here http://java.sun.com/javase/6/docs/api/index.html and try again, with a bit of research this time.

I don't mean to be hard on you, but you will learn it best if you struggle with it a bit yourself, first.

You don't understand the situation in my Java class. The professor is a math teacher who filled the spot of the only java professor who was in the school. He doesn't know java but was the best suited for the job on short notice apparently. Not saying hes not smart, he seems extremely intelligent but cannot teach programming for the life of him, hence why me writing from a general point of view would be sufficient. Everyone seems to be struggling in the class and I didn't just want to copy and paste definitions off the internet. That aside, "It simply allows the developer to type Color instead of java.awt.Color everywhere the developer wishes to use a Color" this is the sort of general ideas I was looking for, thx.
By typeface i meant font. And yeah, I don't know why your being so hard on me either - I just really don't understand and I'm trying to wrap my head conceptually around this program.

I told you why I am being hard on you. I told you also that I don't really mean to be, I did not say I didn't know why.

I gave you a link to the API docs. Every programmers best friend. I was not telling you to "copy stuff you found off the internet". I was suggesting you look at the API docs for the classes and methods concerned in that code and try to figure out what it is doing.

I think this is the most interesting exchange I've read so far in this forum.

If you want a serious suggestion, choose a simpler program to break down. If you don't know Java programming, which you clearly don't, then don't try to write about a program that is above a beginner level, for the same reason that you wouldn't try to explain Calculus to anyone without first understanding algebra. For the same reason, you would be wise to choose a program that comes with a description. For example, if you choose a common school project such as a "Java library", you will be able to analyze it more easily because you'll know what it is supposed to be doing and it'll be easier to connect the dots. My two cents: start over. You'll save time in the long run. If you need help choosing a sensible project for your experience level, there are a lot of current and former students here who I'm sure would be willing to help. . I could give you suggestions for programs of similar length (100 lines) that do tasks which I feel are more sensible for you to analyze. (P.S. does anyone else agree/disagree with my suggestion?)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.