Bar.java
import java.awt.Rectangle;
import java.awt.Graphics2D;
/**
This class describes a bar in a bar chart
*/
public class Bar
{
private int top;
private int width;
private String title;
/**
Constructs a bar.
@param aTop the top of the bar
@param aWidth the width of the bar
@param aTitle the title for the bar
*/
public Bar(int aTop, int aWidth, String aTitle)
{
...
}
/**
Draws the bar.
@param g2 the graphic context
*/
public void draw(Graphics2D g2)
{
...
}
}
BarChartComponent.java
import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
/**
Draws a chart of bridge spans.
*/
public class BarChartComponent extends JComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
// construct and draw four Bar objects
}
}
Use the following file:
BarChartViewer.java
import javax.swing.*;
public class BarChartViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(500, 400);
frame.setTitle("BarChartViewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BarChartComponent component = new BarChartComponent();
frame.add(component);
frame.setVisible(true);
}
}