o alcance do mouse dentro da janela,
impedindo assim que os objetos mergulhe
desaparecendo do scene.
Veja abaixo uma imagem do programa em execução:
Veja abaixo um vídeo com o programa funcionando:
Veja abaixo o código do programa:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.util.ArrayList;
import javafx.scene.shape.*;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.media.AudioClip;
public class PROJETO extends Application {
Group root = new Group ( );
Scene scene = new Scene ( root, 600, 300 );
Canvas canvas = new Canvas ( 600, 300 );
GraphicsContext ctx = canvas.getGraphicsContext2D ( );
Circle circle_1 = new Circle ( 30 );
Circle circle_2 = new Circle ( 30 );
Circle circle_3 = new Circle ( 30 );
double x = 0, y = 0;
double k = 0;
double w = 0;
private ArrayList < Shape > nosObjct;
String Audio = getClass ( ).getResource (
"gun.wav" ).toString ( );
AudioClip clip = new AudioClip ( Audio );
//
/////////////////////////////////////////////////////////////////////////
public void Informe ( ) {
ctx.setFont ( Font.font ( "Arial", FontWeight.BOLD, 13 ) );
ctx.setFill ( Color.RED );
ctx.fillText ( "Por: ", 200, 230 );
ctx.setFill ( Color.BLUE );
ctx.fillText ( "Samuel
Lima", 240, 230 );
ctx.setFill ( Color.BLACK );
ctx.fillText ( "sa_sp10@hotmail.com", 200, 245 );
ctx.setFill ( Color.RED );
ctx.fillText ( " MUITO
OBRIGADO", 250, 280 );
}
//
/////////////////////////////////////////////////////////////////////////
public void arrastaObjetos ( final Shape figuras ) {
figuras.setOnMousePressed ( new EventHandler < MouseEvent > ( ) {
@Override
public void handle ( MouseEvent mouseEvent ) {
x = figuras.getLayoutX ( ) - mouseEvent.getSceneX ( );
y = figuras.getLayoutY ( ) - mouseEvent.getSceneY ( );
figuras.setCursor ( Cursor.CROSSHAIR );
}
} );
figuras.setOnMouseReleased ( new EventHandler < MouseEvent > ( ) {
@Override
public void handle ( MouseEvent mouseEvent ) {
figuras.setCursor ( Cursor.HAND );
}
} );
figuras.setOnMouseDragged ( new EventHandler < MouseEvent > ( ) {
@Override
public void handle ( MouseEvent mouseEvent ) {
if ( mouseEvent.getSceneX ( ) > 50 &&
mouseEvent.getSceneX ( ) < 550 ) {
figuras.setLayoutX ( mouseEvent.getSceneX ( ) + x );
}
if ( mouseEvent.getSceneY ( ) > 50 &&
mouseEvent.getSceneY ( ) < 250 ) {
figuras.setLayoutY ( mouseEvent.getSceneY ( ) + y );
}
if ( mouseEvent.getSceneX ( ) == 50 ||
mouseEvent.getSceneX ( ) == 550 ) {
}
if ( mouseEvent.getSceneY ( ) == 50 ||
mouseEvent.getSceneY ( ) == 250 ) {
}
examineColisao ( figuras );
}
} );
}
//
/////////////////////////////////////////////////////////////////////////
private void examineColisao ( Shape figuras) {
boolean colisao = false;
for ( Shape blocoEstatico : nosObjct ) {
if ( blocoEstatico != figuras ) {
Shape intersect = Shape.intersect ( figuras, blocoEstatico );
if ( intersect.getBoundsInLocal ( ).getWidth ( ) != -1 ) {
colisao = true;
}
}
}
if ( colisao ) {
clip.play ( );
figuras.setFill ( Color.RED );
}
if ( !colisao ) {
clip.stop ( );
figuras.setFill ( Color.LIGHTCORAL );
}
}
//
/////////////////////////////////////////////////////////////////////////
public int listaFiguras ( ) {
nosObjct = new ArrayList < > ( );
circle_1.setCenterX ( 300.0 );
circle_1.setCenterY ( 125.0 );
circle_1.setRadius ( 30.0 );
circle_1.setFill ( Color.LIGHTCORAL );
circle_2.setCenterX ( 130.0 );
circle_2.setCenterY ( 225.0 );
circle_2.setRadius ( 30.0 );
circle_2.setFill ( Color.LIGHTCORAL );
circle_3.setCenterX ( 470.0 );
circle_3.setCenterY ( 225.0 );
circle_3.setRadius ( 30.0 );
circle_3.setFill ( Color.LIGHTCORAL );
nosObjct.add ( circle_1 );
nosObjct.add ( circle_2 );
nosObjct.add ( circle_3 );
for ( Shape moveFiguras : nosObjct ) {
arrastaObjetos ( moveFiguras );
}
examineColisao ( nosObjct.get ( nosObjct.size ( ) - 1 ) );
return 0;
}
//
/////////////////////////////////////////////////////////////////////////
@Override
public void start ( Stage stage ) {
stage.setTitle ( "JAVAFX -
LIMITANDO O ALCANCE DO MOUSE NA"
+ " JANELA E
DETECTANDO COLISÃO" );
ctx.setStroke ( Color.BLACK );
ctx.setLineWidth ( 10.0 );
ctx.strokeRoundRect ( 5, 5, 590, 290, 5, 5 );
ctx.setFont ( Font.font ( "Helvetica", FontWeight.BOLD,
FontPosture.ITALIC, 20 ) );
ctx.setFill ( Color.RED );
ctx.fillText ( "JAVAFX -
LIMITANDO O ALCANCE DO MOUSE\n\t"
+ "NA JANELA E
DETECTANDO COLISÃO", 90, 40 );
listaFiguras ( );
Informe ( );
root.getChildren ( ).addAll ( canvas, circle_1, circle_2, circle_3 );
stage.setScene ( scene );
stage.show ( );
}
//
/////////////////////////////////////////////////////////////////////////
public static void main ( String [ ] args ) {
launch ( args );
}
}
Nenhum comentário:
Postar um comentário
Observação: somente um membro deste blog pode postar um comentário.