# Plotting a function

• 06-07-2012, 11:17 PM
Cruel Hand
Plotting a function
I'm new here, so I'm sorry if this is in the wrong section.

I'm doing an exercise in which I have to plot a function. I have to make a graph and show the graph of f(x)=x^3. The user gets to pick the maximum value of x that is shown on the graph, and also the size of the frame. So, for example, if the user picks 300 for the size, and 2.5 for the maximum value of x the output is shown here:
Attachment 3854

where I need help is how to plot the actual function. How do I know figure out what x and y coordinates to use for the function?

here is my code, I've highlighted exactly where I need help:
Code:

import javax.swing.JFrame;
import java.util.Scanner;

public class PlotFunction {

static Scanner input = new Scanner(System.in);

public static void main(String[] args){
System.out.print("Enter size of frame: ");
int size = Integer.parseInt(input.nextLine());
System.out.print("Enter maximum value for x: ");
double value = input.nextDouble();
input.nextLine();
JFrame frame = new JFrame("Plot Function");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PlotFunctionPanel panel = new PlotFunctionPanel(size, value);
frame.pack();
frame.setVisible(true);
}
}

panel class
Code:

import javax.swing.JPanel;
import java.awt.*;

public class PlotFunctionPanel extends JPanel{

int size;
static double maxValue;

public PlotFunctionPanel(int s, double v){
size = s;
maxValue = v;
setPreferredSize(new Dimension(size, size));
}

public void paintComponent(Graphics g){
g.drawLine(size/2, 0, size/2, size);
g.drawLine(0, size/2, size, size/2);
double[] tick = getTicks();
int x = size/10, y = size/2, vA = 8;
g.setFont(new Font("Sansserif", Font.PLAIN, size/30));
for(int i = 0; i < 9; i++){
g.drawLine(x, y+5, x, y-5);
if(i != 4 && vA != 4){
if(i > 4)
g.drawString(tick[i]+"", x-size/40, y+size/21);
else
g.drawString(tick[i]+"", x-size/30, y+size/21);
if(vA > 4)
g.drawString(tick[vA]+"", y-size/13, x+size/60);
else
g.drawString(tick[vA]+"", y-size/12, x+size/60);
}
g.drawLine(y+5, x, y-5, x);
x+=size/10;
vA--;
}
g.setColor(Color.RED);
//This is where I need help, I'm almost completely lost. The function has to be plotted in red
/*for(double i = -1*(maxValue);i <= maxValue;i+=maxValue/100){
double fx = i*i*i;
for(int j = 0; j < 9; j++){
if(i==tick[j]||i==-1.0){
System.out.println("i: "+i);
for(int k = 0; k < 9; k++){
if(fx==tick[k]){
System.out.println("fx: "+fx);
//g.drawLine((size/10)*(j+1), size-((size/10)*(k+1)), (size/10)*(j+1), size-(size/10)*(k+1));
}
if(fx<tick[k]){
//g.drawLine((size/10)*(j+1), );
}
if(fx>tick[k]){

}
}
}
if(i<tick[j]){
for(int k = 0; k < 9; k++){
if(fx==tick[k]){
//g.drawLine();
}
if(fx<tick[k]){

}
if(fx>tick[k]){

}
}
}
if(i>tick[j]){
for(int k = 0; k < 9; k++){
if(fx==tick[k]){
//g.drawLine();
}
if(fx<tick[k]){

}
if(fx>tick[k]){

}
}
}
}
i=(Math.round(i*1000.0))/1000.0;
//System.out.println(i);
}*/
}

//finds the values of the ticks on the axis e.g. -2.0, -1.5, -1.0, -0.5, 0.0, etc
private static double[] getTicks(){
double increment = maxValue / 5, currentTick = -1*(maxValue);
double[] tick = new double[9];
for(int i = 0; i < 9; i++){
currentTick+=increment;
tick[i] = Math.round(currentTick*100.0)/100.0;
}
return tick;
}
}