You can create your own colors by specifying the red, green, and blue ( RGB) components.įor example: Color purple = new Color(128, 0, 128) Įach value is an integer in the range 0 (darkest) to 255 (lightest). LightGray magenta orange pink white yellow Other colors include: black blue cyan darkGray gray green The setColor method determines the color of everything that gets drawn afterward.Ĭolor.red is a constant provided by the Color class to use it you have to import . To choose the color of a shape, invoke setColor on the Graphics object: g.setColor(Color.red) Graphical coordinates are measured in pixels each pixel corresponds to a dot on the screen. That way, x and y are always positive integers.įigure B.1 shows these coordinate systems. In contrast, Java uses a coordinate system where the origin is in the upper-left corner. You are probably used to Cartesian coordinates, where x and y values can be positive or negative. If you run this code, you should see a black circle on a gray background. The application doesn’t end after the main method returns instead, it waits for the JFrame to close. Once the frame is visible, the paint method is called whenever the canvas needs to be drawn for example, when the window is moved or resized. Pack the frame (resize it) to fit the canvas, and display it on the screen.Create a Drawing object (which is the canvas), set its width and height, and add it to the frame.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |