Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Weird Error With two classes that envolves picking File Paths (oraclebook)

Andre LopesJul 31 2012 — edited Aug 2 2012
Hi Guys! i come here once again to ask help , please !!!

As you may know, im currenly studying that book from oracle, but my two classes that pick paths are giving me some errors.
Can someone have a look for me ? I didnt change the codes at all!
package CapituloII;

/**
 *
 * 
 */

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcBuilder;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.RectangleBuilder;
import javafx.stage.Stage;

/**
 * Creating Images
 * @author cdea
 */
public class Pagina70 extends Application {
    private List<String> imageFiles = new ArrayList<>();
    private int currentIndex = -1;
    public enum ButtonMove {NEXT, PREV};

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

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Chapter 2-1 Creating a Image");
        Group root = new Group();
        Scene scene = new Scene(root, 551, 400, Color.BLACK);
        
        
        // image view
        final ImageView currentImageView = new ImageView();
        
        // maintain aspect ratio
        currentImageView.setPreserveRatio(true);
        
        // resize based on the scene
        currentImageView.fitWidthProperty().bind(scene.widthProperty());
        
        final HBox pictureRegion = new HBox();
        pictureRegion.getChildren().add(currentImageView);
        root.getChildren().add(pictureRegion);
        
        // Dragging over surface
        scene.setOnDragOver(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent event) {
                Dragboard db = event.getDragboard();
                if (db.hasFiles()) {
                    event.acceptTransferModes(TransferMode.COPY);
                } else {
                    event.consume();
                }
            }
        });
        
        // Dropping over surface
        scene.setOnDragDropped(new EventHandler<DragEvent>() {

            @Override
            public void handle(DragEvent event) {
                Dragboard db = event.getDragboard();
                boolean success = false;
                if (db.hasFiles()) {
                    success = true;
                    String filePath = null;
                    for (File file:db.getFiles()) {
                        filePath = file.getAbsolutePath();
                        currentIndex +=1;
                        imageFiles.add(currentIndex, filePath);
                    }
                    
                    // set new image as the image to show.
                    Image imageimage = new Image(filePath);
                    currentImageView.setImage(imageimage);
                        
                }
                event.setDropCompleted(success);
                event.consume();
            }
        });

        
        // create slide controls
        Group buttonGroup = new Group();
        
        // rounded rect
        Rectangle buttonArea = RectangleBuilder.create()
                .arcWidth(15)
                .arcHeight(20)
                .fill(new Color(0, 0, 0, .55))
                .x(0)
                .y(0)
                .width(60)
                .height(30)
                .stroke(Color.rgb(255, 255, 255, .70))
                .build();
        
        buttonGroup.getChildren().add(buttonArea);
        // left control
        Arc leftButton = ArcBuilder.create()
                .type(ArcType.ROUND)
                .centerX(12)
                .centerY(16)
                .radiusX(15)
                .radiusY(15)
                .startAngle(-30)
                .length(60)
                .fill(new Color(1,1,1, .90))
                .build();
        
        leftButton.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
            public void handle(MouseEvent me) {                
                int indx = gotoImageIndex(ButtonMove.PREV);
                if (indx > -1) {
                    String namePict = imageFiles.get(indx);
                    final Image image = new Image(new File(namePict).getAbsolutePath());
                    currentImageView.setImage(image);
                }
            }
        });
        buttonGroup.getChildren().add(leftButton);
        
        // right control
        Arc rightButton = ArcBuilder.create()
                .type(ArcType.ROUND)
                .centerX(12)
                .centerY(16)
                .radiusX(15)
                .radiusY(15)
                .startAngle(180-30)
                .length(60)
                .fill(new Color(1,1,1, .90))
                .translateX(40)
                .build();
        buttonGroup.getChildren().add(rightButton);
        
        rightButton.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
            public void handle(MouseEvent me) {                
                int indx = gotoImageIndex(ButtonMove.NEXT);
                if (indx > -1) {
                    String namePict = imageFiles.get(indx);
                    final Image image = new Image(new File(namePict).getAbsolutePath());
                    currentImageView.setImage(image);
                }
            }
        });
        
        // move button group when scene is resized
        buttonGroup.translateXProperty().bind(scene.widthProperty().subtract(buttonArea.getWidth() + 6));
        buttonGroup.translateYProperty().bind(scene.heightProperty().subtract(buttonArea.getHeight() + 6));
        root.getChildren().add(buttonGroup);
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    /**
     * Returns the next index in the list of files to go to next.
     * 
     * @param direction PREV and NEXT to move backward or forward in the list of 
     * pictures.
     * @return int the index to the previous or next picture to be shown.
     */
    public int gotoImageIndex(ButtonMove direction) {
        int size = imageFiles.size();
        if (size == 0) {
            currentIndex = -1;
        } else if (direction == ButtonMove.NEXT && size > 1 && currentIndex < size - 1) {
            currentIndex += 1;
        } else if (direction == ButtonMove.PREV && size > 1 && currentIndex > 0) {
            currentIndex -= 1;
        }

        return currentIndex;
    }
    
}
Error Generated when i drag & drop :

WARNING : It didnt let me copy the first two lines and last two lines of the error:

>




java.lang.IllegalArgumentException: Invalid URL: unknown protocol: c
at javafx.scene.image.Image.validateUrl(Unknown Source)
at javafx.scene.image.Image.<init>(Unknown Source)
at CapituloII.Pagina70$2.handle(Pagina70.java:96)
at CapituloII.Pagina70$2.handle(Pagina70.java:80)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$DnDGesture.fireEvent(Unknown Source)
at javafx.scene.Scene$DnDGesture.processTargetDrop(Unknown Source)
at javafx.scene.Scene$DnDGesture.access$6500(Unknown Source)
at javafx.scene.Scene$DropTargetListener.drop(Unknown Source)
at com.sun.javafx.tk.quantum.GlassSceneDnDEventHandler.handleDragDrop(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleDragDrop(Unknown Source)
at com.sun.glass.ui.View.handleDragDrop(Unknown Source)
at com.sun.glass.ui.View.notifyDragDrop(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
at com.sun.glass.ui.win.WinApplication$2$1.run(Unknown Source)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(URL.java:590)
at java.net.URL.<init>(URL.java:480)
at java.net.URL.<init>(URL.java:429)
... 27 more
Exception in thread "JavaFX Application Thread" E
This post has been answered by Daniel on Aug 1 2012
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 30 2012
Added on Jul 31 2012
2 comments
956 views