Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Drawing shapes

100% developed
From Wikibooks, open books for an open world
<Java Programming |Graphics

TOCJava Programming
Drawing shapes
Drawing complex shapes
NavigateUser Interface topic:()


Introduction to Graphics

[edit |edit source]

Throughout this chapter, we will refer to the process of creating Graphical content with code as either drawing or painting. However, Java officially recognizes the latter as the proper word for the process, but we will differentiate between the two later on.

Now, the main class that you would be needing would, without doubt, be theGraphics class. If you take a closer look at the method that we used in theIdentifying the acquisition of theGraphics class in our code

Computer codeCode listing 9.3: A basic canvas
importjava.awt.*;importjavax.swing.*;publicclassMyCanvasextendsCanvas{publicMyCanvas(){}publicvoidpaint(Graphicsgraphics){/* We would be using this method only for the sake         * of brevity throughout the current section. Note         * that the Graphics class has been acquired along         * with the method that we overrode. */}publicstaticvoidmain(String[]args){MyCanvascanvas=newMyCanvas();JFrameframe=newJFrame();frame.setSize(400,400);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().add(canvas);frame.setVisible(true);}}

To view the contents of theGraphics class, please check theexternal links at the bottom of the page for links to the online API.

Etching a line on the canvas

[edit |edit source]

Understanding coordinates

[edit |edit source]

To start off your drawing experience, consider drawing the most basic shape — a line. A canvas when viewed upon with regards to drawing routines can be expressed as an invertedCartesian coordinate system. A plane expressed by an x- and a y-axis. The origin point or(0,0){\displaystyle (0,0)} being the top-left corner of a canvas and the visible area of the canvas being theCartesian quadrant I or the positive-positive(+,+) quadrant. The further you go down from the top, the greater the value of y-coordinate on the y-axis, vice-versa for the x-axis as you move toward the right from the left. And unlike the values on a normal graph, the values appear to be positive. So a point at(10,20){\displaystyle (10,20)} would be 10 pixels away from the left and 20 pixels away from the top, hence the format(x,y){\displaystyle (x,y)}.

Figure 9.2: A simple line form displayed across the canvas from Code section 9.4

Drawing a simple line across the screen

[edit |edit source]

Now, we already know that a line is a connection of two discreet points atop a canvas. So, if one point is at(x1,y1){\displaystyle (x1,y1)} and the other is at(x2,y2){\displaystyle (x2,y2)}, drawing a line would require you to write a syntax like code below. For the sake of brevity, we will skim out the rest of the method unused in the example.

ExampleCode section 9.4: Drawing a simple line form
...publicclassMyCanvasextendsCanvas{...publicvoidpaint(Graphicsgraphics){graphics.setColor(Color.black);graphics.drawLine(40,30,330,380);}...}

In the above example, a simple method is used to define precisely where to place the line on the Cartesian scale of the canvas. ThedrawLine(int,int,int,int) asks you to put four arguments, appearing in order, thex1 coordinate, they1 coordinate, thex2 coordinate and they2 coordinate. Running the program will show a simple black line diagonally going across the canvas.

Figure 9.3: A simple black-outlined rectangle drawn

Drawing a simple rectangle

[edit |edit source]

We now proceed on to our second drawing. A simple rectangle would do it justice, see below for code.

ExampleCode section 9.5: Drawing a simple rectangle
...publicclassMyCanvasextendsCanvas{...publicvoidpaint(Graphicsgraphics){graphics.drawRect(10,10,100,100);}...}

In the above example, you see how easy it is to draw a simple rectangle using thedrawRect(int, int, int, int) method in theGraphics instance that we obtained. Run the program and you will see a simple black outline of a rectangle appearing where once a blank canvas was.

The four arguments that are being passed into the method are, in order of appearance, thex-coordinate, they-coordinate,width and theheight. Hence, the resultant rectangle would start painting at the point on the screen 10 pixels from the left and 10 from the top and would be a 100 pixel wide and a 100 pixel in height. To save the argument here, the above drawing is that of a square with equal sides but squares are drawn using the same method and there is no such method asdrawSquare(int, int, int)

Figure 9.4: Same rectangle drawn with a red outline

Playing around with colors

[edit |edit source]

You can change the color of the outline by telling theGraphics instance the color you desire. This can be done as follows:

ExampleCode section 9.6: Changing the outline color of the rectangle
...publicclassMyCanvasextendsCanvas{...publicvoidpaint(Graphicsgraphics){graphics.setColor(Color.red);graphics.drawRect(100,100,500,500);}...}

Running the program would render the same rectangle but with a red colored outline.

For the purposes of bringing color to our drawing, we used a method namely thesetColor(Color) method. This method comes into force for all the drawing made after its call until another color is set. It asks for an argument of typeColor. Now because you have no idea of how to actually instantiate aColor class, the class itself has a few built-in colors. Some built-in colors that you can use are mentioned below.

  • Color.red
  • Color.blue
  • Color.green
  • Color.yellow
  • Color.pink
  • Color.black
  • Color.white

