SlideShare a Scribd company logo
1 of 21
Download to read offline
Frame Class
Lecture 11
Naveen Kumar
A Simple Program
import javax.swing.JFrame;
import javax.swing.JLabel;
class HelloWorldFrame extends JFrame
{
public static void main(String args[])
{ JFrame f = new JFrame();
f. setSize(100, 100);
JLabel X = new JLabel("Hello World");
f.add(X);
f.setVisible(true);
} }
2
A Simple Program
import javax.swing.JFrame;
import javax.swing.JLabel;
class HelloWorldFrame extends JFrame
{
public static void main(String args[])
{ new HelloWorldFrame();
}
HelloWorldFrame()
{ JLabel X = new JLabel("Hello World");
add(X);
setSize(100, 100);
setVisible(true);
} }3
Problem: on window close cursor will not go
on C: prompt
Frame Windows
 The JFrame class JFrame frame = new JFrame();
 frame.setSize(300, 400);
frame.setTitle("An Empty Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
 import javax.swing.*;
4
Frame program
import javax.swing.*;
public class frame
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(300, 400);
frame.setTitle("An Empty Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
5
Drawing Shapes
 paintComponent: called whenever the component
needs to be repainted: (to create component)
public class frame1 extends JComponent
{
public void paintComponent(Graphics g)
{
// Recover Graphics2D
Graphics2D g2 = (Graphics2D) g;
. . .
}
}
6
Drawing Shapes
 Graphics class lets you manipulate the graphics state
(such as current color)
 Graphics2D class has methods to draw shape objects
 Use a cast to recover the Graphics2D object from the
Graphics parameter
Rectangle box = new Rectangle(5, 10, 20, 30);
g2.draw(box);
 java.awt package7
Rectangle Drawing Program
 frame1: its paintComponent method produces the drawing
 frame: its main method constructs a frame and a frame1, adds the
component to the frame, and makes the frame visible
– Construct a frame
– Construct an object of your component class:
frame1 component = new frame1();
– Add the component to the frame
frame.add(component);
– Make the frame visible frame.setVisible(true);
8
An example (produce a drawing
with two boxes)
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent; /** A component that draws two rectangles. */
public class frame1 extends JComponent {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g; // Recover Graphics2D
Rectangle box = new Rectangle(5, 10, 20, 30); // Construct a rectangle and
g2.draw(box); // draw it
box.translate(15, 25); // Move rectangle 15 units to the right and 25 units down
g2.draw(box); // Draw moved rectangle
} }
9
Create frame and add drawing
import javax.swing.JFrame;
public class frame {
public static void main(String[] args) {
JFrame frame = new JFrame();
final int FRAME_WIDTH = 300;
final int FRAME_HEIGHT = 400;
frame.setSize(FRAME_WIDTH,FRAME_HEIGHT);
frame.setTitle("Two rectangles");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1 component = new frame1();
frame.add(component);
frame.setVisible(true);
} }10
Applets
 This is almost the same outline as for a component, with two minor
differences:
– You extend JApplet, [not JComponent]
– You place the drawing code inside the paint method, not inside
paintComponent
 To run an applet, you need an HTML file with the applet tag
 You view applets with the appletviewer
11
JApplet
/* <APPLET CODE="frameapplet.class" WIDTH=350 HEIGHT=200>
</APPLET>*/
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JApplet; /** An applet that draws two rectangles. */
public class frameapplet extends JApplet
{ public void paint(Graphics g)
{ Graphics2D g2 = (Graphics2D) g;
Rectangle box = new Rectangle(5, 10, 20, 30);
g2.draw(box);
box.translate(15, 25);
g2.draw(box);
} }
12
Graphical Shapes
 Rectangle, Ellipse2D.Double, and Line2D.Double describe graphical
shapes
 We won't use the .Float classes
 These classes are inner classes–doesn't matter to us except for the
import statement:
import java.awt.geom.Ellipse2D; // no .Double
 Must construct and draw the shape
Ellipse2D.Double ellipse = new Ellipse2D.Double(x, y, width, height);
g2.draw(ellipse);
13
Drawing Lines
To draw a line:
 Line2D.Double segment = new Line2D.Double(x1, y1, x2, y2);
or
 Point2D.Double from = new Point2D.Double(x1, y1);
Point2D.Double to = new Point2D.Double(x2, y2);
Line2D.Double segment = new Line2D.Double(from, to);
14
Self Check
 Give instructions to draw a circle with center (100,100) and radius 25
 Give instructions to draw a letter "V" by drawing two line segments
 Give instructions to draw a string consisting of the letter "V"
Answers
 g2.draw(new Ellipse2D.Double(75, 75, 50, 50);
 Line2D.Double segment1 = new Line2D.Double(0, 0, 10, 30);
g2.draw(segment1);
Line2D.Double segment2 = new Line2D.Double(10, 30, 20, 0);
g2.draw(segment2);
 g2.drawString("V", 0, 30);
15
Upper-left corner, Width , Height
Colors
 Standard colors Color.BLUE, Color.RED, Color.PINK etc.
 Specify red, green, blue between 0.0F and 1.0F
 Color magenta = new Color(1.0F, 0.0F, 1.0F); // F = float/real
Set color in graphics context
 g2.setColor(magenta);
Color is used when drawing and filling shapes
 g2.fill(rectangle); // filled with current color
16
Self Check
 What are the RGB color values of Color.BLUE?
 How do you draw a yellow square on a red background?
Answers
 0.0F, 0.0F, and 0.1F
 First fill a big red square, then fill a small yellow square inside:
g2.setColor(Color.RED);
g2.fill(new Rectangle(0, 0, 200, 200));
g2.setColor(Color.YELLOW);
g2.fill(new Rectangle(50, 50, 100, 100));
Note: Use import java.awt.Color;
17
Drawing Graphical Shapes
 Rectangle leftRectangle
= new Rectangle(100, 100, 30, 60);
Rectangle rightRectangle
= new Rectangle(160, 100, 30, 60);
Line2D.Double topLine
= new Line2D.Double(130, 100, 160, 100);
Line2D.Double bottomLine
= new Line2D.Double(130, 160, 160, 160);
18
Reading Text Input
 A graphical application can obtain input by displaying a
JOptionPane
 The showInputDialog method displays a prompt and
waits for user input
 The showInputDialog method returns the string that the
user typed
String input = JOptionPane.showInputDialog("Enter x");
double x = Double.parseDouble(input);
19
An Example
import java.awt.Color; import javax.swing.Jframe;
import javax.swing.JOptionPane; import javax.swing.JComponent;
public class ColorViewer {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(300,400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String input; // Ask the user for red, green, blue values
input = JOptionPane.showInputDialog("red:");
double red = Double.parseDouble(input);
input = JOptionPane.showInputDialog("green:");
double green = Double.parseDouble(input);
input = JOptionPane.showInputDialog("blue:");
double blue = Double.parseDouble(input);
Color fillColor = new Color( (float) red, (float) green, (float) blue);
Square component = new Square (fillColor);
frame.add(component); frame.setVisible(true); } }20
Example cont.
import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle
public class Square extends JComponent {
private Color fillColor;
public Square (Color aColor) { fillColor = aColor; }
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g; // Select color into graphics context
g2.setColor(fillColor); // Const and fill a square whose center is center of the window
Rectangle square = new Rectangle(20,40,100,100);
g2.fill(square); }
}
21

More Related Content

What's hot

Computer graphics lab report with code in cpp
Computer graphics lab report with code in cppComputer graphics lab report with code in cpp
Computer graphics lab report with code in cppAlamgir Hossain
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsVishvjeet Yadav
 
Matlab assignment
Matlab assignmentMatlab assignment
Matlab assignmentRutvik
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manualUma mohan
 
Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Palak Sanghani
 
Computer graphics practical(jainam)
Computer graphics practical(jainam)Computer graphics practical(jainam)
Computer graphics practical(jainam)JAINAM KAPADIYA
 
COMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALCOMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALVivek Kumar Sinha
 
Java Graphics
Java GraphicsJava Graphics
Java GraphicsShraddha
 
Computer graphics lab assignment
Computer graphics lab assignmentComputer graphics lab assignment
Computer graphics lab assignmentAbdullah Al Shiam
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manualAnkit Kumar
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsKandarp Tiwari
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_papervandna123
 
SE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of PuneSE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of PuneBhavesh Shah
 

What's hot (20)

Computer graphics lab report with code in cpp
Computer graphics lab report with code in cppComputer graphics lab report with code in cpp
Computer graphics lab report with code in cpp
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commands
 
Monad
MonadMonad
Monad
 
Matlab assignment
Matlab assignmentMatlab assignment
Matlab assignment
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
Container adapters
Container adaptersContainer adapters
Container adapters
 
Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]
 
Computer graphics practical(jainam)
Computer graphics practical(jainam)Computer graphics practical(jainam)
Computer graphics practical(jainam)
 
OpenVX 1.0 Reference Guide
OpenVX 1.0 Reference GuideOpenVX 1.0 Reference Guide
OpenVX 1.0 Reference Guide
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
COMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALCOMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUAL
 
Java Graphics
Java GraphicsJava Graphics
Java Graphics
 
Advance java
Advance javaAdvance java
Advance java
 
java graphics
java graphicsjava graphics
java graphics
 
OpenVX 1.3 Reference Guide
OpenVX 1.3 Reference GuideOpenVX 1.3 Reference Guide
OpenVX 1.3 Reference Guide
 
Computer graphics lab assignment
Computer graphics lab assignmentComputer graphics lab assignment
Computer graphics lab assignment
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
 
SE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of PuneSE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of Pune
 

Viewers also liked

The AWT and Swing
The AWT and SwingThe AWT and Swing
The AWT and Swingadil raja
 
4 gu is-andinheritance
4 gu is-andinheritance4 gu is-andinheritance
4 gu is-andinheritancenotshoaib
 
Rock Candy | Beauty &amp; The Beast
Rock Candy | Beauty &amp; The BeastRock Candy | Beauty &amp; The Beast
Rock Candy | Beauty &amp; The BeastMathew C.P Hayward
 
10.3 Android Video
10.3 Android Video10.3 Android Video
10.3 Android VideoOum Saokosal
 
JAVA GUI PART I
JAVA GUI PART IJAVA GUI PART I
JAVA GUI PART IOXUS 20
 
Java OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - SwingJava OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - SwingOUM SAOKOSAL
 

Viewers also liked (12)

Oop lecture9 10
Oop lecture9 10Oop lecture9 10
Oop lecture9 10
 
The AWT and Swing
The AWT and SwingThe AWT and Swing
The AWT and Swing
 
4 gu is-andinheritance
4 gu is-andinheritance4 gu is-andinheritance
4 gu is-andinheritance
 
Centipetal force[1]
Centipetal force[1]Centipetal force[1]
Centipetal force[1]
 
UCM 6
UCM 6UCM 6
UCM 6
 
Rock Candy | Beauty &amp; The Beast
Rock Candy | Beauty &amp; The BeastRock Candy | Beauty &amp; The Beast
Rock Candy | Beauty &amp; The Beast
 
10.3 Android Video
10.3 Android Video10.3 Android Video
10.3 Android Video
 
Swing
SwingSwing
Swing
 
JAVA GUI PART I
JAVA GUI PART IJAVA GUI PART I
JAVA GUI PART I
 
Java OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - SwingJava OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - Swing
 
Java swing
Java swingJava swing
Java swing
 
Centripetal Force
Centripetal ForceCentripetal Force
Centripetal Force
 

Similar to Lec 11 12_sept [compatibility mode]

HELPModify the code so that the ListItem contains two values, inst.pdf
HELPModify the code so that the ListItem contains two values, inst.pdfHELPModify the code so that the ListItem contains two values, inst.pdf
HELPModify the code so that the ListItem contains two values, inst.pdfmonikajain201
 
This is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdfThis is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdfanjandavid
 
draw a pikachu in java package arreyreview- import java-awt--- import.pdf
draw a pikachu in java package arreyreview- import java-awt--- import.pdfdraw a pikachu in java package arreyreview- import java-awt--- import.pdf
draw a pikachu in java package arreyreview- import java-awt--- import.pdfJake3sTAveryn
 
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfImport java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfapexcomputer54
 
Please read this carefully needs to be in JAVA        Java 2D intr.pdf
Please read this carefully needs to be in JAVA        Java 2D intr.pdfPlease read this carefully needs to be in JAVA        Java 2D intr.pdf
Please read this carefully needs to be in JAVA        Java 2D intr.pdfPRATIKSINHA7304
 
This code currently works... Run it and get a screen shot of its .docx
 This code currently works... Run it and get a screen shot of its .docx This code currently works... Run it and get a screen shot of its .docx
This code currently works... Run it and get a screen shot of its .docxKomlin1
 
explain how 2D drawing is done in Java using Swing- Have you done this.docx
explain how 2D drawing is done in Java using Swing- Have you done this.docxexplain how 2D drawing is done in Java using Swing- Have you done this.docx
explain how 2D drawing is done in Java using Swing- Have you done this.docxjames876543264
 
Trident International Graphics Workshop 2014 1/5
Trident International Graphics Workshop 2014 1/5Trident International Graphics Workshop 2014 1/5
Trident International Graphics Workshop 2014 1/5Takao Wada
 
Test beautycleanness
Test beautycleannessTest beautycleanness
Test beautycleannessbergel
 
Inheritance and-polymorphism
Inheritance and-polymorphismInheritance and-polymorphism
Inheritance and-polymorphismUsama Malik
 
Java_practical_handbook
Java_practical_handbookJava_practical_handbook
Java_practical_handbookManusha Dilan
 
Task Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdfTask Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdfcronkwurphyb44502
 
ch03g-graphics.ppt
ch03g-graphics.pptch03g-graphics.ppt
ch03g-graphics.pptMahyuddin8
 
1z0 851 exam-java standard edition 6 programmer certified professional
1z0 851 exam-java standard edition 6 programmer certified professional1z0 851 exam-java standard edition 6 programmer certified professional
1z0 851 exam-java standard edition 6 programmer certified professionalIsabella789
 

Similar to Lec 11 12_sept [compatibility mode] (20)

HELPModify the code so that the ListItem contains two values, inst.pdf
HELPModify the code so that the ListItem contains two values, inst.pdfHELPModify the code so that the ListItem contains two values, inst.pdf
HELPModify the code so that the ListItem contains two values, inst.pdf
 
This is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdfThis is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdf
 
draw a pikachu in java package arreyreview- import java-awt--- import.pdf
draw a pikachu in java package arreyreview- import java-awt--- import.pdfdraw a pikachu in java package arreyreview- import java-awt--- import.pdf
draw a pikachu in java package arreyreview- import java-awt--- import.pdf
 
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfImport java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
 
Please read this carefully needs to be in JAVA        Java 2D intr.pdf
Please read this carefully needs to be in JAVA        Java 2D intr.pdfPlease read this carefully needs to be in JAVA        Java 2D intr.pdf
Please read this carefully needs to be in JAVA        Java 2D intr.pdf
 
This code currently works... Run it and get a screen shot of its .docx
 This code currently works... Run it and get a screen shot of its .docx This code currently works... Run it and get a screen shot of its .docx
This code currently works... Run it and get a screen shot of its .docx
 
explain how 2D drawing is done in Java using Swing- Have you done this.docx
explain how 2D drawing is done in Java using Swing- Have you done this.docxexplain how 2D drawing is done in Java using Swing- Have you done this.docx
explain how 2D drawing is done in Java using Swing- Have you done this.docx
 
Trident International Graphics Workshop 2014 1/5
Trident International Graphics Workshop 2014 1/5Trident International Graphics Workshop 2014 1/5
Trident International Graphics Workshop 2014 1/5
 
Griffon @ Svwjug
Griffon @ SvwjugGriffon @ Svwjug
Griffon @ Svwjug
 
Test beautycleanness
Test beautycleannessTest beautycleanness
Test beautycleanness
 
662305 11
662305 11662305 11
662305 11
 
Inheritance and-polymorphism
Inheritance and-polymorphismInheritance and-polymorphism
Inheritance and-polymorphism
 
Java_practical_handbook
Java_practical_handbookJava_practical_handbook
Java_practical_handbook
 
Task Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdfTask Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdf
 
ch03g-graphics.ppt
ch03g-graphics.pptch03g-graphics.ppt
ch03g-graphics.ppt
 
1z0 851 exam-java standard edition 6 programmer certified professional
1z0 851 exam-java standard edition 6 programmer certified professional1z0 851 exam-java standard edition 6 programmer certified professional
1z0 851 exam-java standard edition 6 programmer certified professional
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 

More from Palak Sanghani

More from Palak Sanghani (20)

Survey form
Survey formSurvey form
Survey form
 
Survey
SurveySurvey
Survey
 
Nature2
Nature2Nature2
Nature2
 
Texture
TextureTexture
Texture
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
 
Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]
 
Lec 4 06_aug [compatibility mode]
Lec 4 06_aug [compatibility mode]Lec 4 06_aug [compatibility mode]
Lec 4 06_aug [compatibility mode]
 
