SlideShare a Scribd company logo
5
Most read
8
Most read
9
Most read
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

More Related Content

PPTX
JAVA AWT
PDF
Java threads
PPTX
Visual Basic Controls ppt
PPT
Java awt
PPTX
Event handling
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPT
java swing
PPTX
Exception Handling in Java
JAVA AWT
Java threads
Visual Basic Controls ppt
Java awt
Event handling
Basic Concepts of OOPs (Object Oriented Programming in Java)
java swing
Exception Handling in Java

What's hot (20)

PPT
Working with frames
PPTX
Java swing
PDF
Java Thread Synchronization
PPTX
java interface and packages
PPT
Thread model in java
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PPT
Java And Multithreading
PDF
Java I/o streams
PPTX
Function overloading and overriding
PPTX
Virtual base class
PPT
Ch 3 event driven programming
PPT
Java: GUI
PPT
Java interfaces
PPTX
Static Members-Java.pptx
PPTX
Java awt (abstract window toolkit)
PPT
Abstract class in java
PPT
Method overriding
PDF
PPTX
Access specifiers(modifiers) in java
Working with frames
Java swing
Java Thread Synchronization
java interface and packages
Thread model in java
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Java And Multithreading
Java I/o streams
Function overloading and overriding
Virtual base class
Ch 3 event driven programming
Java: GUI
Java interfaces
Static Members-Java.pptx
Java awt (abstract window toolkit)
Abstract class in java
Method overriding
Access specifiers(modifiers) in java
Ad

Similar to Working with color and font (20)

PPTX
L18 applets
PPTX
U5 JAVA.pptx
PPT
Ms vb
PPT
Vb introduction.
DOCX
Debugger & Profiler in NetBeans
PDF
LECTURE 6 DESIGN, DEBasd, INTERFACES.pdf
PPT
Applets
PDF
LECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdf
PPTX
Applets in Java. Learn java program with applets
PDF
Introduction to programming - class 2
DOCX
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docx
PPT
Applet &amp; graphics programming
PPTX
Applets
PPT
Graphics and Java 2D
DOC
Devry cis 170 c i lab 5 of 7 arrays and strings
DOC
Devry cis 170 c i lab 5 of 7 arrays and strings
PPT
Appletsbjhbjiibibibikbibibjibjbibbjb.ppt
PPT
Applets(1)cusdhsiohisdhfshihfsihfohf.ppt
PPT
Client Side Programming with Applet
PPT
Chapter13_7e.ppt
L18 applets
U5 JAVA.pptx
Ms vb
Vb introduction.
Debugger & Profiler in NetBeans
LECTURE 6 DESIGN, DEBasd, INTERFACES.pdf
Applets
LECTURE 6 DESIGN, DEBUGGING, INTERFACES.pdf
Applets in Java. Learn java program with applets
Introduction to programming - class 2
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docx
Applet &amp; graphics programming
Applets
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 strings
Appletsbjhbjiibibibikbibibjibjbibbjb.ppt
Applets(1)cusdhsiohisdhfshihfsihfohf.ppt
Client Side Programming with Applet
Chapter13_7e.ppt
Ad

More from myrajendra (20)

PPT
Fundamentals
PPT
Data type
PPTX
Hibernate example1
PPTX
Jdbc workflow
PPTX
2 jdbc drivers
PPTX
3 jdbc api
PPTX
4 jdbc step1
PPTX
Dao example
PPTX
Sessionex1
PPTX
Internal
PPTX
3. elements
PPTX
2. attributes
PPTX
1 introduction to html
PPTX
Headings
PPTX
Forms
PPT
PPTX
Views
PPTX
Views
PPTX
Views
PPT
Starting jdbc
Fundamentals
Data type
Hibernate example1
Jdbc workflow
2 jdbc drivers
3 jdbc api
4 jdbc step1
Dao example
Sessionex1
Internal
3. elements
2. attributes
1 introduction to html
Headings
Forms
Views
Views
Views
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