Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... ·...

40
Posizionamento di componenti in Java FX

Transcript of Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... ·...

Page 1: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

PosizionamentodicomponentiinJavaFX

Page 2: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

PosizionamentodiunNode

•  Nonvengonofornitimetodiperilposizionamentoassoluto…– es.,setX,setY – sonotuttaviapresentiinalcunesottoclassi

•  …incompensosonodefinitiimetodisetLayoutX,setTranslateX •  Acosaservono?

Page 3: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

Posizionamentoassoluto:Pane public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); Button btn = new Button(); btn.setText("'Hello World'"); btn.setLayoutX(250); btn.setLayoutY(220); Pane pane= new Pane(); pane.getChildren().add(btn); Group root = new Group(pane); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); } Ingenerale,daevitare!

Diciamoallacomponentedovevogliamocheleisi

posizioni

Page 4: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

Aggiunta-rimozionedielementi

Attenzione!Aggiuntaerimozionedielementivengonoeffettuatidallalistadeifigli(children),conimetodi•  add(Nodex),addAll(Collection<Node>c),•  remove(Nodex),,removeAll(Collection<Node>c)Polygon triangolo= new Polygon(); triangolo.getPoints().addAll(new Double[]{ 0.0, 0.0,20.0, 10.0, 10.0, 20.0 }); Circle cerchio=new Circle(10,10,10); layout.getChildren().addAll(cerchio,triangolo); … layout.getChildren().remove(cerchio);

Page 5: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

PosizionamentodiunNode

•  Laresponsabilitàdiposizionareilcontenutoèdelegataadunacomponentespecifica.•  InFXèattribuitaaduntipoparticolaredicontenitori(Parent):iPane)

Page 6: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

FXvs.SwingandAndroidarchitectures

View

ViewGroup

Layout

Component

Container

Layout

Node

Parent

Pane

VarioustypesofLayouts

Android

Swing

JavaFX

Page 7: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

Posizionamentoautomaticomediantelayout

•  L’ideaèdisemplificareilcompitodelprogrammatoredefinendocontenitoridioggettichevengonoposizionatisecondoregoleprestabilite.

•  Ilcontenitorepuòignorareidesideridellacomponente(espressidasetLayoutX)

•  Ladisposizionedellecomponentiè“liquida”

Page 8: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

Posizionamentoautomaticomediantelayout

•  Fondamentalenelprogettodiinterfacce

•  JavaFXforniscenumerosi“layoutmanagers”

•  Letturaraccomandata:https://docs.oracle.com/javase/8/javafx/layout-tutorial/index.html

Page 9: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

Layoutmanagerspredefiniti

Page 10: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

Containerclassesthatautomatecommonlayoutmodels

•  TheHBoxclassarrangesitscontentnodeshorizontallyinasinglerow.•  TheVBoxclassarrangesitscontentnodesverticallyinasinglecolumn.•  TheStackPaneclassplacesitscontentnodesinaback-to-frontsingle

stack.•  TheTilePaneclassplacesitscontentnodesinuniformlysizedlayoutcells

ortiles•  TheFlowPaneclassarrangesitscontentnodesineitherahorizontalor

vertical“flow,”wrappingatthespecifiedwidth(forhorizontal)orheight(forvertical)boundaries.

•  TheBorderPaneclasslaysoutitscontentnodesinthetop,bottom,right,left,orcenterregion.

•  TheAnchorPaneclassenablesdeveloperstocreateanchornodestothetop,bottom,leftside,orcenterofthelayout.

•  TheGridPaneclassenablesthedevelopertocreateaflexiblegridofrowsandcolumnsinwhichtolayoutcontentnodes.

Toachieveadesiredlayoutstructure,differentcontainerscanbenestedwithinaJavaFXapplication.

Page 11: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

HBox public class Layout1 extends Application { public void start(Stage stage) { Pane layout = new HBox(); layout.getChildren().add(new Button("Uno")); layout.getChildren().add(new Button("Due")); layout.getChildren().add(new Button("Tre")); Group root = new Group(layout); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); } … }

Allineainorizzontale

Page 12: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

HBox public class Layout1 extends Application { public void start(Stage stage) { Pane layout = new HBox(); layout.getChildren().add(new Button("Uno")); layout.getChildren().add(new Button("Due")); layout.getChildren().add(new Button("Tre")); //Group root = new Group(layout); Scene scene = new Scene(layout); stage.setScene(scene); stage.show(); } … }

Nota:èpossibilecreareunaScenedirettamentedaunParentgenerico,quindi

anchedaunPaneosottoclasse,maattenzione:ognunohaunsuoallineamentopredefinito

Page 13: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

VBox

public class Layout1 extends Application { public void start(Stage stage) { Pane layout = new VBox(); layout.getChildren().add(new Button("Uno")); layout.getChildren().add(new Button("Due")); layout.getChildren().add(new Button("Tre")); Scene scene = new Scene(layout); stage.setScene(scene); stage.show(); } … }

Allineainverticale

Page 14: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

FlowPane public class Layout1 extends Application { public void start(Stage stage) { FlowPane layout = new FlowPane();

layout.setPrefWrapLength(100); layout.getChildren().add(new Button("Uno")); layout.getChildren().add(new Button("Due")); layout.getChildren().add(new Button("Tre")); stage.setScene(layout); stage.show(); }… }

Organizzaglielementiinunasequenzacontinua,cheva«acapo»aunadistanzaconfigurabile

Page 15: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

StackPane public class Layout1 extends Application { public void start(Stage stage) { StackPane layout = new StackPane(); layout.getChildren().add(new Button("Uno")); layout.getChildren().add(new Button("Due")); layout.getChildren().add(new Button("Tre")); Scene scene = new Scene(layout); stage.setScene(scene); stage.show(); } … }

Impilaglielementi

Page 16: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

StackPane public class Layout1 extends Application { public void start(Stage stage) { StackPane stack = new StackPane(); Circle helpIcon = new Circle(15, 15, 15); helpIcon.setFill(Color.YELLOW); helpIcon.setStroke(Color.GREEN); Text helpText = new Text("?"); helpText.setFont(Font.font("Verdana", FontWeight.BOLD, 18)); helpText.setFill(Color.WHITE); helpText.setStroke(Color.RED); stack.getChildren().addAll(helpIcon, helpText); stack.setAlignment(Pos.CENTER); Scene scene = new Scene(stack); stage.setTitle("My JavaFX Application"); stage.setScene(scene); stage.show(); } … }

StackPaneèutilepercreare«overlay»dielementi

Page 17: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

AnchorPane public void start(Stage stage) { AnchorPane layout = new AnchorPane(); Button buttonSave = new Button("Save"); Button buttonCancel = new Button("Cancel"); HBox hb = new HBox(); hb.setPadding(new Insets(0, 10, 10, 10)); hb.setSpacing(10); hb.getChildren().addAll(buttonSave, buttonCancel); Rectangle r=new Rectangle(100,100); layout.getChildren().addAll(r,hb); layout.setBottomAnchor(hb, 8.0); layout.setRightAnchor(hb, 5.0); layout.setTopAnchor(r, 10.0); layout.setLeftAnchor(r, 50.0); Scene scene = new Scene(layout); stage.setScene(scene); stage.show(); } Permettedi«ancorare»

elementiaunazona

Page 18: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

TilePane public class Layout1 extends Application { public void start(Stage stage) { TilePane layout = new TilePane(); layout.setVgap(10); layout.setHgap(20); layout.setPrefColumns(2); layout.getChildren().add(new Button("Uno")); layout.getChildren().add(new Button("Due")); layout.getChildren().add(new Button("Trentatre"));

Scene scene = new Scene(layout); stage.setScene(scene); stage.show(); } …}

Organizzaglielementiinunagrigliadicellediegualedimensione

Page 19: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

public void start(Stage stage) { GridPane layout = new GridPane(); int width=100; int height=40; Scene scene = new Scene(layout, width, height, Color.BLANCHEDALMOND); layout.add(new Text("0, 0"), 0, 0); layout.add(new Button("0, 1"), 0, 1); layout.add(new Text("1, 1"), 1, 1); Rectangle r = new Rectangle(80,30); layout.add(r, 1, 2); layout.add(new Button("1, 3"), 1, 3); layout.add(new Button("2,3"), 2, 3); layout.add(new Button("4, 0"), 4, 0); layout.add(new Text("4, 2"), 4, 2); ColumnConstraints column1 = new ColumnConstraints(100); ColumnConstraints column2 = new ColumnConstraints(); column2.setPercentWidth(40); column2.setHgrow(Priority.ALWAYS); layout.getColumnConstraints().addAll(column1, column2); stage.setScene(scene); stage.show(); }

GridPane

Organizzaglielementiinunagrigliadicuinonènecessariodaredimensioneprefissata

vedidocumentazione!

Page 20: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

Identificazionedell’elemento(i,j)inunGridPane

/***implementazionegeneraledelmetodopertrovarequaleelementisitrovi*inposizionei,jinunGridPane.*@paramdpilGridPaneincuicercare*@paramiriga*@paramjcolonna*@returnl'elementotrovato*/NodegetElementAt(GridPanegp,inti,intj){for(Nodex:gp.getChildren()){if((GridPane.getRowIndex(x)==i)&&

(GridPane.getColumnIndex(x)==j)){returnx;}}returnnull;}

GridPanehamoltimetodiinteressantieutili,manemancaunocherestituiscal’oggettochesitrovainposizionei,jnellamatrice.Possiamosupplirecosì:

Page 21: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

BorderPane public class Layout1 extends Application { public void start(Stage stage) { BorderPane layout=new BorderPane(); Button top = new Button("Top"); BorderPane.setAlignment(top, Pos.TOP_CENTER); layout.setTop(top); layout.setBottom(new Button("Bottom")); layout.setLeft(new Button("Left")); layout.setRight(new Button("Right")); layout.setCenter(new Button("Center")); Scene scene = new Scene(layout); stage.setScene(scene);

stage.show(); } … }

Organizzaglielementiin«zone»

Page 22: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

CombinazionidilayoutStackPane

BorderPane

HBox

VBox

GridPane

HBox

AnchorPane

FlowPane or TilePane

Page 23: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

Torniamoalposizionamento…

•  Èpossibilemodificareilposizionamentoautomaticodeglielementi– setLayoutXesetTranslateX(eanaloghiperlaY)

NOTA:ingenerelayoutXèstabilitodalcontenitore,cambiarloamanononserveanulla!

Page 24: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

DalleAPI…

•  Thenode'sfinaltranslationwillbecomputedaslayoutX+translateX,wherelayoutXestablishesthenode'sstablepositionandtranslateXoptionallymakesdynamicadjustmentstothatposition.

•  IfthenodeismanagedandhasaRegion(orsubclass)asitsparent,thenthelayoutregionwillsetlayoutXaccordingtoitsownlayoutpolicy.

•  IfthenodeisunmanagedorparentedbyaGroup,thentheapplicationmaysetlayoutXdirectlytopositionit.

Page 25: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

Esempiodiposizio-namentopublic class MoveBall extends Application{ @Override public void start(Stage primaryStage) { Circle c = new Circle(200, 200, 10); c.setFill(Color.AQUAMARINE); Button btn = new Button(); btn.setText("Move circle"); btn.setOnAction(new Controller(c)); VBox root = new Vbox(); root.getChildren().addAll(c, btn); Scene scene = new Scene(root, 100, 50); primaryStage.setScene(scene); primaryStage.show(); }

class Controller implements EventHandler<ActionEvent>{ int inc = 0; Circle c; Controller(Circle c) @Override public void handle(ActionEvent event) { inc += 10; c.setLayoutX(inc % 100); //c.setCenterX(inc % 100); //c.setTranslateX(inc % 100); System.out.println(c.getLayoutX() + " " + c.getTranslateX()+" "+c.getCenterX());}}

Page 26: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

SettandolaLayoutX…

class Controller implements EventHandler<ActionEvent>{ int inc = 0; Circle c; Controller(Circle c) @Override public void handle(ActionEvent event) { inc += 10; c.setLayoutX(inc % 100); //c.setCenterX(inc % 100); //c.setTranslateX(inc % 100); System.out.println(c.getLayoutX() + " " + c.getTranslateX()+" "+c.getCenterX());}}

Idaticambiano,manonfunziona:ilcerchiononsimuove!

Page 27: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

ProvandoconCenterX…

class Controller implements EventHandler<ActionEvent>{ int inc = 0; Circle c; Controller(Circle c) @Override public void handle(ActionEvent event) { inc += 10; //c.setLayoutX(inc % 100); c.setCenterX(inc % 100); //c.setTranslateX(inc % 100); System.out.println(c.getLayoutX() + " " + c.getTranslateX()+" "+c.getCenterX());}}

Nonfunzionaneanchecosì…

Page 28: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

efinalmenteconsetTranslateX…

class Controller implements EventHandler<ActionEvent>{ int inc = 0; Circle c; Controller(Circle c) @Override public void handle(ActionEvent event) { inc += 10; //c.setLayoutX(inc % 100); //c.setCenterX(inc % 100); c.setTranslateX(inc % 100); System.out.println(c.getLayoutX() + " " + c.getTranslateX()+" "+c.getCenterX());}}

Funziona!

Page 29: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

DimensionamentoeallineamentoÈpossibledimensionaredirettamenteglielementi–  Es.,btn.setPrefWidth(200);

•  …oppurespecificarevincolisulledimensioni–  Es.,btn.setMinWidth(100);

•  Peresempididimensionamentoeallineamento:https://docs.oracle.com/javase/8/javafx/layout-tutorial/size_align.htm

Page 30: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

FXML– noinonlotrattiamo.Anchegrazieall’ideadelposizionamentoliquido,èpossibiledefinirelecomponentifuoridalcodice,usandounaprogrammazionedichiarativa(filesXMLinAndroideFXMLinJavaFX)

<BorderPane id="borderPane" xmlns:fx=http://javafx.com/fxml prefHeight="200" prefWidth="320”> <stylesheets>

<URL value="@form.css"/> </stylesheets> <top> <Text text="Who are you?"/> </top> <center> <TextField id="textfield" fx:id="textfield"/> </center> </BorderPane>

Page 31: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

Quasiclassi:enum

Page 32: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

Esempio:isemidellecarte

CuoriPiccheCuoriPicche03-3CuoriQuadriFioriPicche

publicstaticvoidmain(Stringa[]){Semec=Seme.Cuori;Semep=Seme.Picche;System.out.println(c.name()+""+p.name());System.out.println(c+""+p);//usatoString()System.out.println(c.ordinal()+""+p.ordinal());System.out.println(c.compareTo(p));for(Semex:Seme.values()){System.out.println(x);}}

Soluzioneconleenum(def.ditipodidato erelativivaloriammessi!)

enumSeme{Cuori,Quadri,Fiori,Picche}

Soluzioneconlestringhe(def.divariabile)staticfinalStringseme[]{"Cuori","Quadri","Fiori","Picche"};

Page 33: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

Soluzioneesercitazione3

Page 34: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

Leenum

enum Valore { A, _2,_3,_4,_5,_6,_7,_8,_9,_10, J, Q,K};

enum Seme { Cuori, Quadri, Fiori, Picche}

Page 35: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

CartapublicclassCartaimplementsComparable{Valorevalore;Semeseme;publicCarta(Semeseme,Valorevalore){this.valore=valore;this.seme=seme;}@OverridepublicintcompareTo(Objectobj){if(this==obj)return0;if(obj==null||getClass()!=obj.getClass())System.err.println("ErrorincompareTo"+obj);finalCartaother=(Carta)obj;intv_comp=this.valore.compareTo(other.valore);if(v_comp!=0)returnv_comp;ints_comp=this.seme.compareTo(other.seme);//intv_comp=other.seme.ordinal()-this.seme.ordinal();returns_comp;}

@OverridepublicinthashCode(){//returnObjects.hash(valore,seme);inthash=3;hash=41*hash+Objects.hashCode(this.valore);hash=41*hash+Objects.hashCode(this.seme);returnhash;}@Overridepublicbooleanequals(Objectobj){if(this==obj)returntrue;if(obj==null)returnfalse;if(getClass()!=obj.getClass())returnfalse;finalCartaother=(Carta)obj;if(this.valore!=other.valore)returnfalse;if(this.seme!=other.seme)returnfalse;returntrue;}@OverridepublicStringtoString(){return"["+valore+","+seme+']’;}

Page 36: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

Carta–maindiprova

publicstaticvoidmain(Stringa[]){Cartac1=newCarta(Seme.Picche,Valore.K);Cartac2=newCarta(Seme.Cuori,Valore.K);System.out.println(c2);System.out.println(c1.compareTo(c2));}}

Page 37: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

ComparatoreDiCartePerSeme

publicclassComparatoreDiCartePerSemeimplementsComparator<Carta>{@Overridepublicintcompare(Cartac1,Cartac2){if(c1==c2)return0;if(c1==null||c2==null)System.err.println("Errorincompare"+c1+""+c2);ints_comp=c1.seme.compareTo(c2.seme);if(s_comp!=0)returns_comp;intv_comp=c1.valore.compareTo(c2.valore);returnv_comp;}}

Page 38: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

MazzopublicclassMazzoextendsArrayList<Carta>{/***creaunmazzocontenente*ncartepertipo*@paramnnumerodicopie*/Mazzo(intn){for(Semes:Seme.values()){for(Valorev:Valore.values()){for(inti=0;i<n;i++){add(newCarta(s,v));}}}}/***generaunmazzovuoto*/Mazzo(){}/***costruttoredicopia:creaunacopia*contenentelestessecarte*@parammmazzodacopiare*/Mazzo(Mazzom){this.addAll(m);}

voidmescola(){Collections.shuffle(this);}voidriordinaPerValore(){Collections.sort(this);}voidriordinaPerSeme(){Collections.sort(this,newComparatoreDiCartePerSeme());}@OverridepublicStringtoString(){Strings="";for(Cartac:this){s=s.concat(c.toString());}return"Mazzo{"+s+'}';}

Page 39: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

Mazzo–continua

publicCartacontieneCarteUguali(){Mazzom=newMazzo(this);//Mazzom=(Mazzo)(this.clone());m.riordinaPerValore();CartaprossimaCarta,questaCarta=null;Cartadoppia=null;Iterator<Carta>iter=m.iterator();//proteggidalmazzovuotoif(iter.hasNext())questaCarta=iter.next();while(iter.hasNext()){prossimaCarta=iter.next();if(questaCarta.equals(prossimaCarta)){doppia=questaCarta;break;}questaCarta=prossimaCarta;}if(doppia!=null){System.out.println("Haivinto."

+"Trovatocoppiauguale:"+doppia);}else{System.out.println("Haiperso:Nontrovatocoppiauguale.");}returndoppia;}

creaunacopiadelmazzoperpoterlariordinaresenzamodificareilmazzodiorigineseduecartesonouguali,sarannoadiacentinelmazzoriordinato

publicstaticvoidmain(Stringa[]){Mazzom=newMazzo(2);m.mescola();System.out.println(m);System.out.println("==========");m.riordinaPerSeme();System.out.println(m);System.out.println("==========");m.riordinaPerValore();System.out.println(m);}}

Page 40: Posizionamento di componenti in Java FXlatemar.science.unitn.it/segue_userFiles/2019Programma... · 2019-05-03 · • The AnchorPane class enables developers to create anchor nodes

Main

publicclassEsercitazione3a{Mazzom=null;publicstaticvoidmain(String[]args){newEsercitazione3a();}publicEsercitazione3a(){intnMazziBase=2;m=newMazzo(nMazziBase);m.mescola();Mazzomazzetto=newMazzo();mazzetto.addAll(m.subList(0,10));System.out.println(mazzetto);Cartadoppia=mazzetto.contieneCarteUguali();if(doppia!=null)controllaRaddoppio(doppia);}

privatevoidcontrollaRaddoppio(Cartadoppia){Randomrg=newRandom(System.currentTimeMillis());CartaaCaso=null;//dobbiamostareattentianonestrarreunacarta//appartenenteallacoppiadicarteugualigiàselezionatedo{intk=rg.nextInt(m.size());aCaso=m.get(k);}while(doppia.equals(aCaso));System.out.print("Estratto"+aCaso);if(aCaso.seme.equals(doppia.seme)){System.out.println("Raddoppiato!");}else{System.out.println("Raddoppiofallito!");}}}