{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L21 - Computer Science 101 SS C Lecture 21 Contents 2...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CompSci101 Lecture 21 1 Computer Science 101 SS C Lecture 21 Contents Using standard input and output. Define a JFrame class. Create an instance of the JFrame class. Define the title and position for the JFrame instance. Define a JPanel class. Create a JPanel object and add it to the visible area of the JFrame. Define a paintComponent() method inside the JPanel class. The Graphics object - instance methods Course Book: §15 1 2 3 4 5 6 2 Standard input and output So far we have written programs which are able to process input from the keyboard ( Keyboard.readInput() ) and which are able produce output to the terminal window ( System.out.println() ). public class L21 { public void start() { } } String message = Keyboard.readInput() ; System.out.println( message ) ; 3 JFrame - is a Window, a Container We now want to write applications which create their own windows and display graphics inside the window area. A JFrame object is a window with a border and a title bar. The visible area of a JFrame object is a Container which to we will be able to add components, such as a JPanel object. (Later we will use a graphical interface to obtain user input and to display output.) Firstly we want our applications to create a graphical window. 1 2 3 4 5 6 4 JFrame - preliminary definition setVisible(true) : this ensures that the JFrame object is visible on the screen and is in front of any other open windows. import javax.swing.*; public class MyJFrame extends JFrame { public MyJFrame() { } } javax.swing package: contains the definition of a JFrame class. This package needs to be imported. extends JFrame : the class has all the functionality of a JFrame object e.g. a title bar and a border and many instance methods which we can use such as setTitle(), setVisible(). setVisible(true); 1 2 3 4 5 6 5 JFrame - create a JFrame object When using JFrames we will not be using a program class. Instead we create an instance of a JFrame class, in this case MyJFrame, in our applications, for example, import javax.swing.*; public class L21 { public static void main(String[] args) { } } JFrame window = new MyJFrame() ; 6 A window for drawing, 1 So far the JFrame (or window) object created is blank inside. We would like now to display some drawing inside this window. Before we can do this we will need to: add the instance of the JPanel to the JFrame. and define a JPanel class, create an instance of this JPanel class.
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CompSci101 Lecture 21 2 7 A window for drawing, 2 The JFrame instance which we create is our window with title, menu bar and border. The JPanel instance which we will add to the JFrame contains all the visible things we want to see inside the JFrame window. Once we have added a JPanel instance to the JFrame we can draw inside the JPanel area and, later still, we will add components to the JPanel. 1 2 3 4 5 8 JPanel – definition, 1 JPanel is a component in which we can draw shapes import javax.swing.*; public class MyJPanel extends JPanel { public MyJPanel() { } } The javax.swing package contains the definition of the JPanel class and therefore needs to be imported.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern