Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Bar chart (Java) Implement a Swing application which draws a vertical bar chart.

ID: 3602071 • Letter: B

Question

Bar chart (Java)

Implement a Swing application which draws a vertical bar chart. The input is an array of integers (you can hardcode the data in your application.) Use the index of an array entry as an entry on the X axis. The height of the bar should correspond to the value of the array entry. For instance, if the array entries are 13000, 4000, 5000), then the bars should not be drawn 3000, 4000, and 5000 pixels tall (respectively.) They should be drawn in the current proportion to the original values (perhaps 300, 400 and 500 pixels tall, but in the end it will depend on the size of your actual window As an example you can render your bar chart like the following (ignoring the tabs on top of the window): Bake Sale Demo Bar Pie Graph Population Comparison 1400 1300 1200 1100 1 000 E 900 800 700 600 500 400 300 200 100 China India USA Australia

Explanation / Answer

Barchart.java

import java.util.ArrayList;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.Rectangle;

public class BarChart
{
private int width;
private int height;
private ArrayList<Double> data;

public BarChart(int aWidth, int aHeight)
{
width = aWidth;
height = aHeight;
data = new ArrayList<Double>();
}

public void add(double value)
{
data.add(value);
}

public void draw(Graphics2D g2)
{
int i = 0;
double max = 0;

for (Double wrapper : data)
if(max < wrapper)
max = wrapper;

int xwidth = width - 1;
int yheight = height - 1;

int xleft = 0;

for (i = 0; i < data.size(); i++)
{
int xright = xwidth * (i + 1) / data.size();
int barWidth = xwidth / data.size();
int barHeight = (int) Math.round(yheight * data.get(i) / max);

Rectangle bar =
new Rectangle(xleft, yheight - barHeight,
barWidth, barHeight);
g2.draw(bar);

xleft = xright;
}
}
}

BarChartComponent.java

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

public class BarChartComponent extends JComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
BarChart c = new BarChart(getWidth(), getHeight());
c.add(1.1);
c.add(3.6);
c.add(4.0);
c.add(3.7);
c.add(2.1);
c.add(2.7);
c.add(2.6);
c.draw(g2);
}
}

BarChartTester.java
import javax.swing.JFrame;

public class BarChartTester
{
public static void main(String[] args)
{
JFrame frame = new JFrame();

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

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

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

frame.setVisible(true);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote