package edu.jsu.leathrum.mathlets.shared; import javax.swing.*; import java.awt.geom.*; import java.awt.Graphics2D; import java.awt.Graphics; import java.awt.Color; import java.awt.Dimension; public abstract class ASimpleGraphCanvas extends JComponent { Dimension size; protected int wd, ht, sc; GeneralPath axpath = new GeneralPath(); public AffineTransform tr = new AffineTransform(); public AffineTransform trinv = new AffineTransform(); public ASimpleGraphCanvas(int initialWidth, int initialHeight, int scale) { wd = initialWidth; ht = initialHeight; sc = scale; size = new Dimension(initialWidth, initialHeight); setBackground(Color.white); tr.setToIdentity(); tr.scale(sc,-sc); tr.translate(wd/(2*sc),-ht/(2*sc)); try {trinv = tr.createInverse();} catch (NoninvertibleTransformException e) {trinv.setToIdentity();} buildAxesPath(); } public Dimension getPreferredSize() { return getMinimumSize(); } public Dimension getMinimumSize() { return size; } private void buildAxesPath() { double value = 0.0; double temp = 0.0; Point2D p2; double xmax = wd/(2*sc), xmin = -xmax, delx = 1.0; double ymax = ht/(2*sc), ymin = -ymax, dely = 1.0; axpath = new GeneralPath(); p2 = tr.transform(new Point2D.Double(0.0,0.0),null); axpath.append(tr.createTransformedShape( new Line2D.Double(xmin,0.0,xmax,0.0)),false); axpath.append(tr.createTransformedShape( new Line2D.Double(0.0,ymin,0.0,ymax)),false); for (value = delx; value < xmax ; value+=delx) { temp = tr.transform(new Point2D.Double(value,0.0),null).getX(); axpath.append(new Line2D.Double(temp, p2.getY()-3, temp, p2.getY()+3),false); } for (value = -delx; value > xmin ; value-=delx) { temp = tr.transform(new Point2D.Double(value,0.0),null).getX(); axpath.append(new Line2D.Double(temp, p2.getY()-3, temp, p2.getY()+3),false); } for (value = dely; value < ymax ; value+=dely) { temp = tr.transform(new Point2D.Double(0.0,value),null).getY(); axpath.append(new Line2D.Double(p2.getX()-3, temp, p2.getX()+3, temp),false); } for (value = -dely; value > ymin ; value-=dely) { temp = tr.transform(new Point2D.Double(0.0,value),null).getY(); axpath.append(new Line2D.Double(p2.getX()-3, temp, p2.getX()+3, temp),false); } } public void drawAxes(Graphics2D g) { g.setPaint(Color.black); g.draw(axpath); } protected abstract void drawFigure(Graphics2D g); public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; drawAxes(g2); drawFigure(g2); } }