HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
Deutsch   English   

2. Shapes and colors

 

 

YOU LEARN HERE...

 

The GPanel module has many graphic functions with which you can draw geometric shapes. Here are the most important commands:


point(x, y)
line(x1, y1, x2, y2)
rectangle(width, height)
fillRectangle(width, height)
rectangle(x1, y1, x2, y2)
fillRectangle(x1, y1, x2, y2)
triangle(x1, y1, x2, y2, x3, y3)
circle(r)
fillCircle(r)
ellipse(a, b)
arc(r, startwinkel, endwinkel)
fillArc(r, startwinkel, endwinkel)

point
line
rectangle (width, height)
filled rectangle
rectangle (corner points)
filled rectangle
triangle (corner points)
circle with radius r
filled circle
ellipse with the axes a, b
arc of a circle
filled arc


 

 

EXAMPLES

 

For figures that do not have coordinates, you must first define the position of the center point with pos(x, y). The invisible graphic cursor is located at point (0, 0) by default. The functions listed above do not change the position of the graphics cursor.

The current drawing color is set with the command setColor(“color”), GPanel knows the so-called X11 colors. There are several dozen color names that can be looked up on the Internet at https://www.w3schools.com/colors/colors_x11.asp nachschlagen kann (z.B. "red", "yellow", "blue", "green", "magenta", usw.).

Die Farbe kann auch mit dem Befehl setColor(r, g, b) gesetzt werden, wobei r, g, b ganze Zahlen zwischen 0 und 255 sind (RGB-Farbkomponenten). Eine hellgrüne Farbe erhält man z. B. mit setColor(0, 255, 0).

Example 1: Circles and rectangles in different colors

Program:      

# Gp2a.py
from gpanel import *

makeGPanel(-10, 10, -10, 10)
circle(10)
setColor("darkblue")
fillRectangle(19, 6)
setColor("yellow")
fillRectangle(14,14)
setColor("red")
fillCircle(4)
setColor("green")
pos(5, 5)
fillCircle(2)
► Copy to clipboard
 


Example 2
: Shapes defined by the coordinates of the corner points
You can also draw thicker lines. lineWidth(3): sets the line thickness to 3 pixels.

Program:     

# Gp2b.py
from gpanel import *

makeGPanel(0, 10, 0, 10)

setColor("yellow")
fillRectangle(2, 2, 8, 5)
setColor("red")
fillTriangle(2, 5, 8, 5, 5, 8)
setColor("darkblue")
lineWidth(3)
rectangle(3, 3, 4, 4) 
rectangle(6, 3, 7, 4) 
► Copy to clipboard
 

 

Example 3: Filling a closed figure
A figure can be subsequently filled. To do this, use the function fill(x, y, oldcolor, color). x, y are coordinates of a point located in the figure to be colored, oldcolor is the current fill color and color is the new fill color. fill(0, 0, "white", "magenta": fills the white-filled triangle, in which the point (0, 0) is located, with a pink color.

Program:      

# Gp2c.py
from gpanel import *

makeGPanel(-10, 10, -10, 10)

line(-6, -3, 4, 6)
line(-1, 7, 6, -5)
line(-8, 1, 7, -4)
fill(0, 0, "white", "magenta")
► Copy to clipboard
 

 

 

REMEMBER YOU...

 

You can look up the correct syntax of the graphic commands in the Documentation at any time. You can also find a link to the documentation in the WebTigerJython editor under Menu.

The invisible graphics cursor is located in the point (0, 0) by default. With the command pos(x, y) you can move it to any position in the graphics window.

 

 

TO SOLVE BY YOURSELF

 

1)


Choose a suitable coordinate system and draw a traffic light according to the adjacent template.

 

 

2)

Draw a Swiss cross.
 

 

3)

Draw a rainbow. Use the command fillArc(r, startangle, endangle).