Quickstart Examples

Some examples to quickly begin creating amazing things with javaDraw!

Example #1: Drawing

import javadraw.*;

public class Drawing extends Window {
    
    public static void main(String[] args) {
        Window.open();
    }
    
    public void start() {
        screen.color(Color.GRAY);
        Oval face = new Oval(screen, 100, 0, 600, 600, Color.YELLOW);
        
        Oval eye1 = new Oval(screen, 200, 200, 85.71, 100);
        Oval eye2 = new Oval(screen, 500, 200, 85.71, 100);
        
        Rectangle eyebrow1 = new Rectangle(screen, 157.14, 150, 100, 5);
        eyebrow1.rotation(-16);
        
        Rectangle eyebrow2 = new Rectangle(screen, 517.85, 150, 100, 5);
        eyebrow2.rotation(16);
        
        Oval mouth = new Oval(screen, 350, 400, 85.71, face.height() / 5.5);
    }

}
Example #1 Output

Note that we calculated those numbers with these formulas:

Example #2: Animation

We can animate our face now by adding an animation loop and having our eyes blink every 3 seconds (note that we've now switched to the proportions-based calculations):

Example #2 Output

Notice how we added these lines within our start method:

Example #3: Input

Now we will move our emoji's mouth based on the mouse's position:

Example #3 Output

Notice how a new method was added, allowing us to handle input events:

Last updated