I had a question regarding JavaFX and the ability to check a graphic. so within my program, I have a graphics being set for my buttons when the user clicks it.
What I need to find out is what do I need to have inside "temp.getText().equals("O")" and "temp.getText().equals("X")" to make it to where it's checking the button's image name instead of looking for text.
I want the game to say if "X.jpg"(and vice versa for O) is already inside a button then call this error screen popup
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.util.Timer;
import java.util.TimerTask;
public class Controller
{
#FXML
private Text winnerLabel;
#FXML
private Button space1;
#FXML
private Button space2;
#FXML
private Button space3;
#FXML
private Button space4;
#FXML
private Button space5;
#FXML
private Button space6;
#FXML
private Button space7;
#FXML
private Button space8;
#FXML
private Button space9;
#FXML
private Button reset;
#FXML
private Text playerLabel;
#FXML
private Text timerLabel;
#FXML
private Image image;
private boolean isGame = true;
private ImageView tempImageView;
#FXML
protected void initialize()
{
if(isGame)
{
Timer timer = new Timer();
TimerTask task = new TimerTask()
{
#Override
public void run()
{
timerLabel.setText(Integer.toString(Integer.parseInt(timerLabel.getText()) - 1));
if(Integer.parseInt(timerLabel.getText()) == 0)
{
if(playerLabel.getText().equals("Player 1"))
{
playerLabel.setText("Player 2");
}
else
{
playerLabel.setText("Player 1");
}
timerLabel.setText(Integer.toString(Integer.parseInt(timerLabel.getText()+15)));
if(space1.getText() == "X" && space2.getText() == "X" && space3.getText() == "X")
{
space1.setStyle("-fx-background-color:#90EE90");
space2.setStyle("-fx-background-color:#90EE90");
space3.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 1 has won, click reset to play again.");
timer.cancel();
isGame=false;
}
else if (space1.getText() == "O" && space2.getText() == "O" && space3.getText() == "O")
{
space1.setStyle("-fx-background-color:#90EE90");
space2.setStyle("-fx-background-color:#90EE90");
space3.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 2 has won, click reset to play again.");
timer.cancel();
}
//vertical
else if(space1.getText() == "X" && space4.getText() == "X" && space7.getText() == "X")
{
space1.setStyle("-fx-background-color:#90EE90");
space4.setStyle("-fx-background-color:#90EE90");
space7.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 1 has won, click reset to play again.");
timer.cancel();
}
else if(space1.getText() == "O" && space4.getText() == "O" && space7.getText() == "O")
{
space1.setStyle("-fx-background-color:#90EE90");
space4.setStyle("-fx-background-color:#90EE90");
space7.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 2 has won, click reset to play again.");
timer.cancel();
}
//diagonal
else if (space1.getText() == "X" && space5.getText() == "X" && space9.getText() == "X")
{
space1.setStyle("-fx-background-color:#90EE90");
space5.setStyle("-fx-background-color:#90EE90");
space9.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 1 has won, click reset to play again.");
timer.cancel();
}
else if (space1.getText() == "O" && space5.getText() == "O" && space9.getText() == "O")
{
space1.setStyle("-fx-background-color:#90EE90");
space5.setStyle("-fx-background-color:#90EE90");
space9.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 2 has won, click reset to play again.");
timer.cancel();
}
//vertical 2nd
else if (space2.getText() == "X" && space5.getText() == "X" && space8.getText() == "X")
{
space2.setStyle("-fx-background-color:#90EE90");
space5.setStyle("-fx-background-color:#90EE90");
space8.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 1 has won, click reset to play again.");
timer.cancel();
}
else if (space2.getText() == "O" && space5.getText() == "O" && space8.getText() == "O")
{
space2.setStyle("-fx-background-color:#90EE90");
space5.setStyle("-fx-background-color:#90EE90");
space8.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 2 has won, click reset to play again.");
timer.cancel();
}
//vertical 3rd
else if (space3.getText() == "X" && space6.getText() == "X" && space9.getText() == "X")
{
space3.setStyle("-fx-background-color:#90EE90");
space6.setStyle("-fx-background-color:#90EE90");
space9.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 1 has won, click reset to play again.");
timer.cancel();
}
else if(space3.getText() == "O" && space6.getText() == "O" && space9.getText() == "O")
{
space3.setStyle("-fx-background-color:#90EE90");
space6.setStyle("-fx-background-color:#90EE90");
space9.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 2 has won, click reset to play again.");
timer.cancel();
}
//Horizontal 2nd
else if (space4.getText() == "X" && space5.getText() == "X" && space6.getText() == "X")
{
space4.setStyle("-fx-background-color:#90EE90");
space5.setStyle("-fx-background-color:#90EE90");
space6.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 1 has won, click reset to play again.");
timer.cancel();
}
else if (space4.getText() == "O" && space5.getText() == "O" && space6.getText() == "O")
{
space4.setStyle("-fx-background-color:#90EE90");
space5.setStyle("-fx-background-color:#90EE90");
space6.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 2 has won, click reset to play again.");
timer.cancel();
}
//Horizontal 3rd
else if (space7.getText() == "X" && space8.getText() == "X" && space9.getText() == "X")
{
space7.setStyle("-fx-background-color:#90EE90");
space8.setStyle("-fx-background-color:#90EE90");
space9.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 1 has won, click reset to play again.");
timer.cancel();
}
else if (space7.getText() == "O" && space8.getText() == "O" && space9.getText() == "O")
{
space7.setStyle("-fx-background-color:#90EE90");
space8.setStyle("-fx-background-color:#90EE90");
space9.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 2 has won, click reset to play again.");
timer.cancel();
}
//Diagonal 2nd
else if (space3.getText() == "X" && space5.getText() == "X" && space7.getText() == "X")
{
space3.setStyle("-fx-background-color:#90EE90");
space5.setStyle("-fx-background-color:#90EE90");
space7.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 1 has won, click reset to play again.");
timer.cancel();
}
else if (space3.getText() == "O" && space5.getText() == "O" && space7.getText() == "O")
{
space3.setStyle("-fx-background-color:#90EE90");
space5.setStyle("-fx-background-color:#90EE90");
space7.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 2 has won, click reset to play again.");
timer.cancel();
}
}
}
};
timer.schedule(task, 1000, 1000);
}
}
/*
This method is the part of the program that will get which of the 9 buttons was clicked and based on who's player turn it is will change the text of the button to either an
X for player 1 or an O for player 2. afterwards the second player will go
*/
public void playTile(ActionEvent e)
{
Button temp = (Button)e.getSource();
Image tempImageX = new Image(getClass().getResource("X.jpg").toString());
Image tempImageO = new Image(getClass().getResource("O.png").toString());
if(temp.getText().equals("X"))
{
try
{
Stage screen1 = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("errorScreen.fxml"));
screen1.setTitle("Error!");
screen1.setScene(new Scene(root, 300, 275));
screen1.show();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
else if (temp.getText().equals("O"))
{
try
{
Stage screen1 = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("errorScreen.fxml"));
screen1.setTitle("Error!");
screen1.setScene(new Scene(root, 300, 275));
screen1.show();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
else
{
if (!temp.isDisable())
{
if (playerLabel.getText().equals("Player 1"))
{
playerLabel.setText("Player 2");
tempImageView.setImage(tempImageX);
temp.setGraphic(tempImageView);
tempImageView.setId("X");
}
else
{
playerLabel.setText("Player 1");
tempImageView.setImage(tempImageO);
temp.setGraphic(tempImageView);
tempImageView.setId("O");
}
}
}
}
/*
This method is the functionality of the reset button when clicked it will
reset all the 9 "Tiles" buttons text to blank and from that also reset the
player label back to "Player 1:
to continue the game.
*/
public void resetButton(ActionEvent event)
{
playerLabel.setText("Player 1");
Timer timer = new Timer();
TimerTask task = new TimerTask() {
#Override
public void run() {
initialize();
timer.cancel();
}
};
timer.schedule(task, 1000, 1000);
winnerLabel.setText("");
space1.setDisable(false);
space2.setDisable(false);
space3.setDisable(false);
space4.setDisable(false);
space5.setDisable(false);
space6.setDisable(false);
space7.setDisable(false);
space8.setDisable(false);
space9.setDisable(false);
space1.setTextFill(Paint.valueOf("000000"));
space2.setTextFill(Paint.valueOf("000000"));
space3.setTextFill(Paint.valueOf("000000"));
space4.setTextFill(Paint.valueOf("000000"));
space5.setTextFill(Paint.valueOf("000000"));
space6.setTextFill(Paint.valueOf("000000"));
space7.setTextFill(Paint.valueOf("000000"));
space8.setTextFill(Paint.valueOf("000000"));
space9.setTextFill(Paint.valueOf("000000"));
space1.setGraphic(null);
space2.setGraphic(null);
space3.setGraphic(null);
space4.setGraphic(null);
space5.setGraphic(null);
space6.setGraphic(null);
space7.setGraphic(null);
space8.setGraphic(null);
space9.setGraphic(null);
}
}
As far as I understand the question, you dont want to set the text to the buttons and you want to check what button it is based on the graphic it has(either X or O).
If this is the question, I would recommend to set/update a property of the button based on the type of the image/graphic it is holding, something like
button.getProperties().put("TYPE","X"); // For X button
button.getProperties().put("TYPE","O"); // For O button
And you can check directly for the type, rather than accessing the graphic. like..
if(space1.getProperties().get("TYPE").equals("X") && space2.getProperties().get("TYPE").equals("X") && ...){
...
}
Alternatively you can utilize the setUserData() method of the Node which internally uses the above logic only. If you go with userData your code will be less verbose also.
button.setUserData("X");
if(space1.getUserData().equals("X") && space2.getUserData().equals("X") && ...){
...
}
There is no way to keep the file name information in an ImageView or Image. Of course, you can always extend from either of these 2 classes and store the file name as a field, but you probably don't need that.
You can also keep the references of the two Image instances and check for reference instead.
Image imageX = new Image(getClass().getResource("X.jpg").toString());
Image imageO = new Image(getClass().getResource("O.png").toString());
public void playTile(ActionEvent e)
{
// Other stuff
Image currentImage = tempImageView.getImage();
if (currentImage == imageX || currentImage == imageO) // Checking for reference
{
// Your logic
}
else
{
// Your logic
}
}
The most interesting thing here is that all you wanted to do is to check whether the "tile" has been filled. The simplest way to do this is actually to do if (tempImageView.getImage() == null).
There is a way to get the file name of a button's set graphic:
In this example, when you click on a button, it will execute code in the if statement when the image file source name matches an indicated string (the file name you are comparing)
Note: If the button has not had a graphic set, attempting to use the contains method as used below will cause a NullPointerException. It is advisable to surround the code in a try/catch block if this risk applies in your situation.
public void handler(ActionEvent e)
{
//image file you want to check
String name = "X.jpg";
//button reference within handler
Button button = (Button)e.getSource();
//ImageView reference from button
ImageView buttonGraphic = (ImageView)button.getGraphic();
//check if buttons image file directory contains X.jpg
if(buttonGraphic.getImage().getUrl().contains(name))
{
System.out.println("Button has image X.jpg");
}
}
Related
I'm trying to show ads every few button presses, the first two times it works fine but it never gets to show a third.
Is my code wrong? I'm using Vscode and Android Studio.
This is the code I am using:
InterstitialAd _interstitialAd;
static bool _isInterstitialAdReady = false;
InterstitialAd _createInterstitialAd = new InterstitialAd(
adUnitId: Ads.interstitial,
listener: (MobileAdEvent event) {},
);
#override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback(_afterLayout);
FirebaseAdMob.instance.initialize(appId: Ads.appId);
_interstitialAd = InterstitialAd(
adUnitId: Ads.interstitial,
listener: (MobileAdEvent event) {
switch (event) {
case MobileAdEvent.loaded:
_isInterstitialAdReady = true;
break;
case MobileAdEvent.failedToLoad:
_isInterstitialAdReady = false;
break;
case MobileAdEvent.closed:
_interstitialAd.dispose();
_interstitialAd = _createInterstitialAd..load();
break;
default:
}
});
_interstitialAd.load();
}
#override
void dispose() {
// Add these to dispose to cancel timer when user leaves the app
_interstitialAd?.dispose();
super.dispose();
}
Call method:
if (ButtonCounter.rouletteCounter >= 10 && _isInterstitialAdReady) {
_interstitialAd
..load()
..show();
ButtonCounter.rouletteCounter = 0;
} else {
ButtonCounter.rouletteCounter++;
print('_isInterstitialReady: $_isInterstitialAdReady');
}
I need my web view to be tappable and scrolable. Once I implement on touch the scroll doesnt work. This way i managed to get it working however now i dont know how to make the web view tappable? the ButtonPress does nothing and if i use Move then i am just scrolling
This my my render in mu droid project
class ExtendedWebViewClient : WebViewClient
{
WebView _webView;
public async override void OnPageFinished(WebView view, string url)
{
try
{
_webView = view;
if (_xwebView != null)
{
view.Settings.JavaScriptEnabled = true;
await Task.Delay(100);
string result = await _xwebView.EvaluateJavaScriptAsync("(function(){return document.body.scrollHeight;})()");
_xwebView.HeightRequest = Convert.ToDouble(result);
}
base.OnPageFinished(view, url);
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}");
}
}
public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, IWebResourceRequest request)
{
return true;
}
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);
_xwebView = e.NewElement as ExtendedWebView;
_webView = Control;
if (e.OldElement == null)
{
_webView.SetWebViewClient(new ExtendedWebViewClient());
}
if (e.OldElement != null)
{
Control.Touch -= ControlOnTouch;
Control.ScrollChange -= ControlOnScrollChange;
}
if (e.NewElement != null)
{
Control.Touch += ControlOnTouch;
Control.ScrollChange += ControlOnScrollChange;
}
}
private void ControlOnScrollChange(object sender, ScrollChangeEventArgs scrollChangeEventArgs)
{
if (scrollChangeEventArgs.ScrollY > 0 && scrollChangeEventArgs.OldScrollY == 0)
{
Control.Parent.RequestDisallowInterceptTouchEvent(true);
}
}
private void ControlOnTouch(object sender, Android.Views.View.TouchEventArgs e)
{
// Executing this will prevent the Scrolling to be intercepted by parent views
switch (e.Event.Action)
{
case MotionEventActions.Down:
Control.Parent.RequestDisallowInterceptTouchEvent(true);
break;
case MotionEventActions.Up:
Control.Parent.RequestDisallowInterceptTouchEvent(false);
break;
case MotionEventActions.ButtonPress:
Console.WriteLine("press");
break;
case MotionEventActions.Mask:
Console.WriteLine("mask");
break;
}
// Calling this will allow the scrolling event to be executed in the WebView
Control.OnTouchEvent(e.Event);
}
Instead of using the gesture recognizer on your webview, you can use the Focused event like following . It will been invoked when you tap the WebView .
var wb = new WebView
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
Source = "xxx",
};
wb.Focused += (sender, event) =>
{
//Handle your logic here!
wb.Unfocus();
};
Unfocus() is used if you want to implement your logic everytime the webview is tapped.
my code as follows, i followed the example on the qt doc but nothing is being drawn on my widget, any one knows whats wrong? Thanks!
ui.axWidget_X->installEventFilter(this);
bool qtTest::eventFilter(QObject * obj, QEvent * event)
{
if((QAxWidget *)obj == ui.axWidget_X && ((QMouseEvent*)event)->button() == Qt::LeftButton)
{
if(event->type()== QEvent::MouseButtonPress)
{
origin = ((QMouseEvent*)event)->Pos();
if (!rubberBand)
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
rubberBand->setGeometry(QRect(origin, QSize()));
rubberBand->show();
return true;
}else if(event->type()== QEvent::MouseButtonRelease)
{
rubberBand->hide();
//
return true;
}else if(event->type() == QEvent::MouseMove)
{
rubberBand->setGeometry(QRect(origin, ((QMouseEvent*)event)>Pos()).normalized());
return true;
}
}
}
Your mousemove event handler is placed within the left button click checking parent condition. Since for a MouseMove event "((QMouseEvent*)event)->button() == Qt::LeftButton" will return false, the MouseMove event is not handled..
Solution:
Move the "else if" part handling MouseMove outside the if condition.
I am trying to bind two separate event to scene in javafx .
scene.addEventHandler(MouseEvent.MOUSE_RELEASED, new EventHandler<Event>() {
#Override
public void handle(Event event) {
......Drag Content Task.......
}
});
So How can get this two Events any Suggestion.
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if(event.getClickCount() == 2){
System.out.println("Call Dbl Click Eve");
Zooming....Task.....
}
});
I wrote some code and it works for me !
Using Current Time calculate time betwwen two click events if it less than dblclick expected then consider double click..
scene.addEventHandler(MouseEvent.ANY, e -> {
if (e.getEventType().equals(MouseEvent.DRAG_DETECTED)) {
System.out.println("Drag Event call ");
drag_Flag=true;
}
if (e.getEventType().equals(MouseEvent.MOUSE_RELEASED) && drag_Flag) {
System.out.println("Rellease Event call");
drag_Flag=false;
System.out.println("Drag Event Flag : "+drag_Flag);
time1=0;
time2=0;
} else if (e.getEventType().equals(MouseEvent.MOUSE_CLICKED) && !drag_Flag) {
long diff = 0;
if(time1==0)
time1=System.currentTimeMillis();
else
time2=System.currentTimeMillis();
if(time1!=0 && time2!=0)
diff=time2-time1;
if((diff/1000)<=215 && diff>0)
{
isdblClicked=true;
}
else
{
isdblClicked=false;
}
System.out.println("IsDblClicked()"+isdblClicked);
if(isdblClicked==true)
{
ZoomInOutPojo z = null;
do_Zoom();
time1=time2=0;
isdblClicked=false;
}
else
{
statement1;
}
}
drag_Flag=false;
}
});
Solution Works for My Case.
static boolean drag_Flag=false
scene.addEventHandler(MouseEvent.ANY, e -> {
if (e.getEventType().equals(MouseEvent.DRAG_DETECTED)) {
System.out.println("Drag Event call ");
drag_Flag=true;
}
if (e.getEventType().equals(MouseEvent.MOUSE_RELEASED) && drag_Flag) {
System.out.println("Rellease Event call");
drag_Flag=false;
System.out.println("Drag Event Flag : "+drag_Flag);
time1=0;
time2=0;
} else if (e.getEventType().equals(MouseEvent.MOUSE_CLICKED) && !drag_Flag) {
long diff = 0;
if(time1==0)
time1=System.currentTimeMillis();
else
time2=System.currentTimeMillis();
if(time1!=0 && time2!=0)
diff=time2-time1;
if((diff/1000)<=215 && diff>0)
{
isdblClicked=true;
}
else
{
isdblClicked=false;
}
System.out.println("IsDblClicked()"+isdblClicked);
if(isdblClicked==true)
{
ZoomInOutPojo z = null;
do_Zoom();
time1=time2=0;
isdblClicked=false;
}
else
{
statement1;
}
}
drag_Flag=false;
}
});
Well the way you posted it should already work, another way to do it would be:
scene.addEventHandler(MouseEvent.ANY, e -> {
if (e.getEventType().equals(MouseEvent.MOUSE_RELEASED)) {
// do something
} else if (e.getEventType().equals(MouseEvent.MOUSE_CLICKED)) {
// do something else
}
});
You can use the following code too. Try it.
EventHandler<MouseEvent> clickEvent = new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if(event.getClickCount() == 2){
System.out.println("Call Dbl Click Eve");
}
}
};
EventHandler<MouseEvent> releaseEvent = new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println("Rele");
}
};
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, clickEvent);
scene.addEventHandler(MouseEvent.MOUSE_RELEASED, releaseEvent);
I hope it will help you.
I use this code to draw a rectangle while dragging the mouse. The problem is that i only can draw from left down to the right.
Here is my code:
public class functionalTest extends Application {
BorderPane pane;
Rectangle rect;
Group group;
SimpleDoubleProperty rectinitX = new SimpleDoubleProperty();
SimpleDoubleProperty rectinitY = new SimpleDoubleProperty();
SimpleDoubleProperty rectX = new SimpleDoubleProperty();
SimpleDoubleProperty rectY = new SimpleDoubleProperty();
#Override
public void start(Stage stage) {
pane = new BorderPane();
Scene scene = new Scene(pane, 800, 600);
stage.setScene(scene);
group = new Group();
Circle circle = new Circle(200, 200, 25);
circle.setFill(Color.HOTPINK);
group.getChildren().add(circle);
scene.setOnMouseDragged(mouseHandler);
scene.setOnMousePressed(mouseHandler);
scene.setOnMouseReleased(mouseHandler);
rect = getNewRectangle();
rect.widthProperty().bind(rectX.subtract(rectinitX));
rect.heightProperty().bind(rectY.subtract(rectinitY));
pane.getChildren().add(rect);
pane.getChildren().add(group);
stage.show();
ArrayList<Node> containedNodesArray = new ArrayList<Node>();
containedNodesArray = Main.dragBoxSelection(group, rect);
if (containedNodesArray.size() > 0) {
System.out.println("Success");
}
}
EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) {
rect.setX(mouseEvent.getX());
rect.setY(mouseEvent.getY());
rectinitX.set(mouseEvent.getX());
rectinitY.set(mouseEvent.getY());
} else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) {
rectX.set(mouseEvent.getX());
rectY.set(mouseEvent.getY());
System.out.println("RECTX"+rectX.getValue());
System.out.println("RECTY"+rectY.getValue());
ArrayList<Node> containedNodesArray = new ArrayList<Node>();
containedNodesArray = Main.dragBoxSelection(group, rect);
if (containedNodesArray.size() > 0) {
System.out.println("Success");
}
} else if (mouseEvent.getEventType() == MouseEvent.MOUSE_RELEASED) {
// Clone the rectangle
Rectangle r = getNewRectangle();
r.setX(rect.getX());
r.setY(rect.getY());
r.setWidth(rect.getWidth());
r.setHeight(rect.getHeight());
pane.getChildren().add(r);
// Hide the rectangle
rectX.set(0);
rectY.set(0);
}
}
};
private Rectangle getNewRectangle() {
Rectangle r = new Rectangle();
r.setFill(Color.web("blue", 0.1));
r.setStroke(Color.BLUE);
return r;
}
public static void main(String[] args) {
launch(args);
}
}
Please can anyone help me draw in other direcions from right up to left, left up to the right and right down to he left.
Don't know if it worked for anyone but no luck for me. However, this does work for me in JavaFX 8:
if( event.getEventType() == MouseDragEvent.MOUSE_DRRAGGED ) {
double deltaX = event.getX() - initialPt.getX();
double deltaY = event.getY() - initialPt.getY();
if( deltaX < 0 ) {
rect.setX( event.getX() );
rect.setWidth( -deltaX );
}
else {
rect.setX( initialPt.getX() );
rect.setWidth( event.getX() - initialPt.getX() );
}
if( deltaY < 0 ) {
rect.setY( event.getY() );
rect.setHeight( -deltaY );
}
else {
rect.setY( initialPt.getY() );
rect.setHeight( event.getY() - initialPt.getY() );
}
}
No need to call setTranslateX() or setTranslateY(). Don't know if that could be related to which FX version you're using.
This is what you need to do:
When the mouse is dragged: get the dx = mouseEvent.getX() - initial X; if dx < 0 then you need to setTranslateX(dx) and setWidth(-dx)
else setTranslateX(0) and setWidth(dx)
Do the same for the Y coordinate, however this time use height not width;
This is all.
I replaced the code to be like this:
EventHandler mouseHandler = new EventHandler() {
#Override
public void handle(MouseEvent mouseEvent) {
if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) {
rectinitX.set(mouseEvent.getX());
rectinitY.set(mouseEvent.getY());
} else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) {
Double width = mouseEvent.getX() - rectinitX.getValue();
if (width < 0) {
rectX.set(mouseEvent.getX());
rect.setTranslateX(width);
rect.widthProperty().bind(rectinitX.subtract(rectX));
System.out.println(rect.getWidth());
} else {
rectX.set(mouseEvent.getX());
rect.setTranslateX(0);
rect.widthProperty().bind(rectX.subtract(rectinitX));
}
} else if (mouseEvent.getEventType() == MouseEvent.MOUSE_RELEASED) {
// Clone the rectangle
Rectangle r = getNewRectangle();
r.setX(rect.getX());
r.setY(rect.getY());
r.setWidth(rect.getWidth());
r.setHeight(rect.getHeight());
pane.getChildren().add(r);
}
}
};
I only wanted to try your solution for the width vaues at first but it doesn't seem to work. Do you have any comments on my implementation?
You are missing the height parameters.
if (event.getEventType() == MouseEvent.MOUSE_PRESSED) {
rect.setX(event.getX());
rect.setY(event.getY());
rectinitX.set(event.getX());
rectinitY.set(event.getY());
} else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
Double dx = event.getX() - rectinitX.getValue();
Double dy = event.getY() - rectinitY.getValue();
if (dx < 0) {
rectX.set(event.getX());
rect.setTranslateX(dx);
rect.widthProperty().bind(rectinitX.subtract(rectX));
} else {
rectX.set(event.getX());
rect.setTranslateX(0);
rect.widthProperty().bind(rectX.subtract(rectinitX));
}
if(dy < 0) {
rectY.set(event.getY());
rect.setTranslateY(dy);
rect.heightProperty().bind(rectinitX.subtract(rectX));
} else {
rectY.set(event.getY());
rect.setTranslateY(0);
rect.heightProperty().bind(rectY.subtract(rectinitY));
}
} else if (event.getEventType() == MouseEvent.MOUSE_RELEASED) {
// Clone the rectangle
Rectangle r = getNewRectangle();
r.setX(rect.getX());
r.setY(rect.getY());
r.setWidth(rect.getWidth());
r.setHeight(rect.getHeight());
pane.getChildren().add(r);
// Hide the rectangle
rectX.set(0);
rectY.set(0);
}}}};
This is the exact code for drawing in different directions except doesn´t work unfortunately from left bottom to right top and I cannot figure it out.
Works perfectly in default aka left to right but there is a code bug when drawing right to left.
If someone could figure it out, I´ll be much appricated
it should read
if(dy < 0)
{
rectY.set(mouseEvent.getY());
rect.setTranslateY(dy);
rect.heightProperty().bind(rectinitY.subtract(rectY));
}