Changing Font and Color in Java Applet
Java Programming

Changing Font and Color in Java Applet

 

To display text(write the text), you must first select a font. A font is specified by its names, such as “Arial”,  the style(plain, bold, italic, or bold italics), and the point size. A font is an object in Java, so we need to create it via a call to new before we can use it. The setColor methods call selects a Colour that is used for all subsequent drawing operations.

Java Program to set the Font And Color of some Text

import java.applet.*;

import java.awt.*;

public class Fonts extends Applet

{

public void paint(Graphics g)

{

Font f1=new Font(“TimesRoman”,Font.PLAIN,20);//f1 is used for writing(TimesRoman) plain text

Font f2=new Font(“Courier”,Font.BOLD,22);//f2 is used for writing(Courier) Bold text

Font f3=new Font(“Arial”,Font.ITALIC,18);//f3 is used for writing (Arial)Italics text

Font f4=new Font(“Times”,Font.BOLD+Font.ITALIC,24);

g.setColor(Color.blue);

g.setFont(f1);

g.drawString(“Times Roman “,20,40);

g.setColor(Color.pink);

g.setFont(f2);

g.drawString(“Courier bold “,20,60);

Color c=new Color(0,255,255);

g.setColor(c);

g.setFont (f3);

g.drawString(“Arial “,20,80);

g.setColor(Color.gray);

g.setFont(f4);

g.drawString(“Times bold “,20,120);

}}

//<applet code=”Fonts.class” height=500 width=500></applet>

Font and Color Class in Java Applet

Explanation….

The setColor method takes a parameter of type Color.

You can create an object of type Color by using the class variables available with the Color class or by specifying the red, green, and blue components.

g.setColor(Color.gray);

g.setColor(new Color(0,127,122));

By using these methods we can set the Color in the Applet.

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment