JavaFX A little Learning

Source: Internet
Author: User
Tags custom graphics

The Canvas component in Java represents a blank rectangular area on the screen where an application can draw, or you can capture user input events from that area. The application must create subclasses for the Canvas class to gain useful functionality, such as creating custom components. You must override the Paint method to perform custom graphics on the canvas.

Java uses MouseEvent to listen to the mouse. Mouseclicked called when the mouse button is clicked (pressed and released) on the component. Mousedragged called when the mouse button is dragged over the component. The Getbutton () can be used to determine which key is pressed.

LinearGradient is one of two gradient methods in Java and the other is radialgradient. To create a linear gradient, specify STARTX,STARTY,ENDX and Endy for the start point and end point. The start and end coordinates specify where the gradient mode starts and stops.

This small program Canvastest reference part of the code, the main implementation is left-click drag coloring, right-click Erase. Because the gradient does not understand the API, the temporary use of no_cycle, that is, the form of non-cyclic gradients.

1 Importjavafx.application.*;2 Importjavafx.event.*;3 ImportJavafx.scene.Group;4 ImportJavafx.scene.Scene;5 Importjavafx.scene.canvas.*;6 Importjavafx.scene.input.*;7 Importjavafx.scene.paint.*;8 ImportJavafx.scene.shape.*;9 ImportJavafx.stage.Stage;Ten  One  Public classCanvastestextendsApplication { A     Private voidReset (canvas canvas, color color) { -Graphicscontext GC =canvas.getgraphicscontext2d (); - Gc.setfill (color); theGc.fillrect (0, 0, Canvas.getwidth (), Canvas.getheight ()); -     } -  - @Override +      Public voidStart (Stage primarystage) { -Primarystage.settitle ("Drag the mouse to paint and right-click to clear"); +Group root =NewGroup (); A  atRectangle rect =NewRectangle (800, 800); - Background (rect); - Root.getchildren (). Add (rect); -  -         FinalCanvas Canvas =NewCanvas (400, 400); -Canvas.settranslatex (200); inCanvas.settranslatey (200); -Reset (Canvas, Color.web ("#003153"))); to  +         FinalGraphicscontext GC =canvas.getgraphicscontext2d (); -  theCanvas.addeventhandler (mouseevent.mouse_dragged,NewEventhandler<mouseevent>() { * @Override $              Public voidhandle (MouseEvent e) {Panax NotoginsengGc.clearrect (E.getx ()-2, E.gety ()-2, 7, 7); -             } the         }); +  ACanvas.addeventhandler (mouseevent.mouse_clicked,NewEventhandler<mouseevent>() { the @Override +              Public voidhandle (MouseEvent t) { -                 if(T.getbutton () = =mousebutton.secondary) { $Reset (Canvas, Color.web ("#003153"))); $                 } -             } -         }); the  - Root.getchildren (). Add (canvas);WuyiPrimarystage.setscene (NewScene (Root, 800, 800)); the primarystage.show (); -     } Wu  -     Private voidBackground (Rectangle rect) { AboutRect.setfill (NewLinearGradient (0, 0, 1, 1,true, $ Cyclemethod.no_cycle, -                 NewStop (0, Color.web ("#7F0020")), -                 NewStop (1, Color.web ("#CC7722")))); -     } A}
View Code

JavaFX A little Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.