Lec 3 01_aug13
Lec 3 01_aug13Lec 3 01_aug13
Lec 3 01_aug13
 
Lec 2 30_jul13
Lec 2 30_jul13Lec 2 30_jul13
Lec 2 30_jul13
 
Lec 1 25_jul13
Lec 1 25_jul13Lec 1 25_jul13
Lec 1 25_jul13
 
Lec 5 13_aug [compatibility mode]
Lec 5 13_aug [compatibility mode]Lec 5 13_aug [compatibility mode]
Lec 5 13_aug [compatibility mode]
 
Nature
NatureNature
Nature
 
Comparisionof trees
Comparisionof treesComparisionof trees
Comparisionof trees
 
My Structure Patterns
My Structure PatternsMy Structure Patterns
My Structure Patterns
 
My Similarity Patterns
My Similarity PatternsMy Similarity Patterns
My Similarity Patterns
 
My Radiation Patterns
My Radiation PatternsMy Radiation Patterns
My Radiation Patterns
 
My Gradation Patterns
My Gradation PatternsMy Gradation Patterns
My Gradation Patterns
 
My Circular Patterns
My Circular PatternsMy Circular Patterns
My Circular Patterns
 
My Anomaly Patterns
My Anomaly PatternsMy Anomaly Patterns
My Anomaly Patterns
 
Library of Babel Illustrations
Library of Babel IllustrationsLibrary of Babel Illustrations
Library of Babel Illustrations
 

Recently uploaded

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 

