Presentation is loading. Please wait.

Presentation is loading. Please wait.

Drawing in Java.

Similar presentations


Presentation on theme: "Drawing in Java."— Presentation transcript:

1 Drawing in Java

2 Create a new JFrame JFrame frame = new JFrame("My Drawing");
public static void main(String[] args) { JFrame frame = new JFrame("My Drawing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new MySaver()); //class name here! frame.setSize(600, 600); frame.setLocationRelativeTo(null); frame.setVisible(true); }

3 Extend & Implement public class MySaver extends JPanel implements ActionListener { //rest of class/main go here

4 public void actionPerformed(ActionEvent e) { // the events to occur }

5 Try your program out Should have a basic frame drawn to the dimensions you chose Now let’s add some drawing

6 public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; //set a random size Random randy = new Random(); int mySize = randy.nextInt(100); g2.fillOval(0,0, mySize, mySize); g2.fillRect(50, 100, 25,mySize ); }

7 Try your program out Should have a random oval and rectangle drawn Now let’s make it redraw different random shapes

8 Create a timer Timer timer; //create a timer
public MySaver() { //constructor – same name as class timer = new Timer(500, this); //create a timer timer.setInitialDelay(1000); // just to show a pause at start timer.start(); //start the timer }

9 public void actionPerformed(ActionEvent e) { // add in repaint repaint(); }

10 public void paint(Graphics g) { //add in super to erase the screen super.paint(g); Graphics2D g2 = (Graphics2D) g; //set a random size Random randy = new Random(); int mySize = randy.nextInt(100); g2.fillOval(0,0, mySize, mySize); g2.fillRect(50, 100, 25,mySize ); }

11 Try your program out Should have a random oval and rectangle drawn at intervals Next --- Explore the different things you can draw with strokes; opacity; fill etc. Create a screen saver


Download ppt "Drawing in Java."

Similar presentations


Ads by Google