2012-09-12 14 views
5

मैं जावाएफएक्स में स्केलिंग ट्रांसफॉर्म के साथ काम करने की कोशिश कर रहा हूं, लेकिन मेरे सिर को इसके चारों ओर लपेटने में काफी सक्षम नहीं हूं। असल में, मेरे पास एक जटिल ग्राफ वाला एक फलक है और इसे पुन: सहेजने में सक्षम होना चाहते हैं। स्केलिंग हिस्सा स्वयं ठीक काम करता है, हालांकि, संलग्न स्क्रॉल फलक ग्राफ़ को अनुकूलित नहीं करेगा।जावाएफएक्स और स्क्रॉलपैन में स्केलिंग

public class TestApp extends Application { 
    @Override public void start(final Stage stage) throws Exception { 
     final Label label = new Label("Hello World"); 
     label.getTransforms().setAll(new Scale(0.5, 0.5)); 

     label.setStyle("-fx-background-color:blue"); 
     label.setFont(new Font(200)); 

     final ScrollPane scrollPane = new ScrollPane(); 
     scrollPane.setContent(label); 

     stage.setScene(new Scene(scrollPane)); 
     stage.setWidth(200); 
     stage.setHeight(100); 
     stage.show(); 
    } 

    public static void main(String[] args) { 
     Application.launch(args); 
    } 
} 

लेबल सही ढंग से पैमाने पर होगा, लेकिन संलग्न स्क्रॉल फलक की सलाखों के अभी भी का एक घटक को समायोजित करेगा:

सादगी के लिए, मैं एक छोटी उदाहरण है, जिसमें मेरी ग्राफ एक लेबल की जगह पोस्ट करेंगे मूल आकार

मैं अब तक की कोशिश की है:

  • (कोई स्क्रॉलबार जो भी दिखाई देगा)
  • लेबल मिनट और वरी आकार
  • एक समूह के अंदर लेबल लपेटकर के साथ चारों ओर खेलने enclosing समूह स्केलिंग बल्कि लेबल

मुझे क्या याद आ रही है? स्क्रॉलपैन सामग्री दृश्य में अनुकूलित करने के लिए मैं क्या कर सकता हूं?

आपकी मदद के लिए धन्यवाद।

+0

स्केलेबल, pannable Scrollpane: https://stackoverflow.com/a/44314455/1386911 –

उत्तर

3

मैंने this example of scrollpane viewports, transforms and layout bounds in JavaFX में ग्राफ और अन्य नोड्स के लिए स्क्रोलपैन में स्केलिंग लागू की।

कोड लागू किया गया था जब मैं पहली बार जावाएफएक्स सीख रहा था, इसलिए निश्चित रूप से कोड क्लीनर हो सकता है और शायद इसे पूरा करने के सरल तरीके हैं (उदाहरण के लिए समूह को ScrollPane documentation में सुझाए गए स्केल किए गए नोड के कंटेनर के रूप में उपयोग करना)।

समाधान मैं चाहता था (स्क्रॉलबार ही प्रकट जब आप ज़ूम इन होते हैं और नोड दिखाई व्यूपोर्ट से बड़ा है) प्राप्त करने के लिए एक प्रमुख, था इस कोड:

// create a container for the viewable node. 
final StackPane nodeContainer = new StackPane(); 
nodeContainer.getChildren().add(node); 

// place the container in the scrollpane and adjust the pane's viewports as required. 
final ScrollPane scrollPane = new ScrollPane(); 
scrollPane.setContent(nodeContainer); 
scrollPane.viewportBoundsProperty().addListener(
    new ChangeListener<Bounds>() { 
    @Override public void changed(ObservableValue<? extends Bounds> observableValue, Bounds oldBounds, Bounds newBounds) { 
    nodeContainer.setPrefSize(
     Math.max(node.getBoundsInParent().getMaxX(), newBounds.getWidth()), 
     Math.max(node.getBoundsInParent().getMaxY(), newBounds.getHeight()) 
    ); 
    } 
}); 

... 

// adjust the view layout based on the node scalefactor. 
final ToggleButton scale = new ToggleButton("Scale"); 
scale.setOnAction(new EventHandler<ActionEvent>() { 
    @Override public void handle(ActionEvent actionEvent) { 
    if (scale.isSelected()) { 
     node.setScaleX(3); node.setScaleY(3); 
    } else { 
     node.setScaleX(1); node.setScaleY(1); 
    } 
    // runlater as we want to size the container after a layout pass has been performed on the scaled node. 
    Platform.runLater(new Runnable() { 
     @Override public void run() { 
     nodeContainer.setPrefSize(
      Math.max(nodeContainer.getBoundsInParent().getMaxX(), scrollPane.getViewportBounds().getWidth()), 
      Math.max(nodeContainer.getBoundsInParent().getMaxY(), scrollPane.getViewportBounds().getHeight()) 
     ); 
     } 
    }); 
    } 
}); 
+0

आपकी प्रतिक्रिया के लिए धन्यवाद। कोड कुछ अजीब साइड इफेक्ट्स उत्पन्न करता है (उदाहरण के लिए जब स्केल को कम किया जाता है, तो स्क्रॉल बार स्थानांतरित होने तक सही ढंग से अनुकूल नहीं होंगे), लेकिन दृष्टिकोण निश्चित रूप से सही है। इसने मेरी बहुत मदद की। – sarcan

8

को Scrollpane दस्तावेज़ को आप शायद अनुसार समूह में एक फलक को लपेटने का प्रयास करें ताकि स्क्रॉलपैन दृश्यमान द्वारा स्क्रॉल किया गया हो, वास्तविक लेआउट बाध्य न हो।

ScrollPane layout calculations are based on the layoutBounds rather than the 
boundsInParent (visual bounds) of the scroll node. If an application wants the 
scrolling to be based on the visual bounds of the node (for scaled content etc.), 
they need to wrap the scroll node in a Group. 
संबंधित मुद्दे