Incdec3 (rest of program is same as Incdec2)
File: java/Incdec3/Main.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Main program for Incdec
*/
public class Main extends JFrame implements ActionListener {
public static void main (String [] args) {
new Main ();
}
private Incdec id1, id2, id3, id4;
public Main () {
// Window setup
setSize (500, 300);
Container content = getContentPane();
content.setLayout (new FlowLayout());
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
// Create some new Incdec's
id1 = new IncdecVert ();
content.add (id1);
id2 = new IncdecPie ();
content.add (id2);
id3 = new IncdecVert ();
content.add (id3);
id4 = new IncdecPie ();
content.add (id4);
// And a button to report the data from the Incdec's, for testing
JButton b = new JButton ("Get data");
b.addActionListener (this);
content.add (b);
setVisible (true);
}
/**
* Callback from the test button
*/
public void actionPerformed (ActionEvent e) {
System.out.println ("Value 1 = " + id1.getValue() +
", Value 2 = " + id2.getValue() +
", Value 3 = " + id3.getValue() +
", Value 4 = " + id4.getValue());
}
}
File: java/Incdec3/IncdecPie.java
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Code for subclass IncDecPie
*/
public class IncdecPie extends Incdec {
/**
* Superclass constructor does most of the work, we finish the job here,
* just choose the layout and the order of the 3 widgets
*/
public IncdecPie () {
setLayout (new GridLayout (3, 1));
makeUp ();
makeShow ();
makeDown ();
}
/**
* Overload base class version
*/
protected void makeShow () {
show = new MyCanvas (this);
add (show);
// Don't call refreshShow(), first paint callback will call it
}
/**
* Overload base class version
*/
protected void refreshShow () {
show.repaint ();
}
/**
* Paint callback from our canvas, passed up to us,
* We redraw from our saved value data
*/
public void drawCanvas (Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// Outline
g2.draw (new Ellipse2D.Double (0., 0., 50., 50.));
// Filled in segment
g2.fill (new Arc2D.Double (0., 0., 50., 50., 0., 360. * (value-min) / (max-min), Arc2D.PIE));
}
}
File: java/Incdec3/MyCanvas.java
import java.awt.*;
import javax.swing.*;
/**
* Need our own little class for the pie chart,
* so it can catch paint callback and pass it back to parent
*/
public class MyCanvas extends JComponent {
private IncdecPie parent;
public MyCanvas (IncdecPie parent) {
this.parent = parent;
setPreferredSize (new Dimension (50, 50));
}
public void paintComponent (Graphics g) {
parent.drawCanvas (g);
}
}