Computer Science 15-100 (APEA Section E), Summer 2008
Class Notes:  Graphics Redux


Logistics

  1. Schedule
    1. Quizzes
      1. Quiz 1 returned today (max=97, median=70)  [covered mainly Ch 1]
      2. Quiz 2 tomorrow  [covers mainly Ch 2]
      3. Quiz 3 Friday [covers mainly Ch 3]
    2. Hws
      1. Hw2 due today
      2. Hw3 due Thursday
         
  2. Reading:  [no new reading for today]

Topic Outline:

  • Data and Expressions (redux)
     
    1. Increment and Decrement Operators

      b)  As Expressions

      class MyCode {
        public static void main(String[] args) {
          int x, y;
          x = 5;
          y = x++;
          System.out.println(x + "," + y); // ?,?
          x = 5;
          y = ++x;
          System.out.println(x + "," + y); // ?,?
          x = 5;
          y = 10 + x--;
          System.out.println(x + "," + y); // ?,?
          x = 5;
          y = 10 + --x;
          System.out.println(x + "," + y); // ?,?
        }
      }
    2. More on the Scanner

      a)  From p. 87:  Just need to know the methods next***()
      next(), nextLine(), nextBoolean(), nextByte(), etc...

      b)  next() versus nextLine()

      Does not work quite as you might expect:
        * next returns next input token as a String
        * nextLine returns the rest of the current line as a String
        * they do not mix-and-match well!

      class MyCode {
        public static java.util.Scanner scanner = new java.util.Scanner(System.in);
        public static void main(String[] args) {
           String s1, s2;
           System.out.print("Enter a few words: ");
           s1 = scanner.next();
           System.out.print("Enter a few other words: ");
           s2 = scanner.nextLine();
           System.out.println("s1 = '" + s1 + "'");
           System.out.println("s2 = '" + s2 + "'");
        }
      }

      Do this again, but just use next, not nextLine:

      class MyCode {
        public static java.util.Scanner scanner = new java.util.Scanner(System.in);
        public static void main(String[] args) {
           String s1, s2, s3;
           System.out.print("Enter a few words: ");
           s1 = scanner.next();
           s2 = scanner.next();
           s3 = scanner.next();
           System.out.println("s1 = '" + s1 + "'");
           System.out.println("s2 = '" + s2 + "'");
           System.out.println("s3 = '" + s3 + "'");
        }
      }

      Once more, but just use nextLine, not next:

      class MyCode {
        public static java.util.Scanner scanner = new java.util.Scanner(System.in);
        public static void main(String[] args) {
           String s1, s2;
           System.out.print("Enter a few words: ");
           s1 = scanner.nextLine();
           System.out.print("Enter a few other words: ");
           s2 = scanner.nextLine();
           System.out.println("s1 = '" + s1 + "'");
           System.out.println("s2 = '" + s2 + "'");
        }
      }


    Graphics (redux)
     

    1. More Graphics:  BasicGraphicsDemo1.java

      // BasicGraphicsDemo1.java

      // A demo of some simple graphics operations, starting from BasicGraphics.java

      import java.awt.*;
      import java.awt.event.*;
      import java.util.*;
      import javax.swing.*;

      public class BasicGraphicsDemo1 extends JComponent {
        
         public static void main(String[] args) {
            JComponent myGraphics = new BasicGraphicsDemo1(); // Your class name goes here!
            launch(myGraphics, 500, 300); // Set the initial dimensions here!
         }
        
         public void paint(Graphics page) {
            int width = getWidth();
            int height = getHeight();

            // filled blue rectangle as large as the window     
            page.setColor(Color.blue);
            page.fillRect(0,0,width,height);
           
            // filled yellow oval inscribed in the blue rectangle
            page.setColor(Color.yellow);
            page.fillOval(0,0,width,height);
           
            // unfilled (drawn) black rectangle on the left
            page.setColor(Color.black);
            page.drawRect(10,10,width/2-20,height-20);
           
            // unfilled thick-lined black ovals on the right
            ((Graphics2D)page).setStroke(new BasicStroke(5)); // 5 pixels wide
            page.drawOval(width/2+10,10,width/2-20,height/2-20);
            page.drawOval(width/2+10,height/2+10,width/2-20,height/2-20);

            // unfilled thin-lined red triangle on the right
            ((Graphics2D)page).setStroke(new BasicStroke(1)); // set back to 1 pixels wide
            page.setColor(Color.red);
            Polygon p1 = new Polygon();
            p1.addPoint(3*width/4,height/3);  // addPoint(x,y)
            p1.addPoint(width/2+40,2*height/3);
            p1.addPoint(width-40,2*height/3);
            page.drawPolygon(p1);
           
            // filled green pentagon in the middle
            page.setColor(Color.green);
            int pWidth = width/5;
            int pHeight = height/10;
            int pLeft = width/2  - pWidth/2;
            int pTop  = height/2 - pHeight/2;
            Polygon p2 = new Polygon();
            p2.addPoint(pLeft           , pTop);
            p2.addPoint(pLeft+pWidth/3  , pTop+pHeight);
            p2.addPoint(pLeft+2*pWidth/3, pTop+pHeight);
            p2.addPoint(pLeft+pWidth    , pTop);
            page.fillPolygon(p2);
           
            // custom-colored red rectangles in the middle
            page.setColor(new Color(100, 0, 0));  // RGB (Red, Green, Blue)
            page.fillRect(width/2-10, 10, 30, 30);
            page.setColor(new Color(200, 25, 25));
            page.fillRect(width/2-10, 50, 30, 30);
            page.setColor(new Color(255, 50, 50));
            page.fillRect(width/2-10, 90, 30, 30);
           
            // using trig to place a small blue circle at 70 degrees
            // in the first quadrant of an orange circle
            int radius = 50;
            page.setColor(Color.orange);
            // oval will be centered at (100,100)
            page.fillOval(100-radius,100-radius,2*radius,2*radius);
            // here we use 70 degrees
            int blueCenterX = 100 + (int)(radius*Math.cos(Math.PI*70/180));
            int blueCenterY = 100 - (int)(radius*Math.sin(Math.PI*70/180));
            int blueRadius = radius/10;
            page.setColor(Color.blue);
            page.fillOval(blueCenterX-blueRadius,blueCenterY-blueRadius,
                          2*blueRadius,2*blueRadius);
                         
            // draw a line connecting the centers of the orange and blue circles
            page.drawLine(100,100,blueCenterX,blueCenterY);
         }

         //////////////////////////////////////////////////////////////////////////////
         /////////////////           END OF YOUR CODE             /////////////////////
         /////////////////  (you may ignore all the code below!!! /////////////////////
         //////////////////////////////////////////////////////////////////////////////

         public static void launch(JComponent jc, int width, int height) {
            JFrame frame = new JFrame(jc.getClass().getName());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel cp = new JPanel();
            cp.setLayout(new BorderLayout());
            cp.add(jc);
            frame.setContentPane(cp);
            frame.setSize(new Dimension(width,height));
            frame.setVisible(true);
         }
      }
       

    carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem