SlideShare une entreprise Scribd logo
1  sur  20
Working with color and font




                     http://improvejava.blogspot.in/
                                                       1
Objective

On completion of this period, you would be
able to know

• Working with color
• Working with font




              http://improvejava.blogspot.in/
                                                2
Recap

In the previous class, you have leant

• Displaying information within a window
• Working with Graphics




                  http://improvejava.blogspot.in/
                                                    3
Working with Color

• Java supports color in a portable, device-
  independent fashion
• The AWT color system allows you to specify any
  color you want
• It then finds the best match for that color, given
  the limits of the display hardware currently
  executing your program or applet
• Color is encapsulated by the Color class


                  http://improvejava.blogspot.in/   4
Working with Color                            contd..


• The most commonly used constructors are
  shown here
  – Color(int red, int green, int blue)
  – Color(int rgbValue)
  – Color(float red, float green, float blue)
• The first constructor takes three integers that
  specify the color as a mix of red, green, and blue
• These values must be between 0 and 255, as in
  this example
• new Color(255, 100, 100); // light red

                     http://improvejava.blogspot.in/             5
Working with Color                           contd..

• The second color constructor takes a single integer that
  contains the mix of red, green, and blue packed into an
  integer.
• The integer is organized with red in bits 16 to 23, green
  in bits 8 to 15, and blue in bits 0 to 7
• Here is an example of this constructor
   – int newRed = (0xff000000 | (0xc0 << 16) | (0x00 << 8) | 0x00);
   – Color darkRed = new Color(newRed);
• The final constructor, Color(float, float, float), takes three
  float values (between 0.0 and 1.0) that specify the
  relative mix of red, green, and blue


                        http://improvejava.blogspot.in/               6
Setting the Current Graphics Color

• By default, graphics objects are drawn in the
  current foreground color
• You can change this color by calling the
  Graphics method setColor( )
  – void setColor(Color newColor)
  – Here, newColor specifies the new drawing color
• You can obtain the current color by calling
  getColor( ), shown here
  – Color getColor( )

                   http://improvejava.blogspot.in/   7
A Color Demonstration Applet
// Demonstrate color.
import java.awt.*;
import java.applet.*;
/*
<applet code="ColorDemo" width=300 height=200>
</applet>
*/
public class ColorDemo extends Applet {
// draw lines
    public void paint(Graphics g) {
           Color c1 = new Color(255, 100, 100);
           Color c2 = new Color(100, 255, 100);
           Color c3 = new Color(100, 100, 255);
           g.setColor(c1);
           g.drawLine(0, 0, 100, 100);


                         http://improvejava.blogspot.in/   8
A Color Demonstration Applet                         contd..
         g.drawLine(0, 100, 100, 0);
         g.setColor(c2);
         g.drawLine(40, 25, 250, 180);
         g.drawLine(75, 90, 400, 400);
         g.setColor(c3);
         g.drawLine(20, 150, 400, 40);
         g.drawLine(5, 290, 80, 19);
         g.setColor(Color.red);
         g.drawOval(10, 10, 50, 50);
         g.fillOval(70, 90, 140, 100);
         g.setColor(Color.blue);
         g.drawOval(190, 10, 90, 30);
         g.drawRect(10, 10, 60, 50);
         g.setColor(Color.cyan);
         g.fillRect(100, 10, 60, 50);
         g.drawRoundRect(190, 10, 60, 50, 15, 15);
    }
}
                           http://improvejava.blogspot.in/             9
Working with Fonts

• The AWT supports multiple type fonts
• Fonts are encapsulated by the Font class
• Several of the methods defined by Font class




                 http://improvejava.blogspot.in/   10
Creating and Selecting a Font

• To select a new font, you must first construct a
  Font object that describes that font
• One Font constructor has this general form
   – Font(String fontName, int fontStyle, int
     pointSize)
   – Here, fontName specifies the name of the
     desired font



                  http://improvejava.blogspot.in/    11
Creating and Selecting a Font                      contd..

• To use a font that you have created, you must
  select it using setFont( ), which is defined by
  Component
• It has this general form
  – void setFont(Font fontObj)
  – Here, fontObj is the object that contains the desired
    font
• The following program outputs a sample of each
  standard font
• Each time you click the mouse within its window,
  a new font is selected and its name is displayed

                    http://improvejava.blogspot.in/             12
Creating and Selecting a Font                        contd..
// Show fonts.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="SampleFonts" width=200 height=100>
</applet>
*/
public class SampleFonts extends Applet {
   int next = 0;
   Font f;
  String msg;
  public void init() {
       f = new Font("Dialog", Font.PLAIN, 12);


                        http://improvejava.blogspot.in/             13
Creating and Selecting a Font                      contd..

       msg = "Dialog";
       setFont(f);
       addMouseListener(new MyMouseAdapter(this));
       }
       public void paint(Graphics g) {
               g.drawString(msg, 4, 20);
       }
}
class MyMouseAdapter extends MouseAdapter {
   SampleFonts sampleFonts;
   public MyMouseAdapter(SampleFonts sampleFonts) {
        this.sampleFonts = sampleFonts;
   }

                      http://improvejava.blogspot.in/             14
Creating and Selecting a Font                          contd..

public void mousePressed(MouseEvent me) {
// Switch fonts with each mouse click.
      sampleFonts.next++;
      switch(sampleFonts.next) {
         case 0:
             sampleFonts.f = new Font("Dialog", Font.PLAIN, 12);
             sampleFonts.msg = "Dialog";
             break;
         case 1:
            sampleFonts.f = new Font("DialogInput", Font.PLAIN, 12);
            sampleFonts.msg = "DialogInput";
            break;
          case 2:
            sampleFonts.f = new Font("SansSerif", Font.PLAIN, 12);
            sampleFonts.msg = "SansSerif";
            break;

                        http://improvejava.blogspot.in/                15
Creating and Selecting a Font                          contd..

       case 3:
                 sampleFonts.f = new Font("Serif", Font.PLAIN, 12);
                 sampleFonts.msg = "Serif";
                 break;
       case 4:
                 sampleFonts.f = new Font("Monospaced", Font.PLAIN,
12);
                 sampleFonts.msg = "Monospaced";
                 break;
       }
       if(sampleFonts.next == 4) sampleFonts.next = -1;
               sampleFonts.setFont(sampleFonts.f);
               sampleFonts.repaint();
       }
}

                         http://improvejava.blogspot.in/              16
Creating and Selecting a Font                     contd..


• Sample output from this program is shown here:




            Fig. 69.1 Font creation and selection


                   http://improvejava.blogspot.in/             17
Summary

• In this class we have discussed
   – The constructors and methods of
      • Color
      • Font classes
   – The relevant programs




                 http://improvejava.blogspot.in/   18
Quiz

1. What is the default color for drawing of Graphics
    object ?
  a)   Black
  b)   Current background color
  c)   Blue
  d)   Current foreground color




                   http://improvejava.blogspot.in/   19
Frequently Asked Questions

1. List the constructors and methods of Color class
2. List the constructors and methods of Font class




                  http://improvejava.blogspot.in/   20

Contenu connexe

Tendances (20)

Event handling
Event handlingEvent handling
Event handling
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Java-java virtual machine
Java-java virtual machineJava-java virtual machine
Java-java virtual machine
 
Java layoutmanager
Java layoutmanagerJava layoutmanager
Java layoutmanager
 
Dot net assembly
Dot net assemblyDot net assembly
Dot net assembly
 
C# Framework class library
C# Framework class libraryC# Framework class library
C# Framework class library
 
Java socket programming
Java socket programmingJava socket programming
Java socket programming
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
Java swing
Java swingJava swing
Java swing
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
 
Introduction to .NET Framework
Introduction to .NET FrameworkIntroduction to .NET Framework
Introduction to .NET Framework
 
