GUI: Car Flag Bulls Eye

Exercises:
1. Write a GUI application, YI_MyFlag.java and its JFrame class to draw your favorite flag.
Screen Shot 2014-10-28 at 1.51.02 PM

  1. Write a GUI application, YI_Car.java and its JFrame class to draw a car like the one in the picture.
    Screen Shot 2014-10-28 at 1.54.03 PM

  2. Write a GUI application, YI_BullEye.java and its JFrame class to draw the picture.
    Screen Shot 2014-10-28 at 1.56.35 PM

  3. Write a GUI application, YI_OlympicRings.java and its JFrame class to draw famous rings.
    Screen Shot 2014-10-28 at 1.56.51 PM


Screen Shot 2014-10-28 at 1.43.03 PM

How did Zach Yazdani combine the two classes?
You need to have both methods: paintComponent(Graphics g) and main in one file

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class RectangleObject extends JComponent
{
    public void paintComponent(Graphics g)
    {
      Graphics2D g2 = (Graphics2D) g;
      // Construct a rectangle object
      Rectangle box = new Rectangle(5, 10, 20, 30);
      // draw the rectangle
      g2.draw(box);  
      g2.draw(new Ellipse2D.Double(200,200,40,100));
      g2.draw(new Line2D.Double(50,30,200,100));
      g2.drawPolygon(new int [] {10,20,30}, new int [] {100,20,100}, 3);
    }
       public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.setSize(300,400);
        frame.setTitle("A Rectanlge Object in a JFrame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        RectangleObject aRectangle = new RectangleObject();
        
        frame.add(aRectangle);
        frame.setVisible(true);
    }
}
//See attached Programs

GUI: JFrame

Classwork:
Use a JFrame to create an application, YI_ManyShapes.java and YI_ManyShapesJFrame.java that draws rectangles, ellipses and lines. Use different colors and fill.
Here is a good source for your application:
Screen Shot 2014-10-28 at 5.47.35 AM

Here is a simple example:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
/**
 * Write a description of class RectangleObject here.
 
 */
public class RectangleObject extends JComponent
{
    public void paintComponent(Graphics g)
    {
      Graphics2D g2 = (Graphics2D) g;

      // Construct a rectangle object
      Rectangle box = new Rectangle(5, 10, 20, 30);
      // draw the rectangle
      g2.draw(box);  
    }
   
}

The JFrame class:

import javax.swing.JFrame;
/**
 * Draw an empty frame using JFrame.
 * 
 */

public class RectangleJFrame
{
       public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.setSize(300,400);
        frame.setTitle("A Rectanlge Object in a JFrame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        RectangleObject aRectangle = new RectangleObject();
        
        frame.add(aRectangle);
        frame.setVisible(true);
    }
}

Homework:
Write an application, YI_HappyFace.java using what you learned from Manyshapes to draw a happy face. Make sure to include the JFrame class also.

GUI – DialogDemo.java

Check edmodo.com for questions on content.

import javax.swing.JOptionPane;


/* 1.4 example used by DialogDemo.java. */
class DialogViewer1{
   public static void main(String[] args) {
       JOptionPane.showMessageDialog(null, "Hello, World!"); System.exit(0);
    }
}
import javax.swing.JOptionPane;

public class DialogViewer2 {
    public static void main(String[] args) {
    
        String name = JOptionPane.showInputDialog("What is your name?"); 
        System.out.println(name);
        System.exit(0);

    }
 }

Homework: Prepare for more questions including JOptionPane.

GUI: Action Listener

ActionEvent, ActionListener, and MouseListener

Screen Shot 2015-10-28 at 8.17.50 PM

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
   An action listener that prints a message.
*/
public class ButtonListener implements ActionListener
{
   public void actionPerformed(ActionEvent event)
   {
      System.out.println("You clicked me!!!!");
   }            
}

import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
   This program demonstrates how to install an action listener.
*/
public class ButtonViewer
{  
   private static final int FRAME_WIDTH = 100;
   private static final int FRAME_HEIGHT = 60;

   public static void main(String[] args)
   {  
      JFrame frame = new JFrame();
      JButton button = new JButton("Click here if you dare!");
      frame.add(button);
     
      ActionListener listener = new ButtonListener();
      button.addActionListener(listener);

      frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}


Screen Shot 2014-11-03 at 10.10.30 AM

Screen Shot 2015-10-28 at 8.53.39 PM

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;

/**
   This component displays a circle that can be moved. 
*/
public class ShapeComponent extends JComponent
{  
   private static final int SQUARE_X = 100;
   private static final int SQUARE_Y = 100;
   private static final int SQUARE_WIDTH = 50;
   private static final int SQUARE_HEIGHT = 50;

   private Ellipse2D.Double circle;

   public ShapeComponent()
   {  
      // The circle that the paintComponent method draws
      circle = new Ellipse2D.Double(SQUARE_X, SQUARE_Y, SQUARE_WIDTH, SQUARE_HEIGHT);         
   }

   public void paintComponent(Graphics g)
   {  
      Graphics2D g2 = (Graphics2D) g;

      g2.draw(circle);
   }

   /**
      Moves the rectangle to the given location.
      @param x the x-position of the new location
      @param y the y-position of the new location
   */
   public void moveTo(int x, int y)
   {
      circle = new Ellipse2D.Double(x, y, SQUARE_WIDTH, SQUARE_HEIGHT);
      repaint();      
   }
} 

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import java.awt.Color;

/**
   This program displays a ShapeComponent.
*/
public class ShapeComponentViewer
{  
   private static final int FRAME_WIDTH = 300;
   private static final int FRAME_HEIGHT = 400;

   public static void main(String[] args)
   {        
      final ShapeComponent component = new ShapeComponent();

      // Add mouse press listener         

      class MousePressListener implements MouseListener
      {  
         public void mousePressed(MouseEvent event)
         {  
            int x = event.getX();
            int y = event.getY();
            component.moveTo(x, y);
         }

         // Do-nothing methods
         public void mouseReleased(MouseEvent event) {}
         public void mouseClicked(MouseEvent event) {}
         public void mouseEntered(MouseEvent event) {}
         public void mouseExited(MouseEvent event) {}
      }
         
      MouseListener listener = new MousePressListener();
      component.addMouseListener(listener);

      JFrame frame = new JFrame();
      frame.getContentPane().setBackground(new Color(255, 0, 0));
      frame.add(component);

      frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
} 

Classwork:
Use the resources given to create a new GUI application, YI_CrazyCircle.java and its JFrame to generate random colors background and random filled circles wherever you click the mouse on the window. PLEASE INCLUDE SCREEN SHOT OF YOUR WORK.
Hint: To be able to also change the background at random create a rectangle of size equivalent to the JFrame. Instead of creating two public classes, create one inner class, the JComponnet inside main so it can have access to the JFrame size.

Q4 Grades and Assignments

I hope you and your family are well.
Q4 assignments and projects must be in by June 11th since I must submit all grades by June 16th.
Submit whatever you have done by that date preferably before if possible. 
 
Any submitted assignment with issues should have a description of the problem in the first line of your documentation.
If you cannot complete an assignment, email me and let me know about it. I am flexible and open to suggestions. Talk to me!!
 
 
After June 11th,
 
Stay healthy. Stay safe.

mrs.e

❤️

CS Department: University of Washington

My Old Website

NOTE: This old web site is out of date. This is the course web site from a past quarter, 15wi (Winter 2015), but the current quarter is 19su (Summer 2019). If you are a current student taking the course, this is not your class web site, and you should visit the current class web site instead.

Links

Programming:

For your homeworks:

  • Unofficial style guide, developed by a TA. Style tips are covered chapter by chapter with sections at the end for indentation, spacing, and commenting.

Software:

Computer Science Major:

Other:

Pi Day Activity

March 11th, 2016

pi1

Monday is Pi Day – We will have a pi-day activity ending on Monday.
Starting today we are celebrating Pi day by preparing to write a program to do one of the following:
1. Calculate the digits of Pi to highest possible precision. DOCUMENT YOUR MATHEMATICAL EXPRESSION AND THE LINK TO THE RESOURCE.
2. Illustrate how Pi is used in a circle.
3. Illustrate how Pi can be applied.
4. Illustrate how Pi was discovered by telling an interactive story.

300px-Circle_radians

220px-2pi-unrolled

From https://en.wikipedia.org/wiki/Radian

The programs will be judge based on ingenuity and/or creativity.
NOTE: You can use online resources to help you develop your program. There are good algorithms online. You can not use already written programs.
Include any links used for your resources.

If you like to memorize some of the digits of pi, there will be a competition on Friday. The student who can write the most digits without any help, get a small trophy.

Homework: Explore and investigate the uses and history of pi.