Recently uploaded (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 

Lec 11 12_sept [compatibility mode]

  • 2. A Simple Program import javax.swing.JFrame; import javax.swing.JLabel; class HelloWorldFrame extends JFrame { public static void main(String args[]) { JFrame f = new JFrame(); f. setSize(100, 100); JLabel X = new JLabel("Hello World"); f.add(X); f.setVisible(true); } } 2
  • 3. A Simple Program import javax.swing.JFrame; import javax.swing.JLabel; class HelloWorldFrame extends JFrame { public static void main(String args[]) { new HelloWorldFrame(); } HelloWorldFrame() { JLabel X = new JLabel("Hello World"); add(X); setSize(100, 100); setVisible(true); } }3 Problem: on window close cursor will not go on C: prompt
  • 4. Frame Windows  The JFrame class JFrame frame = new JFrame();  frame.setSize(300, 400); frame.setTitle("An Empty Frame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);  import javax.swing.*; 4
  • 5. Frame program import javax.swing.*; public class frame { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(300, 400); frame.setTitle("An Empty Frame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } 5
  • 6. Drawing Shapes  paintComponent: called whenever the component needs to be repainted: (to create component) public class frame1 extends JComponent { public void paintComponent(Graphics g) { // Recover Graphics2D Graphics2D g2 = (Graphics2D) g; . . . } } 6
  • 7. Drawing Shapes  Graphics class lets you manipulate the graphics state (such as current color)  Graphics2D class has methods to draw shape objects  Use a cast to recover the Graphics2D object from the Graphics parameter Rectangle box = new Rectangle(5, 10, 20, 30); g2.draw(box);  java.awt package7
  • 8. Rectangle Drawing Program  frame1: its paintComponent method produces the drawing  frame: its main method constructs a frame and a frame1, adds the component to the frame, and makes the frame visible – Construct a frame – Construct an object of your component class: frame1 component = new frame1(); – Add the component to the frame frame.add(component); – Make the frame visible frame.setVisible(true); 8
  • 9. An example (produce a drawing with two boxes) import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JComponent; /** A component that draws two rectangles. */ public class frame1 extends JComponent { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; // Recover Graphics2D Rectangle box = new Rectangle(5, 10, 20, 30); // Construct a rectangle and g2.draw(box); // draw it box.translate(15, 25); // Move rectangle 15 units to the right and 25 units down g2.draw(box); // Draw moved rectangle } } 9
  • 10. Create frame and add drawing import javax.swing.JFrame; public class frame { public static void main(String[] args) { JFrame frame = new JFrame(); final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 400; frame.setSize(FRAME_WIDTH,FRAME_HEIGHT); frame.setTitle("Two rectangles"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1 component = new frame1(); frame.add(component); frame.setVisible(true); } }10
  • 11. Applets  This is almost the same outline as for a component, with two minor differences: – You extend JApplet, [not JComponent] – You place the drawing code inside the paint method, not inside paintComponent  To run an applet, you need an HTML file with the applet tag  You view applets with the appletviewer 11
  • 12. JApplet /* <APPLET CODE="frameapplet.class" WIDTH=350 HEIGHT=200> </APPLET>*/ import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JApplet; /** An applet that draws two rectangles. */ public class frameapplet extends JApplet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; Rectangle box = new Rectangle(5, 10, 20, 30); g2.draw(box); box.translate(15, 25); g2.draw(box); } } 12
  • 13. Graphical Shapes  Rectangle, Ellipse2D.Double, and Line2D.Double describe graphical shapes  We won't use the .Float classes  These classes are inner classes–doesn't matter to us except for the import statement: import java.awt.geom.Ellipse2D; // no .Double  Must construct and draw the shape Ellipse2D.Double ellipse = new Ellipse2D.Double(x, y, width, height); g2.draw(ellipse); 13
  • 14. Drawing Lines To draw a line:  Line2D.Double segment = new Line2D.Double(x1, y1, x2, y2); or  Point2D.Double from = new Point2D.Double(x1, y1); Point2D.Double to = new Point2D.Double(x2, y2); Line2D.Double segment = new Line2D.Double(from, to); 14
  • 15. Self Check  Give instructions to draw a circle with center (100,100) and radius 25  Give instructions to draw a letter "V" by drawing two line segments  Give instructions to draw a string consisting of the letter "V" Answers  g2.draw(new Ellipse2D.Double(75, 75, 50, 50);  Line2D.Double segment1 = new Line2D.Double(0, 0, 10, 30); g2.draw(segment1); Line2D.Double segment2 = new Line2D.Double(10, 30, 20, 0); g2.draw(segment2);  g2.drawString("V", 0, 30); 15 Upper-left corner, Width , Height
  • 16. Colors  Standard colors Color.BLUE, Color.RED, Color.PINK etc.  Specify red, green, blue between 0.0F and 1.0F  Color magenta = new Color(1.0F, 0.0F, 1.0F); // F = float/real Set color in graphics context  g2.setColor(magenta); Color is used when drawing and filling shapes  g2.fill(rectangle); // filled with current color 16
  • 17. Self Check  What are the RGB color values of Color.BLUE?  How do you draw a yellow square on a red background? Answers  0.0F, 0.0F, and 0.1F  First fill a big red square, then fill a small yellow square inside: g2.setColor(Color.RED); g2.fill(new Rectangle(0, 0, 200, 200)); g2.setColor(Color.YELLOW); g2.fill(new Rectangle(50, 50, 100, 100)); Note: Use import java.awt.Color; 17
  • 18. Drawing Graphical Shapes  Rectangle leftRectangle = new Rectangle(100, 100, 30, 60); Rectangle rightRectangle = new Rectangle(160, 100, 30, 60); Line2D.Double topLine = new Line2D.Double(130, 100, 160, 100); Line2D.Double bottomLine = new Line2D.Double(130, 160, 160, 160); 18
  • 19. Reading Text Input  A graphical application can obtain input by displaying a JOptionPane  The showInputDialog method displays a prompt and waits for user input  The showInputDialog method returns the string that the user typed String input = JOptionPane.showInputDialog("Enter x"); double x = Double.parseDouble(input); 19
  • 20. An Example import java.awt.Color; import javax.swing.Jframe; import javax.swing.JOptionPane; import javax.swing.JComponent; public class ColorViewer { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(300,400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String input; // Ask the user for red, green, blue values input = JOptionPane.showInputDialog("red:"); double red = Double.parseDouble(input); input = JOptionPane.showInputDialog("green:"); double green = Double.parseDouble(input); input = JOptionPane.showInputDialog("blue:"); double blue = Double.parseDouble(input); Color fillColor = new Color( (float) red, (float) green, (float) blue); Square component = new Square (fillColor); frame.add(component); frame.setVisible(true); } }20
  • 21. Example cont. import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle public class Square extends JComponent { private Color fillColor; public Square (Color aColor) { fillColor = aColor; } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; // Select color into graphics context g2.setColor(fillColor); // Const and fill a square whose center is center of the window Rectangle square = new Rectangle(20,40,100,100); g2.fill(square); } } 21