Java awt
Java awtJava awt
Java awt
 
java swing
java swingjava swing
java swing
 
Overview of UML Diagrams
Overview of UML DiagramsOverview of UML Diagrams
Overview of UML Diagrams
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
graphics programming in java
graphics programming in javagraphics programming in java
graphics programming in java
 
class and objects
class and objectsclass and objects
class and objects
 
Fragment
Fragment Fragment
Fragment
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 

Similaire à Working with color and font

U5 JAVA.pptx
U5 JAVA.pptxU5 JAVA.pptx
U5 JAVA.pptxmadan r
 
Debugger & Profiler in NetBeans
Debugger & Profiler in NetBeansDebugger & Profiler in NetBeans
Debugger & Profiler in NetBeansHuu Bang Le Phan
 
LECTURE 6 DESIGN, DEBasd, INTERFACES.pdf
LECTURE 6 DESIGN, DEBasd, INTERFACES.pdfLECTURE 6 DESIGN, DEBasd, INTERFACES.pdf
LECTURE 6 DESIGN, DEBasd, INTERFACES.pdfShashikantSathe3
 
LECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdf
LECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdfLECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdf
LECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdfSHASHIKANT346021
 
Introduction to programming - class 2
Introduction to programming - class 2Introduction to programming - class 2
Introduction to programming - class 2Paul Brebner
 
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docx
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docxWeek 2 iLab TCO 2 — Given a simple problem, design a solutio.docx
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docxmelbruce90096
 
Applet &amp; graphics programming
Applet &amp; graphics programmingApplet &amp; graphics programming
Applet &amp; graphics programmingShekh Ahmed
 
Devry cis 170 c i lab 5 of 7 arrays and strings
Devry cis 170 c i lab 5 of 7 arrays and stringsDevry cis 170 c i lab 5 of 7 arrays and strings
Devry cis 170 c i lab 5 of 7 arrays and stringsshyaminfo04
 
Devry cis 170 c i lab 5 of 7 arrays and strings
Devry cis 170 c i lab 5 of 7 arrays and stringsDevry cis 170 c i lab 5 of 7 arrays and strings
Devry cis 170 c i lab 5 of 7 arrays and stringsash52393
 
Client Side Programming with Applet
Client Side Programming with AppletClient Side Programming with Applet
Client Side Programming with Appletbackdoor
 
Chapter13_7e.ppt
Chapter13_7e.pptChapter13_7e.ppt
Chapter13_7e.pptmohaz5
 
Presentation on design pattern software project lll
 Presentation on design pattern  software project lll  Presentation on design pattern  software project lll
Presentation on design pattern software project lll Uchiha Shahin
 

Similaire à Working with color and font (20)

L18 applets
L18 appletsL18 applets
L18 applets
 
U5 JAVA.pptx
U5 JAVA.pptxU5 JAVA.pptx
U5 JAVA.pptx
 
Ms vb
Ms vbMs vb
Ms vb
 
Vb introduction.
Vb introduction.Vb introduction.
Vb introduction.
 
Debugger & Profiler in NetBeans
Debugger & Profiler in NetBeansDebugger & Profiler in NetBeans
Debugger & Profiler in NetBeans
 
LECTURE 6 DESIGN, DEBasd, INTERFACES.pdf
LECTURE 6 DESIGN, DEBasd, INTERFACES.pdfLECTURE 6 DESIGN, DEBasd, INTERFACES.pdf
LECTURE 6 DESIGN, DEBasd, INTERFACES.pdf
 
Applets
AppletsApplets
Applets
 
LECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdf
LECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdfLECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdf
LECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdf
 
Introduction to programming - class 2
Introduction to programming - class 2Introduction to programming - class 2
Introduction to programming - class 2
 
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docx
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docxWeek 2 iLab TCO 2 — Given a simple problem, design a solutio.docx
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docx
 
Applet &amp; graphics programming
Applet &amp; graphics programmingApplet &amp; graphics programming
Applet &amp; graphics programming
 
Applets
AppletsApplets
Applets
 
Graphics and Java 2D
Graphics and Java 2DGraphics and Java 2D
Graphics and Java 2D
 
Devry cis 170 c i lab 5 of 7 arrays and strings
Devry cis 170 c i lab 5 of 7 arrays and stringsDevry cis 170 c i lab 5 of 7 arrays and strings
Devry cis 170 c i lab 5 of 7 arrays and strings
 
Devry cis 170 c i lab 5 of 7 arrays and strings
Devry cis 170 c i lab 5 of 7 arrays and stringsDevry cis 170 c i lab 5 of 7 arrays and strings
Devry cis 170 c i lab 5 of 7 arrays and strings
 
Client Side Programming with Applet
Client Side Programming with AppletClient Side Programming with Applet
Client Side Programming with Applet
 
Chapter13_7e.ppt
Chapter13_7e.pptChapter13_7e.ppt
Chapter13_7e.ppt
 
GUI.pdf
GUI.pdfGUI.pdf
GUI.pdf
 
Presentation on design pattern software project lll
 Presentation on design pattern  software project lll  Presentation on design pattern  software project lll
Presentation on design pattern software project lll
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 

Plus de myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

Working with color and font

  • 1. Working with color and font http://improvejava.blogspot.in/ 1
  • 2. Objective On completion of this period, you would be able to know • Working with color • Working with font http://improvejava.blogspot.in/ 2
  • 3. Recap In the previous class, you have leant • Displaying information within a window • Working with Graphics http://improvejava.blogspot.in/ 3
  • 4. Working with Color • Java supports color in a portable, device- independent fashion • The AWT color system allows you to specify any color you want • It then finds the best match for that color, given the limits of the display hardware currently executing your program or applet • Color is encapsulated by the Color class http://improvejava.blogspot.in/ 4
  • 5. Working with Color contd.. • The most commonly used constructors are shown here – Color(int red, int green, int blue) – Color(int rgbValue) – Color(float red, float green, float blue) • The first constructor takes three integers that specify the color as a mix of red, green, and blue • These values must be between 0 and 255, as in this example • new Color(255, 100, 100); // light red http://improvejava.blogspot.in/ 5
  • 6. Working with Color contd.. • The second color constructor takes a single integer that contains the mix of red, green, and blue packed into an integer. • The integer is organized with red in bits 16 to 23, green in bits 8 to 15, and blue in bits 0 to 7 • Here is an example of this constructor – int newRed = (0xff000000 | (0xc0 << 16) | (0x00 << 8) | 0x00); – Color darkRed = new Color(newRed); • The final constructor, Color(float, float, float), takes three float values (between 0.0 and 1.0) that specify the relative mix of red, green, and blue http://improvejava.blogspot.in/ 6
  • 7. Setting the Current Graphics Color • By default, graphics objects are drawn in the current foreground color • You can change this color by calling the Graphics method setColor( ) – void setColor(Color newColor) – Here, newColor specifies the new drawing color • You can obtain the current color by calling getColor( ), shown here – Color getColor( ) http://improvejava.blogspot.in/ 7
  • 8. A Color Demonstration Applet // Demonstrate color. import java.awt.*; import java.applet.*; /* <applet code="ColorDemo" width=300 height=200> </applet> */ public class ColorDemo extends Applet { // draw lines public void paint(Graphics g) { Color c1 = new Color(255, 100, 100); Color c2 = new Color(100, 255, 100); Color c3 = new Color(100, 100, 255); g.setColor(c1); g.drawLine(0, 0, 100, 100); http://improvejava.blogspot.in/ 8
  • 9. A Color Demonstration Applet contd.. g.drawLine(0, 100, 100, 0); g.setColor(c2); g.drawLine(40, 25, 250, 180); g.drawLine(75, 90, 400, 400); g.setColor(c3); g.drawLine(20, 150, 400, 40); g.drawLine(5, 290, 80, 19); g.setColor(Color.red); g.drawOval(10, 10, 50, 50); g.fillOval(70, 90, 140, 100); g.setColor(Color.blue); g.drawOval(190, 10, 90, 30); g.drawRect(10, 10, 60, 50); g.setColor(Color.cyan); g.fillRect(100, 10, 60, 50); g.drawRoundRect(190, 10, 60, 50, 15, 15); } } http://improvejava.blogspot.in/ 9
  • 10. Working with Fonts • The AWT supports multiple type fonts • Fonts are encapsulated by the Font class • Several of the methods defined by Font class http://improvejava.blogspot.in/ 10
  • 11. Creating and Selecting a Font • To select a new font, you must first construct a Font object that describes that font • One Font constructor has this general form – Font(String fontName, int fontStyle, int pointSize) – Here, fontName specifies the name of the desired font http://improvejava.blogspot.in/ 11
  • 12. Creating and Selecting a Font contd.. • To use a font that you have created, you must select it using setFont( ), which is defined by Component • It has this general form – void setFont(Font fontObj) – Here, fontObj is the object that contains the desired font • The following program outputs a sample of each standard font • Each time you click the mouse within its window, a new font is selected and its name is displayed http://improvejava.blogspot.in/ 12
  • 13. Creating and Selecting a Font contd.. // Show fonts. import java.applet.*; import java.awt.*; import java.awt.event.*; /* <applet code="SampleFonts" width=200 height=100> </applet> */ public class SampleFonts extends Applet { int next = 0; Font f; String msg; public void init() { f = new Font("Dialog", Font.PLAIN, 12); http://improvejava.blogspot.in/ 13
  • 14. Creating and Selecting a Font contd.. msg = "Dialog"; setFont(f); addMouseListener(new MyMouseAdapter(this)); } public void paint(Graphics g) { g.drawString(msg, 4, 20); } } class MyMouseAdapter extends MouseAdapter { SampleFonts sampleFonts; public MyMouseAdapter(SampleFonts sampleFonts) { this.sampleFonts = sampleFonts; } http://improvejava.blogspot.in/ 14
  • 15. Creating and Selecting a Font contd.. public void mousePressed(MouseEvent me) { // Switch fonts with each mouse click. sampleFonts.next++; switch(sampleFonts.next) { case 0: sampleFonts.f = new Font("Dialog", Font.PLAIN, 12); sampleFonts.msg = "Dialog"; break; case 1: sampleFonts.f = new Font("DialogInput", Font.PLAIN, 12); sampleFonts.msg = "DialogInput"; break; case 2: sampleFonts.f = new Font("SansSerif", Font.PLAIN, 12); sampleFonts.msg = "SansSerif"; break; http://improvejava.blogspot.in/ 15
  • 16. Creating and Selecting a Font contd.. case 3: sampleFonts.f = new Font("Serif", Font.PLAIN, 12); sampleFonts.msg = "Serif"; break; case 4: sampleFonts.f = new Font("Monospaced", Font.PLAIN, 12); sampleFonts.msg = "Monospaced"; break; } if(sampleFonts.next == 4) sampleFonts.next = -1; sampleFonts.setFont(sampleFonts.f); sampleFonts.repaint(); } } http://improvejava.blogspot.in/ 16
  • 17. Creating and Selecting a Font contd.. • Sample output from this program is shown here: Fig. 69.1 Font creation and selection http://improvejava.blogspot.in/ 17
  • 18. Summary • In this class we have discussed – The constructors and methods of • Color • Font classes – The relevant programs http://improvejava.blogspot.in/ 18
  • 19. Quiz 1. What is the default color for drawing of Graphics object ? a) Black b) Current background color c) Blue d) Current foreground color http://improvejava.blogspot.in/ 19
  • 20. Frequently Asked Questions 1. List the constructors and methods of Color class 2. List the constructors and methods of Font class http://improvejava.blogspot.in/ 20