Try running the program while coding changes to colors for a different colored outline each time. Play around a bit with more colors. Look for the Color class API documentation in theexternal links at the bottom of the page.

Figure 9.5: Same rectangle drawn with a red outline and a yellow fill

Filling up the area of the rectangle

[edit |edit source]

Up until now, you have been able to draw a simple rectangle for yourself while asking a question silently, "why is the outline of the rectangle being painted rather the area as a whole?" The answer is simple. Any method that starts withdrawXxxx(...) only draws the outline. To paint the area within the outline, we use thefillXxxx(...) methods. For instance, the code below would fill a rectangle with yellow color while having a red outline. Notice that the arguments remain the same.

ExampleCode section 9.7: Drawing a yellow rectangle with a red outline
...publicclassMyCanvasextendsCanvas{...publicvoidpaint(Graphicsgraphics){graphics.setColor(Color.yellow);graphics.fillRect(10,10,100,100);graphics.setColor(Color.red);graphics.drawRect(10,10,100,100);}...}


Figure 9.6: A white circle drawn with a blue outline

What about a circle?

[edit |edit source]

Drawing a circle is ever so easy? It is the same process as the syntax above only that the wordRect is changed to the wordOval. And don't ask me why oval? You simply don't have the methoddrawCircle(int, int, int) as you don't havedrawSquare(int, int, int). Following is the application of Graphics code to draw a circle just to whet your appetite.

ExampleCode section 9.8: Drawing a white circle with a blue outline
...publicclassMyCanvasextendsCanvas{...publicvoidpaint(Graphicsgraphics){graphics.setColor(newColor(0,0,255));graphics.drawOval(50,50,100,100);}...}


A new form of a rectangle

[edit |edit source]
Figure 9.7: A pink rounded rectangle with a red outline. Amazing!

Simple so far, isn't it? Of all the shapes out there, these two are the only shapes that you'd need to build for the moment. Complex graphics routines are required to build shapes like a rhombus, triangle, trapezium or a parallelogram. We would be tackling them later on in another section. However, on a last note I would leave you with another interesting shape - a combination of both ovals and rectangle. Think a rectangle with rounded corners, a Rounded Rectangle (RoundRect).

ExampleCode section 9.9: Drawing a pink rounded rectangle with a red outline
...publicclassMyCanvasextendsCanvas{...publicvoidpaint(Graphicsgraphics){graphics.setColor(Color.pink);graphics.fillRoundRect(10,10,100,100,5,5);graphics.setColor(Color.red);graphics.drawRoundRect(10,10,100,100,5,5);}...}

Notice that the syntax of thedrawRoundRect(int, int, int, int, int, int) method is a bit different than the syntax for the simple rectangle drawing routinedrawRect(int, int, int, int). The two new arguments added at the end are thewidth of the arc in pixels and theheight of the arc in pixels. The result is pretty amazing when you run the program. You don't need to squint your eyes to tell that the corners of the rectangle are slightly rounded. The more the values of the width and height of the arcs, the more roundness appears to form around the corner.

Hmm, everything's perfect, but...

[edit |edit source]

Sometimes people ask, after creating simple programs like the ones above, questions like:

  • Why did I have to tell theGraphics instance the color before each drawing routine? Why can't it remember my choice for the outlines and for the fill colors? The answer is simpler than it seems. But, to fully understand it, we need to focus on one little thing called theGraphics Context. The graphics context is the information that adheres to a single instance of theGraphics class. Such an instance remembers only one color at a time and that is why we need to make sure the context knows of the color we need to use by using thesetColor(Color) method.
  • Can I manipulate the shapes, like tilt them and crop them? Hold your horses, cowboy! Everything is possible in Java, even tilting and cropping drawings. We will be focusing on these issues in a later section.
  • Is making shapes like triangles, rhombuses and other complex ones tedious? Well, to be honest here, you need to go back to your dusty book cabinet and take out that High School Geometry book because we would be covering some geometry basics while dealing with such shapes. Why not read awikibook on Geometry?
Test your knowledge

Question 9.3: Throughout the exercise listings above, we have been filling the shapes first and then drawing their outlines. What happens if we do it the other way around? Consider the code below.

...publicvoidpaint(Graphicsgraphics){graphics.setColor(Color.red);graphics.drawRect(10,10,100,100);graphics.setColor(Color.yellow);graphics.fillRect(10,10,100,100);}...
  1. The left and the top outlines disappear.
  2. The right and the bottom outlines disappear.
  3. The color for the outline becomes the color for the fill area.
  4. All the outlines disappear.
Answer

All the outlines disappear.

Question 9.4: What woulddrawLine(10, 100, 100, 100) give you?

  1. A horizontal line.
  2. A vertical line.
  3. A diagonal line.
Answer

A horizontal line.

If you have any questions regarding the content provided here, please feel free to comment in this page'sdiscussion.

External Links

[edit |edit source]


TOCJava Programming
Drawing shapes
Drawing complex shapes
Retrieved from "https://en.wikibooks.org/w/index.php?title=Java_Programming/Graphics/Drawing_shapes&oldid=4440458"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp