How could I access local variable into a handler?












-1















Sorry guys, I am currently learning java.



I had a question that is how to get access to my local variable?



because I am trying to create a class to implements MouseEvent.
I want to change the property of Line.
but I declare my line in a scope.
which not allow me to change the property of the line.



here is my code



package dotgame;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.Group;
import javafx.scene.layout.GridPane;

public class Dotgame extends Application {

@Override
public void start(Stage primaryStage)
{

//creating array of circles;
Circle circle=new Circle[9];
int cCount=0; //for increment the circle index;
Line line=new Line[12];




//assign the lines
line[0]=new Line(100,100,200,100);
line[1]=new Line(200,100,300,100);
line[2]=new Line(100,200,200,200);
line[3]=new Line(200,200,300,200);
line[4]=new Line(100,300,200,300);
line[5]=new Line(200,300,300,300);
line[6]=new Line(100,100,100,200);
line[7]=new Line(100,200,100,300);
line[8]=new Line(200,100,200,200);
line[9]=new Line(200,200,200,300);
line[10]=new Line(300,100,300,200);
line[11]=new Line(300,200,300,300);

//set line to be invisible
line[0].setVisible(false);
line[1].setVisible(false);
line[2].setVisible(false);
line[3].setVisible(false);
line[4].setVisible(false);
line[5].setVisible(false);
line[6].setVisible(false);
line[7].setVisible(false);
line[8].setVisible(false);
line[9].setVisible(false);
line[10].setVisible(false);
line[11].setVisible(false);




//assign the size of circles;
for(int i=0;i<9;i++)
{
circle[i]=new Circle(10);
}

//assign the X,Y of the circles;
for(int i=100;i<400;i+=100)
{
for(int j=100;j<400;j+=100)
{
circle[cCount].setTranslateX(i);
circle[cCount].setTranslateY(j);

cCount++;

}

}


//start the game
for(int i=0;i<12;i++)
{


}

//node for line
Group lineGroup=new Group();
lineGroup.getChildren().addAll(line);

Group circleGroup = new Group();
circleGroup.getChildren().addAll(circle);

StackPane root=new StackPane();
root.getChildren().addAll(lineGroup,circleGroup);






Scene scene = new Scene(root, 500, 500);


primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String args) {
launch(args);
}


class VisibleLine implements EventHandler<MouseEvent>
{
@Override
public void handle(MouseEvent e)
{
line.setVisible(true); //the problem is here. I couldnt change the line , because line is local varibale.
}
}









share|improve this question

























  • Which one is "the line"? Where would the event handler be added? You're using an array of 12 lines; your handler seems to be working with a single Line though...

    – fabian
    Nov 24 '18 at 11:45


















-1















Sorry guys, I am currently learning java.



I had a question that is how to get access to my local variable?



because I am trying to create a class to implements MouseEvent.
I want to change the property of Line.
but I declare my line in a scope.
which not allow me to change the property of the line.



here is my code



package dotgame;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.Group;
import javafx.scene.layout.GridPane;

public class Dotgame extends Application {

@Override
public void start(Stage primaryStage)
{

//creating array of circles;
Circle circle=new Circle[9];
int cCount=0; //for increment the circle index;
Line line=new Line[12];




//assign the lines
line[0]=new Line(100,100,200,100);
line[1]=new Line(200,100,300,100);
line[2]=new Line(100,200,200,200);
line[3]=new Line(200,200,300,200);
line[4]=new Line(100,300,200,300);
line[5]=new Line(200,300,300,300);
line[6]=new Line(100,100,100,200);
line[7]=new Line(100,200,100,300);
line[8]=new Line(200,100,200,200);
line[9]=new Line(200,200,200,300);
line[10]=new Line(300,100,300,200);
line[11]=new Line(300,200,300,300);

//set line to be invisible
line[0].setVisible(false);
line[1].setVisible(false);
line[2].setVisible(false);
line[3].setVisible(false);
line[4].setVisible(false);
line[5].setVisible(false);
line[6].setVisible(false);
line[7].setVisible(false);
line[8].setVisible(false);
line[9].setVisible(false);
line[10].setVisible(false);
line[11].setVisible(false);




//assign the size of circles;
for(int i=0;i<9;i++)
{
circle[i]=new Circle(10);
}

//assign the X,Y of the circles;
for(int i=100;i<400;i+=100)
{
for(int j=100;j<400;j+=100)
{
circle[cCount].setTranslateX(i);
circle[cCount].setTranslateY(j);

cCount++;

}

}


//start the game
for(int i=0;i<12;i++)
{


}

//node for line
Group lineGroup=new Group();
lineGroup.getChildren().addAll(line);

Group circleGroup = new Group();
circleGroup.getChildren().addAll(circle);

StackPane root=new StackPane();
root.getChildren().addAll(lineGroup,circleGroup);






Scene scene = new Scene(root, 500, 500);


primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String args) {
launch(args);
}


class VisibleLine implements EventHandler<MouseEvent>
{
@Override
public void handle(MouseEvent e)
{
line.setVisible(true); //the problem is here. I couldnt change the line , because line is local varibale.
}
}









share|improve this question

























  • Which one is "the line"? Where would the event handler be added? You're using an array of 12 lines; your handler seems to be working with a single Line though...

    – fabian
    Nov 24 '18 at 11:45
















-1












-1








-1








Sorry guys, I am currently learning java.



I had a question that is how to get access to my local variable?



because I am trying to create a class to implements MouseEvent.
I want to change the property of Line.
but I declare my line in a scope.
which not allow me to change the property of the line.



here is my code



package dotgame;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.Group;
import javafx.scene.layout.GridPane;

public class Dotgame extends Application {

@Override
public void start(Stage primaryStage)
{

//creating array of circles;
Circle circle=new Circle[9];
int cCount=0; //for increment the circle index;
Line line=new Line[12];




//assign the lines
line[0]=new Line(100,100,200,100);
line[1]=new Line(200,100,300,100);
line[2]=new Line(100,200,200,200);
line[3]=new Line(200,200,300,200);
line[4]=new Line(100,300,200,300);
line[5]=new Line(200,300,300,300);
line[6]=new Line(100,100,100,200);
line[7]=new Line(100,200,100,300);
line[8]=new Line(200,100,200,200);
line[9]=new Line(200,200,200,300);
line[10]=new Line(300,100,300,200);
line[11]=new Line(300,200,300,300);

//set line to be invisible
line[0].setVisible(false);
line[1].setVisible(false);
line[2].setVisible(false);
line[3].setVisible(false);
line[4].setVisible(false);
line[5].setVisible(false);
line[6].setVisible(false);
line[7].setVisible(false);
line[8].setVisible(false);
line[9].setVisible(false);
line[10].setVisible(false);
line[11].setVisible(false);




//assign the size of circles;
for(int i=0;i<9;i++)
{
circle[i]=new Circle(10);
}

//assign the X,Y of the circles;
for(int i=100;i<400;i+=100)
{
for(int j=100;j<400;j+=100)
{
circle[cCount].setTranslateX(i);
circle[cCount].setTranslateY(j);

cCount++;

}

}


//start the game
for(int i=0;i<12;i++)
{


}

//node for line
Group lineGroup=new Group();
lineGroup.getChildren().addAll(line);

Group circleGroup = new Group();
circleGroup.getChildren().addAll(circle);

StackPane root=new StackPane();
root.getChildren().addAll(lineGroup,circleGroup);






Scene scene = new Scene(root, 500, 500);


primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String args) {
launch(args);
}


class VisibleLine implements EventHandler<MouseEvent>
{
@Override
public void handle(MouseEvent e)
{
line.setVisible(true); //the problem is here. I couldnt change the line , because line is local varibale.
}
}









share|improve this question
















Sorry guys, I am currently learning java.



I had a question that is how to get access to my local variable?



because I am trying to create a class to implements MouseEvent.
I want to change the property of Line.
but I declare my line in a scope.
which not allow me to change the property of the line.



here is my code



package dotgame;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.Group;
import javafx.scene.layout.GridPane;

public class Dotgame extends Application {

@Override
public void start(Stage primaryStage)
{

//creating array of circles;
Circle circle=new Circle[9];
int cCount=0; //for increment the circle index;
Line line=new Line[12];




//assign the lines
line[0]=new Line(100,100,200,100);
line[1]=new Line(200,100,300,100);
line[2]=new Line(100,200,200,200);
line[3]=new Line(200,200,300,200);
line[4]=new Line(100,300,200,300);
line[5]=new Line(200,300,300,300);
line[6]=new Line(100,100,100,200);
line[7]=new Line(100,200,100,300);
line[8]=new Line(200,100,200,200);
line[9]=new Line(200,200,200,300);
line[10]=new Line(300,100,300,200);
line[11]=new Line(300,200,300,300);

//set line to be invisible
line[0].setVisible(false);
line[1].setVisible(false);
line[2].setVisible(false);
line[3].setVisible(false);
line[4].setVisible(false);
line[5].setVisible(false);
line[6].setVisible(false);
line[7].setVisible(false);
line[8].setVisible(false);
line[9].setVisible(false);
line[10].setVisible(false);
line[11].setVisible(false);




//assign the size of circles;
for(int i=0;i<9;i++)
{
circle[i]=new Circle(10);
}

//assign the X,Y of the circles;
for(int i=100;i<400;i+=100)
{
for(int j=100;j<400;j+=100)
{
circle[cCount].setTranslateX(i);
circle[cCount].setTranslateY(j);

cCount++;

}

}


//start the game
for(int i=0;i<12;i++)
{


}

//node for line
Group lineGroup=new Group();
lineGroup.getChildren().addAll(line);

Group circleGroup = new Group();
circleGroup.getChildren().addAll(circle);

StackPane root=new StackPane();
root.getChildren().addAll(lineGroup,circleGroup);






Scene scene = new Scene(root, 500, 500);


primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String args) {
launch(args);
}


class VisibleLine implements EventHandler<MouseEvent>
{
@Override
public void handle(MouseEvent e)
{
line.setVisible(true); //the problem is here. I couldnt change the line , because line is local varibale.
}
}






java javafx mouseevent






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 6:45









Nicholas K

6,19351031




6,19351031










asked Nov 24 '18 at 6:44









runable_lame runable_lame

11




11













