1

I'm writing a graphic calculator via Java GUI (yes I know there's libraries for this already), when the user types in a function I.E. x^6, I want the drawbox to dynamically size itself, according to the minimum and maximum.

For example, if the user enters x^2 from -4 to 4 I have a min of 0 and a max of 16, so I'd want to display the whole graph in the box, and size it properly. Right now the graph is very small and if I use x^6 as the polynomial it looks very cramped.

Here's where I'm at.

    Graphics2D g2 = (Graphics2D) g;
    double myStart1 = Double.parseDouble(myStart);
    double myEnd1 = Double.parseDouble(myEnd);
    g2.translate(getWidth() / 2, getHeight() / 2);
    g2.scale(5.0, 5.0);
    g2.draw(new Line2D.Double( -myStart1 * 100,0,  -myEnd1 * 100,0));
    if (min < 0) {
        g2.draw(new Line2D.Double(0, -min * 100, 0, -max * 100));
    } else {
        g2.draw(new Line2D.Double(0, min * 100, 0, -max * 100));
    }
    g2.setColor(Color.RED);
    for (int i = 0; i < x.length; i++) {
        if (i + 1 < x.length) {
            g2.draw(new Line2D.Double(x[i], -y[i], x[i + 1], -y[i + 1]));

        } else {
            break;
        }
    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Without further context, `paint` is not responsible for resizing the content, the component is. – MadProgrammer Nov 28 '12 at 01:29
  • yes, but do I use the scale property of g2 to do this? What does scale actually do? Does it actually shift the components of the drawbox or does it just make things bigger? – Evan Hirtenfeld Nov 28 '12 at 01:49
  • It's would be exactly like trying to scale an image in something like photoshop. It's going to effect the overall pixel size... – MadProgrammer Nov 28 '12 at 02:43

1 Answers1

2

At least consider JFreeChart, which scales to fill the preferred size of the enclosing ChartPanel and supports zooming via right-click or mouse-wheel.

Addendum: If an external library is proscribed, SineTest, cited here, may be an approach.

enter image description here

import java.awt.Dimension;
import java.util.Random;
import javax.swing.JFrame;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/** @see https://stackoverflow.com/a/13597045/230513 */
public class ChartPanelTest {

    private static final Random random = new Random();

    private static XYDataset createDataset() {

        final XYSeries series = new XYSeries("Data");
        for (double x = -4; x < 4.01; x += 0.1) {
            series.add(x, Math.pow(x, 6d));
        }
        return new XYSeriesCollection(series);
    }

    private static JFreeChart createChart(final XYDataset dataset) {
        JFreeChart chart = ChartFactory.createXYLineChart(
            "f(x) = x^6", "x", "y", dataset,
            PlotOrientation.VERTICAL, false, false, false);
        return chart;
    }

    public static void main(String[] args) {

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        XYDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart) {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        };
        chartPanel.setMouseWheelEnabled(true);
        f.add(chartPanel);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045