Programming in Java Practical Class
You will need to complete the optional parts described in Workbook 6 before attempting Tick 6*. In this exercise you will extend your graphical interface to the Game of Life you wrote in Workbook 6 to include an additional panel containing statistics about the configuration as it evolves. You should produce the same statistics as you computed in Tick 4*, and additionally you should include graphs showing the evolution of these statistics over the previous one hundred generations.
To complete this tick you should create a new package called uk.ac.cam.your-crsid.tick6star
and implement a new class called StatisticsPanel
which extends JPanel
. The class should provide an implementation of a method called update
with the following prototype:
public void update(World w);
You should copy the code from your Tick 6 submission into your new package and update your GuiLife
program to include the StatisticsPanel
by adding it to the main window for the graphical interface and specifying BorderLayout.EAST
as its layout position. You will also need to modify GuiLife
to call update
whenever the state of the world changes.
Hints:
In order to draw arbitrary content onto a JPanel
you need to override the paintComponent
method (see GamePanel.java
for an example).
You may find it helpful to create an additional class called Graph
, which extends JPanel
, to hold your line graphs. In the constructor for this class set the layout manager to be an instance of BorderLayout
. You can add a title to the graph by adding a JLabel
with BorderLayout.NORTH
as its layout position, and add the actual data plot (as another class extending JPanel
) using the BorderLayout.CENTER
layout position. This structure will ensure that your plot automatically resizes nicely if the user changes the size of the main window.
You can find out the width of a component when drawing using the getWidth
and getHeight
methods.
You can draw text on to a Graphics
context using the drawString
method.
The alignment of text in a Graphics
context must be done manually by adjusting the position you start drawing at. To do this you need to know the size of your text string. Here is an sample method which accepts 0, 1 or 2 (representing left, centre or right alignment) for halign
(horizontal alignment) or valign
(vertical alignment).
protected void drawString(Graphics g, String text, int x, int y, int halign, int valign) { FontMetrics m = g.getFontMetrics(); Rectangle2D r = m.getStringBounds(text, g); x -= r.getWidth() * halign / 2; y += r.getHeight() * valign / 2; g.drawString(text,x, y); }
The coordinate (0,0) is the top left pixel in a Graphics
context.
To complete your tick you need to prepare a jar file with the contents of all the classes you have written in this workbook and email it to ticks1a-java@cl.cam.ac.uk
. Your jar file should contain:
uk/ac/cam/your-crsid/tick6star/HelloSwingWorld.java uk/ac/cam/your-crsid/tick6star/HelloSwingWorld.class uk/ac/cam/your-crsid/tick6star/CommandLineOptions.java uk/ac/cam/your-crsid/tick6star/CommandLineOptions.class uk/ac/cam/your-crsid/tick6star/TextLife.java uk/ac/cam/your-crsid/tick6star/TextLife.class uk/ac/cam/your-crsid/tick6star/GuiLife.java uk/ac/cam/your-crsid/tick6star/GuiLife.class uk/ac/cam/your-crsid/tick6star/GamePanel.java uk/ac/cam/your-crsid/tick6star/GamePanel.class uk/ac/cam/your-crsid/tick6star/Strings.java uk/ac/cam/your-crsid/tick6star/Strings.class uk/ac/cam/your-crsid/tick6star/ControlPanel.java uk/ac/cam/your-crsid/tick6star/ControlPanel.class uk/ac/cam/your-crsid/tick6star/PatternPanel.java uk/ac/cam/your-crsid/tick6star/PatternPanel.class uk/ac/cam/your-crsid/tick6star/SourcePanel.java uk/ac/cam/your-crsid/tick6star/SourcePanel.class uk/ac/cam/your-crsid/tick6star/WorldImpl.java uk/ac/cam/your-crsid/tick6star/WorldImpl.class uk/ac/cam/your-crsid/tick6star/ArrayWorld.java uk/ac/cam/your-crsid/tick6star/ArrayWorld.class uk/ac/cam/your-crsid/tick6star/PackedWorld.java uk/ac/cam/your-crsid/tick6star/PackedWorld.class uk/ac/cam/your-crsid/tick6star/AgingWorld.java uk/ac/cam/your-crsid/tick6star/AgingWorld.class uk/ac/cam/your-crsid/tick6star/Pattern.java uk/ac/cam/your-crsid/tick6star/Pattern.class uk/ac/cam/your-crsid/tick6star/PackedLong.java uk/ac/cam/your-crsid/tick6star/PackedLong.class uk/ac/cam/your-crsid/tick6star/PatternLoader.java uk/ac/cam/your-crsid/tick6star/PatternLoader.class uk/ac/cam/your-crsid/tick6star/PatternFormatException.java uk/ac/cam/your-crsid/tick6star/PatternFormatException.class uk/ac/cam/your-crsid/tick6star/StatisticsPanel.java uk/ac/cam/your-crsid/tick6star/StatisticsPanel.class