Csc 308 graphics programming
Download
1 / 15

- PowerPoint PPT Presentation


  • 431 Views
  • Uploaded on

CSC 308 – Graphics Programming Say Hi to Graphics i.e.: A first, simple graphics program Dr. Paige H. Meeker Computer Science Presbyterian College, Clinton, SC Lecture 2: Creating Simple Graphics in Java* Homework

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about '' - johana


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Csc 308 graphics programming l.jpg

CSC 308 – Graphics Programming

Say Hi to Graphics

i.e.: A first, simple graphics program

Dr. Paige H. Meeker

Computer Science

Presbyterian College, Clinton, SC


Lecture 2 l.jpg
Lecture 2:

  • Creating Simple Graphics in Java*

  • Homework

    * Material modified from “Computer Graphics via Java” by Ian Ferguson – online ebook and a nice download) http://www.ablibris.com/site/review.php3?bid=19


Features l.jpg
Features

  • Main window

  • Graphics canvas

  • Some lines drawn



Java s machinery l.jpg
Java’s Machinery

  • In order to draw things on screen (which is what this is all about) you have to have quite a bit of Java “machinery” in place before you can start.


Slide6 l.jpg

Gapp and Gcanvas are ours – we’re developing them. JFrame and JPanel from which they inherit, are part of the Java Foundation Classes - in particular they are part of the “swing” GUI components.

The class Gapp inherits from JFrame, so when a Gapp object is created, a new GUI window appears on the screen. A Gcanvas/JPanel is simply something we can draw on, but it isn’t allowed to be on screen by itself, it must be part of a JFrame. So what we actually need is an instance of Gapp with an instance of Gcanvas “inside” it


Gapp main l.jpg
Gapp – main() JFrame and JPanel from which they inherit, are part of the Java Foundation Classes - in particular they are part of the “swing” GUI components.

static public void main(String[] args) {

// Main entry point

try {

Gapp myGapp = new Gapp();

myGapp.initComponents();

myGapp.setVisible(true);

}

catch (Exception e) {

e.printStackTrace();

}

}//end main


Gapp initcomponents l.jpg
Gapp – initComponents() JFrame and JPanel from which they inherit, are part of the Java Foundation Classes - in particular they are part of the “swing” GUI components.

public void initComponents() throws Exception {

setLocation(new java.awt.Point(0, 30));

setSize(new java.awt.Dimension(350, 400));

setTitle("Graphics Application");

getContentPane().add(new Gcanvas());

addWindowListener(new java.awt.event.WindowAdapter() {

public void windowClosing(java.awt.event.WindowEvent e) {

thisWindowClosing(e);

}

});

}//end - initComponents


Slide9 l.jpg

public void paintComponent(Graphics g) { JFrame and JPanel from which they inherit, are part of the Java Foundation Classes - in particular they are part of the “swing” GUI components.

g.drawLine(100,50,100,150); g.drawLine(100,100,150,100); g.drawLine(150,100,150,150); g.drawLine(200,150,200,100); g.drawLine(200,50,200,60);

}


Upon closer examination l.jpg
Upon Closer Examination… JFrame and JPanel from which they inherit, are part of the Java Foundation Classes - in particular they are part of the “swing” GUI components.

you may be asking, this program doesn’t actually seem to call the paintComponent method and where does it get “g” the Graphics object from? You are right, it doesn’t. paintComponent is what is known as a “callback” method. It’s actually called by the system everytime the JPanel object it belongs to needs to be displayed on screen and likewise the system passes us the mysterious “g” to provide all of those useful functions - we don’t have to worry about it.


Slide11 l.jpg
So, JFrame and JPanel from which they inherit, are part of the Java Foundation Classes - in particular they are part of the “swing” GUI components.

Our program “hi” is actually a simple drawing of 5 lines, each with its own start and end point. To draw using this technique, it’s easiest to create a grid, label it, and plot your points.


Graphics primitives l.jpg
Graphics Primitives JFrame and JPanel from which they inherit, are part of the Java Foundation Classes - in particular they are part of the “swing” GUI components.

  • A graphics primitive can be loosely defined as a drawing function which the system makes available to the applications programmer.

  • Java implements a comprehensive set of graphics primitives. Let’s take a look at their description…


Java graphics class api l.jpg
Java Graphics Class API JFrame and JPanel from which they inherit, are part of the Java Foundation Classes - in particular they are part of the “swing” GUI components.

  • http://java.sun.com/j2se/1.5.0/docs/api/


Homework due wednesday 8 30 06 l.jpg
Homework: Due Wednesday, 8/30/06 JFrame and JPanel from which they inherit, are part of the Java Foundation Classes - in particular they are part of the “swing” GUI components.

  • Modify the Gcanvas.java code to draw a square.

  • Create from this a “Swirling Square” as seen below.


Homework hint l.jpg
Homework “Hint” JFrame and JPanel from which they inherit, are part of the Java Foundation Classes - in particular they are part of the “swing” GUI components.

  • This basically draws a square, then a second square inside the first reduced in size by 0.1 and slightly rotated. This continues for 40 iterations.

  • HINT: P1x = ((1-u)*Px)+(u*Qx) and P1y = ((1-u)*Py)+(u*Qy) and so on and so on…


ad