  • Which one is "the line"? Where would the event handler be added? You're using an array of 12 lines; your handler seems to be working with a single Line though...

    – fabian
    Nov 24 '18 at 11:45





















  • Which one is "the line"? Where would the event handler be added? You're using an array of 12 lines; your handler seems to be working with a single Line though...

    – fabian
    Nov 24 '18 at 11:45



















Which one is "the line"? Where would the event handler be added? You're using an array of 12 lines; your handler seems to be working with a single Line though...

– fabian
Nov 24 '18 at 11:45







Which one is "the line"? Where would the event handler be added? You're using an array of 12 lines; your handler seems to be working with a single Line though...

– fabian
Nov 24 '18 at 11:45














1 Answer
1






active

oldest

votes


















0














Two options :




  1. Define it as static : static Line line=new Line[12];



  2. You can initialize it as a member variable in the class



    public class Dotgame extends Application {
    Line line=new Line[12];

    // rest of the code
    }



In both options the visibility of line is seen throughout the class.






share|improve this answer
























  • I declared in outside of start function. but after that, the line showing can't find the methods of Setvisible. Does it mean that I can not setvisible as whole line array? because I add index it works fine. I don't know how to set it as whole things

    – runable_lame
    Nov 25 '18 at 4:31













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455838%2fhow-could-i-access-local-variable-into-a-handler%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Two options :




  1. Define it as static : static Line line=new Line[12];



  2. You can initialize it as a member variable in the class



    public class Dotgame extends Application {
    Line line=new Line[12];

    // rest of the code
    }



In both options the visibility of line is seen throughout the class.






share|improve this answer
























  • I declared in outside of start function. but after that, the line showing can't find the methods of Setvisible. Does it mean that I can not setvisible as whole line array? because I add index it works fine. I don't know how to set it as whole things

    – runable_lame
    Nov 25 '18 at 4:31


















0














Two options :




  1. Define it as static : static Line line=new Line[12];



  2. You can initialize it as a member variable in the class



    public class Dotgame extends Application {
    Line line=new Line[12];

    // rest of the code
    }



In both options the visibility of line is seen throughout the class.






share|improve this answer
























  • I declared in outside of start function. but after that, the line showing can't find the methods of Setvisible. Does it mean that I can not setvisible as whole line array? because I add index it works fine. I don't know how to set it as whole things

    – runable_lame
    Nov 25 '18 at 4:31
















0












0








0







Two options :




  1. Define it as static : static Line line=new Line[12];



  2. You can initialize it as a member variable in the class



    public class Dotgame extends Application {
    Line line=new Line[12];

    // rest of the code
    }



In both options the visibility of line is seen throughout the class.






share|improve this answer













Two options :




  1. Define it as static : static Line line=new Line[12];



  2. You can initialize it as a member variable in the class



    public class Dotgame extends Application {
    Line line=new Line[12];

    // rest of the code
    }



In both options the visibility of line is seen throughout the class.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 6:47









Nicholas KNicholas K

6,19351031




6,19351031













  • I declared in outside of start function. but after that, the line showing can't find the methods of Setvisible. Does it mean that I can not setvisible as whole line array? because I add index it works fine. I don't know how to set it as whole things

    – runable_lame
    Nov 25 '18 at 4:31





















  • I declared in outside of start function. but after that, the line showing can't find the methods of Setvisible. Does it mean that I can not setvisible as whole line array? because I add index it works fine. I don't know how to set it as whole things

    – runable_lame
    Nov 25 '18 at 4:31



















I declared in outside of start function. but after that, the line showing can't find the methods of Setvisible. Does it mean that I can not setvisible as whole line array? because I add index it works fine. I don't know how to set it as whole things

– runable_lame
Nov 25 '18 at 4:31







I declared in outside of start function. but after that, the line showing can't find the methods of Setvisible. Does it mean that I can not setvisible as whole line array? because I add index it works fine. I don't know how to set it as whole things

– runable_lame
Nov 25 '18 at 4:31




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455838%2fhow-could-i-access-local-variable-into-a-handler%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks