Hi, sorry my title isn't very explanatory. What I need to do is to access a variable from a main function, in another function without using a parameter to do so. Based on the nature of the problem you might see why I would do this.
(if there is a way using parameters, that would work too.)
I want the variable String stringFunction to be accessible by the function paintComponent. The get/set methods I have here work in the confines of the main method but not outside of it.
If what I need is unclear, please ask for clarification.
*parts have been omitted that are unrealated to my problem, denoted by .........
public class PlotExpression extends JFrame {
private String stringFunction;
public void setStringFunction(String x){
stringFunction = x;
}
public String getStringFunction(){
return stringFunction;
}
public PlotExpression(){
add(new ExpressionPanel());
}
public static void main(String[] args) {
if(args.length != 3){
System.out.println("Usage: java PlotExpression [Function] [X bound] [Y bound]");
}
String stringFunction = args[0];
String xBoundString = args[1];
String yBoundString = args[2];
System.out.println("Function: "+args[0]+"\nX Bound: "+args[1]+"\nY Bound: "+args[2]);
PlotExpression frame = new PlotExpression();
frame.setStringFunction(args[0]);
frame.setTitle("PlotExpression");
frame.setSize(1000,1000);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
protected void paintComponent(Graphics g){
int xorigin = 50;
int yorigin = 950;
//String stringFunction = s;
//BEGIN GRAPH
int x[] = {xorigin,xorigin,yorigin};
int y[] = {xorigin,yorigin,yorigin};
g.drawPolyline(x,y,x.length);
.......
//TEST FUNCTION
//attempt to graph y = x*2
int bound = 9;
int xpoly[] = new int[bound]; //declare points to be made
for(int i = 0; i<bound; i++){ //read in command line function here
xpoly[i] = i*xorigin + xorigin;
}
int ypoly[] = new int[bound]; //declare points to be made
for(int i = 0; i<bound; i++){
ypoly[i] = yorigin - ((xpoly[i]*2)-2*xorigin); //read in command line function here
}
g.drawPolyline(xpoly,ypoly,xpoly.length);
}
}
My first issue in using parameters exists here:
public PlotExpression(){
add(new ExpressionPanel());
}
and here
PlotExpression frame = new PlotExpression();
Because of this, I am unsure how to include a new parameter for a string. If parameters aren't the way to go, I haven't been able to let paintComponent have access to stringFunction either.