Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Norm

Thread: Rectangle ArrayList errorLog

  1. #1
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    2

    Default Rectangle ArrayList errorLog

    Instead of creating several textures one-by-one, I have decided to create a loop who would fill my arrayList with rectangles.
    Later, once I try to display all rectangles from arraylist - program crashes.
    ( However if my secondary loop index(i) is set 'i<1' the program do not crash, but it only displays last element of arrayList only )



    Java FX Code:
    ArrayList<Rectangle> bricks = new ArrayList<>();
    		int x = 55;
    		int y = 55;
    		for ( int i = 0; i < 3; i++ )
    		{
    			y= y + 50;
    			bricks.add(picture.getBricks(x, y));		
    		}
    
                 for ( int i = 0; i < 3; i++ )
    		 {		
    		 group.getChildren().addAll ( bricks.get(i) );	 
    		 }
    Exception in Application start method
    java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchAppl icationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchAppl ication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
    Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchAppl ication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$lau nchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Group@18446e3
    at javafx.scene.Parent$2.onProposedChange(Parent.java :454)
    at com.sun.javafx.collections.VetoableListDecorator.a ddAll(VetoableListDecorator.java:234)
    at com.sun.javafx.collections.VetoableListDecorator.a ddAll(VetoableListDecorator.java:103)
    at application.Main.start(Main.java:65)
    at com.sun.javafx.application.LauncherImpl.lambda$lau nchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$run AndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$nul l$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$run Later$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run( InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Nativ e Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$14 8(WinApplication.java:191)
    ... 1 more
    Exception running application application.Main
    Last edited by asdfg; 08-27-2017 at 10:15 PM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,965
    Rep Power
    7

    Default Re: Rectangle ArrayList errorLog

    You are adding a child that is already in the scene. You can't add a node multiple times. Apparently, the bricks collection contains the same objects.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    2

    Default Re: Rectangle ArrayList errorLog

    I dont get. I try to create several 'bricks', because I need alot of them. They are similar, just diffrend XY coordinates.

    Java FX Code:
                          Rectangle bricks = new Rectangle(40, 40);
    				Image img3 = new Image ("bricks.jpg");
    				ImagePattern brickFill = new ImagePattern(img3);
    				
    				public Rectangle getBricks ( int x, int y) 
    				{	
    				bricks.setTranslateX(x);	
    				bricks.setTranslateY(y);
    				bricks.setFill(brickFill);	
    			    return bricks;
    				}

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,948
    Rep Power
    30

    Default Re: Rectangle ArrayList errorLog

    Only one Rectangle object is created at line 1. The calls to the set methods changes the values in that one object. Move line 1 inside the method so that a new Rectangle is created each time the method is called.
    asdfg likes this.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,193
    Rep Power
    12

    Default Re: Rectangle ArrayList errorLog

    In addition to what Norm said, your translate method, as used would continue to add x and y to the previous coordinates, not set those coordinates as passed. So if you just want to create a rectangle at location (x,y) then use the Rectangle(x,y,width, height) constructor inside the method. No need to mess with translate.

    I know that the setTranslate method for this class in JavaFX has other advantages but I do not see them being applied in this context.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Spring errorLog
    By asdfg in forum Spring
    Replies: 3
    Last Post: 03-12-2017, 01:16 PM
  2. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  3. Replies: 6
    Last Post: 11-30-2010, 11:29 PM
  4. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  5. Wrong with Rectangle res = new Rectangle(0,0,0,0);???
    By jiapei100 in forum AWT / Swing
    Replies: 3
    Last Post: 09-25-2010, 03:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •