SlideShare ist ein Scribd-Unternehmen logo
1 von 2
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 defines several constants (for example, Color.black) to specify a number of common
colors. You can also create your own colors, using one of the color constructors. The most
commonly used forms 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.


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.

Once you have created a color, you can use it to set the foreground and/or background
color by using the setForeground( ) and setBackground( ) methods Color Methods getRed(
), getGreen( ), getBlue( ) You can obtain the red, green, and blue components of a color
independently using getRed( ), getGreen( ), and getBlue( ), shown here:

int getRed( )
int getGreen( )
int getBlue( )
THE JAVA LIBRARY

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( )

A Color Demonstration Applet

The following applet constructs several colors and draws various objects using these colors:
// 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);
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);
}
}

Weitere ähnliche Inhalte

Andere mochten auch

Sistema Génesis y Portales Institucionales
Sistema Génesis y Portales InstitucionalesSistema Génesis y Portales Institucionales
Sistema Génesis y Portales InstitucionalesMiguel_arci
 
Il sit per la protezione civile della provincia di Brindisi, 12a Conferenza A...
Il sit per la protezione civile della provincia di Brindisi, 12a Conferenza A...Il sit per la protezione civile della provincia di Brindisi, 12a Conferenza A...
Il sit per la protezione civile della provincia di Brindisi, 12a Conferenza A...Marco Palazzo
 
παρουσίαση3
παρουσίαση3παρουσίαση3
παρουσίαση3persitsa
 
0302曾衍彰長老-福音的目的
0302曾衍彰長老-福音的目的0302曾衍彰長老-福音的目的
0302曾衍彰長老-福音的目的萬隆基督的教會
 
Strategi for sosiale medier
Strategi for sosiale medierStrategi for sosiale medier
Strategi for sosiale mediernyggis
 
2013년 개발 결산 보고 최재형 ver.20131226
2013년 개발 결산 보고 최재형 ver.201312262013년 개발 결산 보고 최재형 ver.20131226
2013년 개발 결산 보고 최재형 ver.20131226Choi Jae Hyung
 
Sistem dokumentasi pelayanan di unit rawat jalan
Sistem dokumentasi pelayanan di unit rawat jalanSistem dokumentasi pelayanan di unit rawat jalan
Sistem dokumentasi pelayanan di unit rawat jalanOperator Warnet Vast Raha
 

Andere mochten auch (17)

Presentacion
PresentacionPresentacion
Presentacion
 
Lecture2 oopj
Lecture2 oopjLecture2 oopj
Lecture2 oopj
 
Lecture10 oopj
Lecture10 oopjLecture10 oopj
Lecture10 oopj
 
Lecture6 oopj
Lecture6 oopjLecture6 oopj
Lecture6 oopj
 
Sistema Génesis y Portales Institucionales
Sistema Génesis y Portales InstitucionalesSistema Génesis y Portales Institucionales
Sistema Génesis y Portales Institucionales
 
Lecture1 oopj
Lecture1 oopjLecture1 oopj
Lecture1 oopj
 
Lecture7 oopj
Lecture7 oopjLecture7 oopj
Lecture7 oopj
 
Lecture3 oopj
Lecture3 oopjLecture3 oopj
Lecture3 oopj
 
02 Hidup ini Pilihan
02 Hidup ini Pilihan02 Hidup ini Pilihan
02 Hidup ini Pilihan
 
Tik v
Tik vTik v
Tik v
 
Il sit per la protezione civile della provincia di Brindisi, 12a Conferenza A...
Il sit per la protezione civile della provincia di Brindisi, 12a Conferenza A...Il sit per la protezione civile della provincia di Brindisi, 12a Conferenza A...
Il sit per la protezione civile della provincia di Brindisi, 12a Conferenza A...
 
παρουσίαση3
παρουσίαση3παρουσίαση3
παρουσίαση3
 
0302曾衍彰長老-福音的目的
0302曾衍彰長老-福音的目的0302曾衍彰長老-福音的目的
0302曾衍彰長老-福音的目的
 
Makalah perhitungan derajat keasaman
Makalah perhitungan derajat keasamanMakalah perhitungan derajat keasaman
Makalah perhitungan derajat keasaman
 
Strategi for sosiale medier
Strategi for sosiale medierStrategi for sosiale medier
Strategi for sosiale medier
 
2013년 개발 결산 보고 최재형 ver.20131226
2013년 개발 결산 보고 최재형 ver.201312262013년 개발 결산 보고 최재형 ver.20131226
2013년 개발 결산 보고 최재형 ver.20131226
 
Sistem dokumentasi pelayanan di unit rawat jalan
Sistem dokumentasi pelayanan di unit rawat jalanSistem dokumentasi pelayanan di unit rawat jalan
Sistem dokumentasi pelayanan di unit rawat jalan
 

Ähnlich wie Lecture4 oopj

Colours and Menus - HMI Presentation
Colours and Menus - HMI PresentationColours and Menus - HMI Presentation
Colours and Menus - HMI PresentationANUP MULAY
 
Applets - lev' 2
Applets - lev' 2Applets - lev' 2
Applets - lev' 2Rakesh T
 
Working with color and font
Working with color and fontWorking with color and font
Working with color and fontmyrajendra
 
lectureAll-OpenGL-complete-Guide-Tutorial.pdf
lectureAll-OpenGL-complete-Guide-Tutorial.pdflectureAll-OpenGL-complete-Guide-Tutorial.pdf
lectureAll-OpenGL-complete-Guide-Tutorial.pdfSanjeevSaharan5
 
Client Side Programming with Applet
Client Side Programming with AppletClient Side Programming with Applet
Client Side Programming with Appletbackdoor
 
Image processing with matlab
Image processing with matlabImage processing with matlab
Image processing with matlabAman Gupta
 
CIS 115 Achievement Education--cis115.com
CIS 115 Achievement Education--cis115.comCIS 115 Achievement Education--cis115.com
CIS 115 Achievement Education--cis115.comagathachristie170
 
Android_Bootcamp_PPT_GDSC_ITS_Engineering
Android_Bootcamp_PPT_GDSC_ITS_EngineeringAndroid_Bootcamp_PPT_GDSC_ITS_Engineering
Android_Bootcamp_PPT_GDSC_ITS_EngineeringShivanshSeth6
 
CIS 115 Education for Service--cis115.com
CIS 115 Education for Service--cis115.com  CIS 115 Education for Service--cis115.com
CIS 115 Education for Service--cis115.com williamwordsworth10
 
CIS 115 Redefined Education--cis115.com
CIS 115 Redefined Education--cis115.comCIS 115 Redefined Education--cis115.com
CIS 115 Redefined Education--cis115.comagathachristie208
 
CIS 115 Education Counseling--cis115.com
CIS 115 Education Counseling--cis115.comCIS 115 Education Counseling--cis115.com
CIS 115 Education Counseling--cis115.comclaric59
 
CIS 115 Education in iCounseling ---cis115.com
CIS 115 Education in  iCounseling ---cis115.comCIS 115 Education in  iCounseling ---cis115.com
CIS 115 Education in iCounseling ---cis115.comclaric59
 
Hive Functions Cheat Sheet
Hive Functions Cheat SheetHive Functions Cheat Sheet
Hive Functions Cheat SheetHortonworks
 
U5 JAVA.pptx
U5 JAVA.pptxU5 JAVA.pptx
U5 JAVA.pptxmadan r
 
Develop a system flowchart and then write a menu-driven C++ program .pdf
Develop a system flowchart and then write a menu-driven C++ program .pdfDevelop a system flowchart and then write a menu-driven C++ program .pdf
Develop a system flowchart and then write a menu-driven C++ program .pdfleventhalbrad49439
 

Ähnlich wie Lecture4 oopj (20)

Colours and Menus - HMI Presentation
Colours and Menus - HMI PresentationColours and Menus - HMI Presentation
Colours and Menus - HMI Presentation
 
Graphics and Java 2D
Graphics and Java 2DGraphics and Java 2D
Graphics and Java 2D
 
Applets - lev' 2
Applets - lev' 2Applets - lev' 2
Applets - lev' 2
 
Working with color and font
Working with color and fontWorking with color and font
Working with color and font
 
lectureAll-OpenGL-complete-Guide-Tutorial.pdf
lectureAll-OpenGL-complete-Guide-Tutorial.pdflectureAll-OpenGL-complete-Guide-Tutorial.pdf
lectureAll-OpenGL-complete-Guide-Tutorial.pdf
 
Client Side Programming with Applet
Client Side Programming with AppletClient Side Programming with Applet
Client Side Programming with Applet
 
Image processing with matlab
Image processing with matlabImage processing with matlab
Image processing with matlab
 
java graphics
java graphicsjava graphics
java graphics
 
Java swing
Java swingJava swing
Java swing
 
CIS 115 Achievement Education--cis115.com
CIS 115 Achievement Education--cis115.comCIS 115 Achievement Education--cis115.com
CIS 115 Achievement Education--cis115.com
 
Android_Bootcamp_PPT_GDSC_ITS_Engineering
Android_Bootcamp_PPT_GDSC_ITS_EngineeringAndroid_Bootcamp_PPT_GDSC_ITS_Engineering
Android_Bootcamp_PPT_GDSC_ITS_Engineering
 
CIS 115 Education for Service--cis115.com
CIS 115 Education for Service--cis115.com  CIS 115 Education for Service--cis115.com
CIS 115 Education for Service--cis115.com
 
CIS 115 Redefined Education--cis115.com
CIS 115 Redefined Education--cis115.comCIS 115 Redefined Education--cis115.com
CIS 115 Redefined Education--cis115.com
 
CIS 115 Education Counseling--cis115.com
CIS 115 Education Counseling--cis115.comCIS 115 Education Counseling--cis115.com
CIS 115 Education Counseling--cis115.com
 
CIS 115 Education in iCounseling ---cis115.com
CIS 115 Education in  iCounseling ---cis115.comCIS 115 Education in  iCounseling ---cis115.com
CIS 115 Education in iCounseling ---cis115.com
 
Hive Functions Cheat Sheet
Hive Functions Cheat SheetHive Functions Cheat Sheet
Hive Functions Cheat Sheet
 
U5 JAVA.pptx
U5 JAVA.pptxU5 JAVA.pptx
U5 JAVA.pptx
 
first-applet
first-appletfirst-applet
first-applet
 
Learn Java 3D
Learn Java 3D Learn Java 3D
Learn Java 3D
 
Develop a system flowchart and then write a menu-driven C++ program .pdf
Develop a system flowchart and then write a menu-driven C++ program .pdfDevelop a system flowchart and then write a menu-driven C++ program .pdf
Develop a system flowchart and then write a menu-driven C++ program .pdf
 

Lecture4 oopj

  • 1. 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 defines several constants (for example, Color.black) to specify a number of common colors. You can also create your own colors, using one of the color constructors. The most commonly used forms 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. 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. Once you have created a color, you can use it to set the foreground and/or background color by using the setForeground( ) and setBackground( ) methods Color Methods getRed( ), getGreen( ), getBlue( ) You can obtain the red, green, and blue components of a color independently using getRed( ), getGreen( ), and getBlue( ), shown here: int getRed( ) int getGreen( ) int getBlue( ) THE JAVA LIBRARY 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( ) A Color Demonstration Applet The following applet constructs several colors and draws various objects using these colors:
  • 2. // 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); 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); } }