JavaFX Canvas : "fuzzy" lines compared to Swing

Hi,
I would very much like to port my Swing application (that draws on a JComponent) to JavaFX. But to my dismay the JavaFX Canvas does not draw as sharp and thin lines as Swing does.
Lines drawn with the same thickness is noticeably thicker (and "fuzzier") on the JavaFX Canvas than the equivalent Swing lines. I have tested both on Windows (7) and Linux, both 64-bit.
In my JavaFX app, I proceed like this: gc.beginPath() --> gc.moveTo(..) --> gc.lineTo() --> gc.stroke().
Is there a setting or something I am missing?

You need to take into account the coordinate system.
For further explanation of why this works see the answer to the question below and Node's javadoc:
http://stackoverflow.com/questions/11881834/what-are-a-lines-exact-dimensions-in-javafx-2
http://docs.oracle.com/javafx/2/api/javafx/scene/Node.html
At the device pixel level, integer coordinates map onto the corners and cracks between the pixels and the centers of the pixels appear at the midpoints between integer pixel locations. Because all coordinate values are specified with floating point numbers, coordinates can precisely point to these corners (when the floating point values have exact integer values) or to any location on the pixel. For example, a coordinate of (0.5, 0.5) would point to the center of the upper left pixel on the Stage. Similarly, a rectangle at (0, 0) with dimensions of 10 by 10 would span from the upper left corner of the upper left pixel on the Stage to the lower right corner of the 10th pixel on the 10th scanline. The pixel center of the last pixel inside that rectangle would be at the coordinates (9.5, 9.5).
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.canvas.*;
import javafx.stage.Stage;
/** JavaFX Canvas : "fuzzy" lines compared to Swing JavaFX Canvas : "fuzzy" lines compared to Swing */
public class FuzzyCanvas extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(Stage stage) {
    Canvas canvas = new Canvas(50, 50);
    GraphicsContext gc = canvas.getGraphicsContext2D();
    gc.beginPath();    // fuzzy line.
    gc.moveTo(10, 30);
    gc.lineTo(20, 30);
    gc.stroke();
    gc.beginPath();    // clean line.
    gc.moveTo(10.5, 39.5);
    gc.lineTo(19.5, 39.5);
    gc.stroke();
    stage.setScene(new Scene(new Group(canvas)));
    stage.show();
}

Similar Messages

  • JavaFX compared to Swing

    Hi All
    I am trying to work with new JavaFX, I got an experience with Swing, with my first impression it seems that controls like Dialog, messageBox, internalFrame, Frame, Dialog, are not there, maybe because this is the way to make the new RIA looks like web sites, but what are the controls instead of these Swing controls?
    Shlomo.

    There are no specific replacements if you are looking for things like what JOptionPane provided.. you have to build your own dialog with a Stage.

  • Ipod to tv white/black fuzzy lines

    please help and advise if im doing something wrong.i have the ipod setting on tv set to on,set at pal(have tried ntsc still same)when i plug ipod into tv to watch video/photos all i get is black/white fuzzy lines across screen,thought at first cable faulty but have now tried a second cable still the same issue.what am i doing wrong has anyone had same problem and sorted it.please advise...thanks

    there will always be ways to get around purchasing something from apple for those who are so inclined. the link i included above shows the difference in wiring, and sure, you can swap / rewire / cut / change your existing cables if you like. it shows you how to wire to an ibook av cable which is the same as a standard DV from what i understand. it's your ipod, and you can plug whatever you like in it.
    me, i personally like to use things the way they are intended, and unless there is a third party, informally endorsed version that does it better, i like to kee it in the family. the ipod is (as the rest of the apple family) an expression of style and a design statement. it's for the same reason i wouldn't want to have a black cord connecting my ears to my music - the white cord(s) are part of the (admittedly grandiose) ipod brand and culture. i don't only use mine in my living room, but for presentations in boardrooms too, and the entire package from my pocket is more impressive than an old lumpy laptop...
    it's personal preference. but IMHO, if you can spring for the apple AV's, i would do it...

  • Fuzzy lines

    Does anyone know or can suggest what settings to use in videora, when converting to ipod use, for videos, so i do not have the fuzzy lines? im not too sure what happened to the volume either, if anyone has encountered this problem and overcome it and can offer help, would very much appreciate it,
    many thanks in advance

    Thanks Colin
    I am using vectors.
    I have been experimenting and reading and it seems that the iPhone just doesn't like vectors especially on a timeline.
    Export from flash to a png and then reimport and its lovely; rather frustating.
    I experiment on ...

  • How to make the diagonal line in the swing table cell?

    Hi all,
    How to make the diagonal line in the swing table cell just like the link below?
    http://61.132.17.188/webber/table.gif

    One improvement.. To get a line that is neat, use a bit of Graphics2D
        public void paintComponent(java.awt.Graphics g)
            int w = getSize().width;
            int h = getSize().height;
            // draw a line between (0,0) and (w,h)  
            g.setColor(getForeground());
            if (strTop == null)
                strTop = "  ";
            if (strBottom == null)
                strBottom = "  ";
                java.awt.Graphics2D g2 = (java.awt.Graphics2D)g;
              g2.setRenderingHint(java.awt.RenderingHints.KEY_ANTIALIASING,java.awt.RenderingHints.VALUE_ANTIALIAS_ON);
              //g2.setStroke(new java.awt.BasicStroke(14.0f));
            g.drawLine(0, 0, w, h);
            //the following lines will draw the two strings,
            //one above the top of the line and the above below it.     
            g2.drawString(strTop, (w/2)+2, (h/2)-2);
            g2.drawString(strBottom, 2, (h / 2) + 2);
            //the x,y for drawString are only based on assumption.   //do necessary changes to suit ur needs.
        }Then create a TableCellRenderer and set it to the Table Header for ur table's 1st row.....
    Cheers

  • How to remove the Blue Fuzzy Line in Spry Collapsible Panels

    Do anyone know exactly how to remove the blue fuzzy line that appears when a tab is focussed on in the Collapsible Panels feature?
    I heard there was some code that could be implemented into one of the style sheets which could solve this but I am yet to find a solution
    Thanks

    .CollapsiblePanelTab { outline:none; }
    Try that.

  • Fuzzy line on the middle of display.

    Recently i have found a fuzzy line in the middle of my retina macbook pro screen (15"). I have always treated it very well, it never falled down.
    The line appear only with a strong light from sun, especially with dark backgounds on the screen.
    The line seams like dust, it's not like a crack. It has a lenght of 9cm long and an height of 1mm.
    I understand that the problem is probably related to how i used the mac, but i don't undertand the cause. The only thing have done is replacing the Spek cover, but two or three weeks before now. What do you think? Have I to replace the screen? How much does it cost?
    Hope you can help me.
    Olivum
    Sorry for my bad english

    Warranty is worldwide. He can take it to a Dutch Apple Store without worrying
    Best of luck.

  • W520 extended out to LCD got fuzzy lines?

    have been using these 2 machines for years already, i linked them up with a Display Port -> DVI cable, somehow now the Philips LCD got fuzzy line like this :
    https://www.dropbox.com/s/yqa2lyt9llf1yj6/20150128​_101415.mp4?dl=0
    Tried with a VGA cable and used Nvidia software to extend screens, but problem still exists. 
    My machines:
    W520 + Philips 240PW.

    Does the Philips LCD work using another display source?
    W520: i7-2720QM, Q2000M at 1080/688/1376, 21GB RAM, 500GB + 750GB HDD, FHD screen
    X61T: L7500, 3GB RAM, 500GB HDD, XGA screen, Ultrabase
    Y3P: 5Y70, 8GB RAM, 256GB SSD, QHD+ screen

  • Iphine 5 - fuzzy lines when downloading an app

    I brought an Iphone 5 yesterday and i think its really good apart from one little problem. Every time I insert my password to download an App fuzzy lines appear (as if you have dropped a phone, which I havet.) Has anyone else have this problem? Its a little issue but when you have spent over £500 on the phone its very annoying

    I phoned apple and they told me to restart to factory settings  and set it up as a new phone then restart it again but do it from youtr back up. So  far it worked

  • Freezing, fuzzy lines

    Whilst using my ibook g4 today I clicked on explorer and all of a sudden a load of little lines started to flick on the screen and the screen froze. I have re started my computer a number of times since and heres what happens
    1.The grey screen comes up with apple logo and spinny thing then freezes
    2.Goes through to a blue screen and freezes
    3.Gets to the desktop display and then freezes with fuzzy lines
    I have tried the fsck -y(think thats the right thing), and it says everything is fine. I have inserted my osx restore disk only to have that freeze aswell with the fuzzy lines.
    I have managed to firewire all the documents onto my G5 so i dont mind wiping anything.
    Any suggestions would be much appreciated
    Trev

    Hello Trev
    Given that your machine works in target mode (I assume this is how you got your data off) you may be able to run disk utility from your G5 and see if that shows up anything amiss.
    If not then it is either get hold of a copy of diskwarrior which can help or go for a clean install. Make sure you go for a secure disk formatting option (Zero all data) as this will map out any bad sectors on your drive.
    You havn't by any chance got Norton on there have you? Had a couple of freinds who's machines have gone west with similar symptoms as a result.
    S.

  • Fuzzy lines in youtube from compressing it on Imovie 09

    I have tried to get those fuzzy lines out of youtube, when I do the compression for sharing in high quality, how can I get rid of it.
    http://www.youtube.com/watch?v=NBSlL-RF7ao

    Hi,
    Try exporting your HD footage using one of the exports I've marked in green, I believe there is a bug when exporting HD interlaced footage using the exports I've marked in red that will result in the type of problem you are seeing.
    If this does not work you may need to export as Apple Intermediate Codec and run it through JES Deinterlacer.
    Uploaded with plasq's Skitch!

  • How to fill canvas with lines

    <mx:Canvas id="b1" x="10" y="10" height="40" width="300" borderStyle="solid" borderColor="black"/>
    when i want to draw lines with 15 pixels gap to fill the entire canvas i wrote as follows
                   for(var i:int=b1.x+15;i<b1.x+b1.width;i=i+15)
                        var line1:UIComponent = new UIComponent();
                        var lineThickness1:Number = 1;
                        var lineColor1:Number = 0x000000;
                        var lineAlpha1:Number = 1;
                        line1.graphics.lineStyle(lineThickness1,lineColor1,lineAlpha1);
                        line1.graphics.moveTo(i,b1.y);
                        line1.graphics.lineTo(i,b1.y+b1.height);
                        this.addChild(line1);
    it's working fine
    lly,
    <mx:Canvas id="b4" x="600" y="200" height="60" width="300" borderStyle="solid" borderColor="black" rotation="40"/>
    I have the above canvas with id 'b4' only difference is that this canvas has the rotation
    how to fill this canvas with lines just i did above?

    Hope this code will help you,
    for(var i: int = 15; i < b4.width; i = i + 15) {
         var line1: UIComponent = new UIComponent();
         var lineThickness1: Number = 1;
         var lineColor1: Number = 0x000000;
         var lineAlpha1: Number = 1;
         line1.graphics.lineStyle(lineThickness1, lineColor1, lineAlpha1);
         line1.graphics.moveTo(i, 0);
         line1.graphics.lineTo(i, b4.height - 1);
         //Add line in canvas instead of main container
         b4.addChild(line1);
    <mx:Canvas id="b4" x="600" y="200" height="60" width="300" borderStyle="solid" borderColor="black" rotation="40"/>

  • My mac screen comes out with white fuzzy lines going across the screen, it used to just go back to normal and out of no where it freezes and the white fuzzy lines go across the screen. Now it is frozen at the opening screen. what do i do?

    My mac screen is frozen with white fuzzy lines going across the screen. I shut it off and turn it back on but it still remains white fish lines on the start up screen. I've tried revolting it, it works sometimes but now its stuck at the starting screen. I have been at this for 2 hours to try and fix it but nothing has changed about the screen. What do I do, if I break this computer I'm screwed.

    I would post a picture of the screen but I do not know how to do that

  • Embedding JavaFX v1.2 Scene's in Swing

    Before I start... SUN DOES NOT SUPPORT THIS AND IT IS LIKELY TO CHANGE... hopefully we get official support in a future release, please :)
    +"Note: We also recognize the need for the inverse (embedding a JavaFX scene into a Swing app), however that is not supported with 1.2 as it requires a more formal mechanism for manipulating JavaFX objects from Java code. The structure of these JavaFX objects is a moving target (for some very good reasons) and we're not ready to lock that down (yet). However,as with most software, this can be hacked (see Rich & Jasper's hack on Josh's blog) but know that you are laying compatibility down on the tracks if you go there. "+ Source: http://weblogs.java.net/blog/aim/archive/2009/06/insiders_guide.html
    But if you really really want this now read on.
    After a lengthy investigation and some help from others (including the jfxtra's project) here is how to Embed a JavaFX scene in your Swing application:
    First, start your java application with javafx and not java! This will fix any potential class path issues and really does solve a lot of issues you will have 'patching' the JVM with JavaFX (if that is even possible). JavaFX will happily run a Java project (with the JVM) even if there is not one JavaFX class in your project.
    Next, you need to create a Java Interface for your JavaFX object to implement:
    package mix.javastuff;
    * This is a Java Interface that your JavaFX class should implement.
    * This will define how Java will interact with your JavaFX class.
    * @author ahhughes
    public interface TextDisplay {
        public void setText(String text);
    }Next, you need to create your JavaFX object... it must extend Scene and you are to provide any interaction with Java you will need to implement a java interface:
    package mix.javafxstuff;
    import mix.javastuff.TextDisplay;
    import javafx.scene.Scene;
    import javafx.scene.text.Text;
    import javafx.scene.text.Font;
    import javafx.scene.effect.Reflection;
    import javafx.scene.text.TextOrigin;
    * Here is a very simple scene, that implements a java interface
    * That allows Java (or JavaFX) to setText on the Scene's content.
    public class TextDisplayJavaFXScene extends Scene , TextDisplay {
        override public function setText(text:String):Void{
            content = Text {
                    font : Font {size: 44}
                    x: 0, y: 0
                    textOrigin: TextOrigin.TOP
                    content: text
                    effect: Reflection {}
    }Finally, you need to use some reflection and some under the hood magic to get your JavaFX object and wrap in in a SwingScene (which just so happens to extend JComponent).
    package mix.javastuff;
    import com.sun.javafx.tk.swing.SwingScene;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javafx.reflect.FXClassType;
    import javafx.reflect.FXContext;
    import javafx.reflect.FXFunctionMember;
    import javafx.reflect.FXLocal;
    import javafx.reflect.FXLocal.ObjectValue;
    import javafx.scene.Scene;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    * Example of how to inject JavaFX into your Swing app.
    public class Main {
        private static final FXLocal.Context context = FXLocal.getContext();
        private static TextDisplay textDisplayJavaFXScene;// javafx scene & java interface
        public static void main(String[] args) {
            //Get the JavaFX Object, that has 'extends Scene, SomeJavaInterface'
         FXClassType instanceType = context.findClass("mix.javafxstuff.TextDisplayJavaFXScene");
         ObjectValue objectInstance = (ObjectValue) instanceType.newInstance();
         textDisplayJavaFXScene = (TextDisplay) objectInstance.asObject();
            //Wrap this in a SwingScene using refection
            JComponent textDisplayJavaFXSwingScene = sceneInstanceToJComponent((Scene)textDisplayJavaFXScene);
            //Now let's show the JComponent+JavaFXScene in a simple Swing App.
            createSimpleSwingApp(textDisplayJavaFXSwingScene);
         * This method wraps the Scene with a JComponent. This is what you can add
         * to your Swing application.
         * @param scene the JavaFX object that extends javafx.scene.Scene.
         * @return the javafx.scene.Scene wrapped by a JComponent.
         public static JComponent sceneInstanceToJComponent(Scene scene) {
             FXClassType sceneType = FXContext.getInstance().findClass("javafx.scene.Scene");
             ObjectValue obj = FXLocal.getContext().mirrorOf(scene);
             FXFunctionMember getPeer = sceneType.getFunction("impl_getPeer");
             FXLocal.ObjectValue peer = (ObjectValue) getPeer.invoke(obj);
             SwingScene swingScene = (SwingScene)peer.asObject();
             return (JComponent) swingScene.scenePanel;
         * OK, this method is just to show you this in action.... the real stuff
         * you are interested is in the methods above.
         * @param javaFXTextDisplaySwingScene
        private static void createSimpleSwingApp(JComponent javaFXTextDisplaySwingScene){
             JFrame frame = new JFrame();
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setSize(500,500);
             frame.setAlwaysOnTop(true);
              JPanel panel = new JPanel();
              panel.setLayout(new FlowLayout());
            //create a swing interface to interact with textDisplayJavaFXScene.setText()
            final JTextField textInputField = new JTextField("Set THIS Text!");
              panel.add(textInputField);
              JButton button = new JButton("Send to JavaFX!");
              panel.add(button);
              button.addActionListener(new ActionListener() {
                   @Override
                   public void actionPerformed(ActionEvent e) {
                        textDisplayJavaFXScene.setText(textInputField.getText());
            //add the JavaFX SwingScene to the Swing's JPanel
             panel.add(javaFXTextDisplaySwingScene);
            //display the swing app
             frame.add(panel);
             frame.setVisible(true);
    }That's it!

    morningstar wrote:
    You can refer to this article for how to embeded javaFX scene into SWING:
    [JavaFX Scene embedded in SWING Window|http://www.javafxgame.com/javafx-scene-swing-java/]
    [http://www.javafxgame.com/javafx-scene-swing-java/|http://www.javafxgame.com/javafx-scene-swing-java/]
    Hi morningstar, this article is good and it too helped me out A LOT! It shows how to embed a JavaFX scene in swing but doesn't show you how to interact with JavaFX objects FROM your Java/Swing code. Wrapping the JavaFX node in a SwingScene doesn't offer a way for the Java/Swing code to interact with the underlying JavaFX objects.
    How can you set the text in the JavaFX Text node from the Java/Swing code in the linked article? You can't, all you can do is have the JavaFX objects reference BACK to public static fields in your Java code (not the best):
    Java Class:
    public class JavaFXToSwingTest extends JFrame {
        public static JTextField tf = new JTextField("JavaFX for SWING");
        //other code
    JavaFX Class:
        text = JavaFXToSwingTest.tf.getText();  On the other hand, the code in the first post exposes the JavaFX objects through java interfaces as well as wrapping them in a SwingScene. This means that you can do YourJavaFXObject.setText("Blah"); from inside your Java/Swing code, rather handy! :)
    Edited by: AndrewHughes on Jul 27, 2009 12:25 AM

  • How do I convert an HTML5 Canvas arc() to JavaFX Canvas arc()?

    I wish to convert a statement from JavaScript used in an HTML5 Canvas such as:
    ctx.arc(x, y, (rad+5)*factor, 0, Math.PI*2, true);
    to the equivalent statement in JavaFX.
    What would it look like?
    For reference, in HTML5 Canvas the arc() method is defined as:
    x
    The x-coordinate of the center of the circle
    Play it »
    y
    The y-coordinate of the center of the circle
    Play it »
    r
    The radius of the circle
    Play it »
    sAngle
    The starting angle, in radians (0 is at the 3 o'clock position of the arc's circle)
    Play it »
    eAngle
    The ending angle, in radians
    Play it »
    counterclockwise
    Optional. Specifies whether the drawing should be counterclockwise or clockwise. False=clockwise, true=counter-clockwise
    but in JavaFX it is defined as:
    public void arc(double centerX, double centerY, double radiusX, double radiusY, double startAngle, double length)
    Adds path elements to the current path to make an arc that uses Euclidean degrees. This Euclidean orientation sweeps from East to North, then West, then South, then back to East.
    Parameters:
    centerX - the center x position of the arc.
    centerY - the center y position of the arc.
    radiusX - the x radius of the arc.
    radiusY - the y radius of the arc.
    startAngle - the starting angle of the arc in the range 0-360.0
    length - the length of the baseline of the arc.
    Could someone please show me what the JavaFX arc() statement would look like and explain how to convert between these two?
    Thanks.

    No conversion possible. Are you on a responsive project perhaps? Then
    output is only HTML, mobile devices do not support Flash.

Maybe you are looking for

  • Simplest way to password protect an external drive??

    I want to clone my Macbook onto an external drive - I don't want to have to turn on filevault - never used it - i would ideally like to password protect the "Macintosh HD" icon on the external drive.... is there a really simple way to do this?

  • Value mapping in UDF?

    Hi Guys, Is it possible to do the value mapping in a UDF. ReceiverService at runtime - X --> Value mapping not maintained ReceiverService - Y ---> Value mapping maintained.  I want to use this receievr service. There are more than 300 VM groups and i

  • E-Brochure/E-Commerce

    I am developing a website to sell yachts and I would like to give my customers the ability to customise the spec of the yacht they would like to purchase and get a total cost showing all the options they have selected. Can anybody tell me how to do t

  • Unable to open files or the program with having to switch users.... Why?

    Updated to 10.5.6 now when i try to open any filemaker files or the filemaker program it starts to open and closes in a split second. If i log out and open as new user it will open. I have deleted and reinstalled filemaker and this did not solve the

  • Workflow calling Web Services

    Hopefully a quick question. I have a web service running on the apps server. If I want to call it can I do this directly from Workflow (2.6.x)? Or would I have to have BPEL call the web service and call BPEL from Workflow? Many thanks, Jon