email me at borlaj@portlandschools.org

Loading
notes previous (14/<14) submit the dump links  
 

Basic drawing activity

 

Sorry I couldnt be there today. I thought it would be good practice for you to do another drawing activity (we soon are going to be doing graphical stuff). Copy the code below in to a new class in BlueJ. Compile and run it (to run it, right click method startDrawing()

You are going to write the code in paint to draw a scene. This time instead of using triangles and circles (like before), you have a huge library called graphics. Look at the api here - on this page go to methods. You will see hundreds like drawLine or fillOval).

I want you to create a drawing that looks like something (a person, a house), I want at least 3 colors and shapes. I also want you to draw some text on it. Submit it online.

  
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class Drawing extends JFrame{

    public void paint(Graphics g) {
        g.drawLine(30,30,200,200);
        g.setColor(Color.green);
        g.fillRect(0, 0, 200, 100);
        g.setColor(Color.black);
        g.drawString("Sample Drawing", 20, 20);
        g.setColor(Color.blue);
        g.drawString("created by Borland", 200, 400);
    }


    public Drawing(){
        JPanel panel=new JPanel();
        getContentPane().add(panel);
        setSize(600,600);

    }        
    
    public static void startDrawing(){
        Drawing d=new Drawing();
        d.setVisible(true);
    }
}