1 / 11

Java Concepts Chapter 2 – Graphical Applications

Java Concepts Chapter 2 – Graphical Applications. Mr. Smith AP Computer Science A. Graphical Applications and Frame Windows. Objective Overview of drawing graphical applications You will be able to draw basic graphics in a frame window (more attractive than a console window)

jenna
Download Presentation

Java Concepts Chapter 2 – Graphical Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java ConceptsChapter 2 – Graphical Applications Mr. Smith AP Computer Science A

  2. Graphical Applicationsand Frame Windows Objective • Overview of drawing graphical applications • You will be able to draw basic graphics in a frame window (more attractive than a console window) • Construct a frame of a certain size • Construct a component (such as a rectangles, circles, or a combination) • Add the component to the frame • Make the frame visible

  3. Graphical Applicationsand Frame Windows Constructing a Frame • Construct an object of the JFrame class JFrame frame = new JFrame(); • Set the size of the frame frame.setSize(300, 400); //Frame will be 300 pixels wide and 400 pixels tall //Omitting this step will make the frame 0x0 • Set the title of the frame (optional) frame.setTitle("Graphical Application"); • Set the "default close operation". frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //When the user closes the frame, the program will end • Make the frame visible frame.setVisible(true);

  4. Graphical Applicationsand Frame Windows Creating a Rectangle Component Object • In order to draw something in the frame, you must first create a component object and then add it to the frame • A component can consist of multiple graphics objects • Here are some of the classes that need to be imported in order to create a component object that draws a rectangle: import java.awt.Graphics; //primitive class – stores graphics state //including the current color, font, etc. import java.awt.Graphics2D; //Class used to draw shapes import java.awt.Rectangle; //Rectangles class import javax.swing.JComponent; //Used to draw in a frame

  5. Graphical Applicationsand Frame Windows Example of a Rectangle Component Object Class (used for creating, drawing, and positioning Rectangle objects) import java.awt.Graphics; //Allows you to manipulate the graphics //state (color, etc.) import java.awt.Graphics2D; //Contains methods to draw shapes import java.awt.Rectangle; import javax.swing.JComponent; public class RectangleComponent extends JComponent { //Place all the drawing instructions inside paintComponent method public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; //Cast to recover Graphics2D Rectangle box = new Rectangle(5, 10, 20, 30); //Construct rectangle g2.draw(box); //Draw rectangle box.translate(15, 25); //Move rectangle 15 units right and 25 down g2.draw(box); //Draw rectangle again } }

  6. Graphical Applicationsand Frame Windows Drawing a Rectangle Component • Construct a frame (see previous slide) • Construct an object of your component class RectangleComponent component = new RectangleComponent(); • Add the component to the frame frame.add(component); • Make the frame visible (see previous slide)

  7. Graphical Applicationsand Frame Windows Write a Client Program to View the Rectangles (used for instantiating the RectangleComponent object and adding it to the frame) import javax.swing.JFrame; public class RectangleViewer { public static void main(String [] args) { JFrame frame = new JFrame(); frame.setSize(300, 400); frame.setTitle("Graphics Application"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); RectangleComponent component = new RectangleComponent(); frame.add(component); frame.setVisible(true); } }

  8. Graphical Applicationsand Frame Windows Drawing a Face using Basic Shapes • You must first construct a "face" component and then add it to the frame • The face component will consist of different types of graphical objects for the head, eyes, and mouth • Here are some of the classes that need to be imported in order to use graphics: import java.awt.Color; //color class import java.awt.Graphics; //primitive graphics class import java.awt.Graphics2D;//Contains methods to draw shapes import java.awt.Rectangle; //Rectangles class Import java.awt.geom.Ellipse2D; Import java.awt.geom.Line2D; Import javax.swing.JPanel; import javax.swing.JComponent;

  9. Graphical Applicationsand Frame Windows Write a Face Component Class (used for creating, drawing, and positioning a face) import java.awt.Color; //color class import java.awt.Graphics; //primitive graphics class import java.awt.Graphics2D; //Extends Graphics class import java.awt.Rectangle; //Rectangles class import java.awt.geom.Ellipse2D; //Ellipse and circle class import java.awt.geom.Line2D; //Line class import javax.swing.JPanel; import javax.swing.JComponent; public class FaceComponentextendsJComponent { //Place drawing instructions inside this method public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; //Make graphics 2D Ellipse2D.Double head = new Ellipse2D.Double(5, 10, 100, 150); //Construct head (x, y, width, height) g2.draw(head); //Draw head Line2D.Double eye1 = new Line2D.Double(25, 70, 45, 90); //Construct left eye (x1, y1, x2, y2) g2.draw(eye1); Line2D.Double eye2 = new Line2D.Double(85, 70, 65, 90); //Construct right eye (x1, y1, x2, y2) g2.draw(eye2); Rectangle mouth = new Rectangle(30, 130, 50, 5); //Construct mouth (x, y, width, height) g2.setColor(Color.RED); //Color of mouth g2.fill(mouth); //Fill mouth with red g2.setColor(Color.BLUE); g2.drawString("Hello, Class!!!", 5, 175); //Draw greeting (“message”, x, y) } }

  10. Graphical Applicationsand Frame Windows Write a Client Program to View the Face (used for creating the FaceComponent object and adding it to the frame) import javax.swing.JFrame; public class FaceViewer { public static void main(String [] args) { JFrame frame = new JFrame(); //Instantiate new frame frame.setSize(300, 400); //Set frame size frame.setTitle("Alien Face"); //Set frame title frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); FaceComponent component = new FaceComponent(); //Instantiate component frame.add(component); //Add component to frame frame.setVisible(true); //Set frame visible } }

  11. Graphical Applicationsand Frame Windows Classwork/Homework Assignment • Download the following classes from my website: • FaceComponent – Face drawing object • FaceViewer – View the face • Use these classes as a starting point for your component class and client program to draw it in a frame • Refer to Java Concepts 2.11-2.13 for drawing other graphical objects (ellipse, lines, rectangles, etc.) • You can also refer to the link on my website named Drawing Geometric Shapes • Use your imagination and draw an object of your choice • Remember to include your name (JohnDoe) at the end of all classes you create, so I know who created them • Once these are finished, we will show them to the class

More Related