import javax.swing.*; import java.awt.*; abstract class Chart extends JPanel { int[] data; int width, height; int margin = 20; //a margin of blank space around the chart Color fgColor = Color.black; Color bgColor = Color.white; public Chart(int[] thedata) { data = new int[thedata.length]; System.arraycopy(thedata,0,data,0,data.length); } public void setData(int[] thedata) { data = new int[thedata.length]; System.arraycopy(thedata,0,data,0,data.length); } public void setBackground(Color color) { bgColor = color; } public void setForeground(Color color) { fgColor = color; } public void setMargin(int newMargin) { margin = newMargin; } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); width = getWidth(); height = getHeight(); g2.setBackground(bgColor); g2.clearRect(0,0,width,height); width -= 2*margin; height -= 2*margin; g2.translate(margin,margin); g2.setColor(fgColor); draw(g); } abstract public void draw(Graphics g); }