Results 1 to 2 of 2
  1. #1
    Cruel Hand is offline Member
    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    Default 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:
    Plotting a function-e6vck.jpg

    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:
    Java 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.getContentPane().add(panel);
    		frame.pack();
    		frame.setVisible(true);
    	}
    }
    panel class
    Java 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;
    	}
    }
    thanks in advance
    Last edited by Cruel Hand; 06-08-2012 at 12:20 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Plotting a function

    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Plotting a graph
    By cet_1990 in forum Java 2D
    Replies: 2
    Last Post: 12-21-2011, 12:11 AM
  2. Plotting Map Tiles
    By RDReavis in forum Java 2D
    Replies: 2
    Last Post: 06-06-2010, 08:16 PM
  3. plotting graph
    By dhruvaharsha in forum AWT / Swing
    Replies: 1
    Last Post: 02-06-2010, 08:04 PM
  4. Replies: 3
    Last Post: 12-04-2009, 04:08 PM
  5. Help! Plotting a function
    By cvubando in forum New To Java
    Replies: 14
    Last Post: 02-20-2009, 08:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •