• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Java

Java Class Example Code

By Dinesh Thakur

A Java class is a group of Java objects all of which have the same or similar properties in common. Classes are logical entities and can never be physical. In a class  you may find:
● Fields
● Methods
● Constructors
● Blocks
● Nested classes and an interface
[Read more…] about Java Class Example Code

Find Largest and Smallest Number in an Array Using Java Example

By Dinesh Thakur

In the Following example LargestandSmallestnumber shows how to Find Largest and Smallest Number in an Array Using Java Example [Read more…] about Find Largest and Smallest Number in an Array Using Java Example

Changing the Cursor in Java Example

By Dinesh Thakur

In the Following example ChangeCursor shows how to Changing the Cursor in Java with  setCursor() method defined in the Component class. setCursor method is used to changes the cursor icon when the cursor goes upon the particular component. and the getPredefinedCursor() method returns the cursor object to be specified in the cursor block. Changing Cursor means showing cursor in many different  shapes when the cursor goes upon the particular component.

 

Here is the java code for the program ChangeCursor :

[Read more…] about Changing the Cursor in Java Example

Create a Simple Window Using JFrame – Java

By Dinesh Thakur

The basic component that require whenever we implement a visual interface to rid Swing is the JFrame (top-level container) class. This class encapsulates a classic window any operating system with graphical environment (Windows, OS X, Linux etc.)

We have said that this class is in the javax.swing package and as we usually use several classes to import this package then use the syntax: import javax.swing.*;

1. In the beginning of the program, the javax.swing package is imported.
2. Next, JavaSimpleFrame class is declared. An object of JFrame class is created using the statement.
      JFrame frame =new JFrame ();
It creates a container called frame that defines a rectangular window with a specified string displayed on the title bar.
3. The size of the window is specified by using the statement.
        frame.setSize (350, 250);
The general form of setSize() method to set the size of the window is:
void setSize (int width, int height)
where,
width is the width of the window
height is the height of the window
4. The statement used for terminating the program when the window is closed is:
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
When the above statement is executed, the entire application terminates with the closing of window.
The general form of setDefaultCloseOperation() is
      void setDefaultCloseOperation (int w)
where, w specifies the action to be performed on the window when it is closed. It has several options which are as follows
JFrame.DISPOSE_ON_CLOSE
JFrame.HIDE_ON_CLOSE
JFrame.DO_NOTHING_ON_CLOSE
By default, when the top-level window is closed, the application is not terminated. It simply removes the window from the screen.
5. The statement to make the window visible is
frame.setVisible(true);
The setVisible () method is inherited from the AWT Component class. If it is set to true, the window will be displayed, otherwise not. By default, a JFrame is invisible.

import javax.swing.*;
class JavaSimpleFrame
{
          public static void main(String[] args)
          {
          //thread safe app
          javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {new SimpleFrame();}});
      }
}
       class SimpleFrame extends JFrame
       {
        private static final int WIDTH = 350;
        private static final int HEIGHT = 250;
        public SimpleFrame() {
        //make sure we have nice Java's look and feel window
        setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame();
        frame.setTitle("Simple Frame");
        frame.setSize(WIDTH, HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
     }
}

Simple Window Using JFrame

 

A Simple Window Using Frame Example in Java

By Dinesh Thakur

[Read more…] about A Simple Window Using Frame Example in Java

Java Program to Find Odd or Even Numbers | Java Examples

By Dinesh Thakur

In the Following example OddorEvenNumbers use mod operator to check if the number is even or odd. If the number divided by 2 and the reminder is 0 then number is even, otherwise the number is odd.

Here is the java code for the program OddorEvenNumbers :

[Read more…] about Java Program to Find Odd or Even Numbers | Java Examples

Calculate Rectangle Area Using Java Example

By Dinesh Thakur

In the Following example AreaofRectangleExample shows how to Calculate Area of Rectangle is length * width.

[Read more…] about Calculate Rectangle Area Using Java Example

Calculate Circle Perimeter Using Java Example

By Dinesh Thakur

In the Following example PerimeterofCircleExample shows how to Calculate Perimeter of Circle using 2*pi*r. where r is a radius of a circle.

[Read more…] about Calculate Circle Perimeter Using Java Example

Area of Circle Example in Java

By Dinesh Thakur

In the Following example AreaofCircleExample shows how to Calculate Area of Circle using pi*r*r. where r is a radius of a circle.

[Read more…] about Area of Circle Example in Java

Draw 3D Rectangles Java Example

By Dinesh Thakur

In the Following Draw3DRectExample shows how to Draw 3DRect or Fill 3DRect using draw3DRect,fill3DRect method of Graphics class. The Syntax for draw3DRect(int x_coordinates,int y_coordinates, int width, int height, boolean raised) and The Syntax for fill3DRect(int x_coordinates,int y_coordinates, int width, int height, boolean raised)

Here is the java code for the program Draw3DRectExample :.

[Read more…] about Draw 3D Rectangles Java Example

Draw Smiley in Applet Example

By Dinesh Thakur

Following example SmileExample shows how to Smily face using an Applet window with drawArc,fillArc, drawLine,fillOval drawOval method of Graphics class.

Here is the java code for the program SmileExample :.

[Read more…] about Draw Smiley in Applet Example

GetDocumentBase and getCodeBase Example

By Dinesh Thakur

In most of the applets, it is required to load text and images explicitly. Java enables loading data from two directories. The first one is the directory which contains the HTML file that started the applet (known as the document base). The other one is the directory from which the class file of the applet is loaded (known as the code base). These directories can be obtained as URL objects by using getDocumentBase ()and getCodeBase ()methods respectively. You can concatenate these URL objects with the string representing the name of the file that is to be loaded.

Here is the java code for the program GetDocumentBase and getCodeBase Example :.*/

[Read more…] about GetDocumentBase and getCodeBase Example

How to Set the Dimension of the Applet

By Dinesh Thakur

Eample AppletDimension print center aligned text “Hello Java” This Java example ‘How to Set the Dimension of the Applet’ shows how to print a string in center of an applet screen window using the Dimension class.

In the Following example AppletDimension shows How to Set the Dimension of the Applet using getSize(),getFontMetrics() method. The Syntax for getting X coordinates are

x_coordinates = dimension.width/2 – fontmetrics.stringWidth(str)/2 and The Syntax for getting Y coordinates are y_coordinates = dimension.height/2 – fontmetrics.getHeight()/2;

Here is the java code for the program AppletDimension Example :.

[Read more…] about How to Set the Dimension of the Applet

Fill Screen With Pixel in Applet windows Example

By Dinesh Thakur

In the Following example PixelsApplet shows how to Fill Screen With Pixel in Applet windows Example and set color of an Applet window using setColor(),Random(), method of Graphics class for color the screen. Following example demonstrates how to Random setcolor with RGB values, for this you can use a Color object.

Here is the java code for the program PixelsApplet :

[Read more…] about Fill Screen With Pixel in Applet windows Example

Draw Dashed Stroke Polygon Applet Window Example

By Dinesh Thakur

In the Following example DashedStrokeApplet shows how to Draw Dashed Stroke Polygon Applet Window Example using Graphics2D class and setPaint(),setStroke(),BasicStroke() method of Graphics class. The Syntax for drawPolygon(int[] xPoints, int[] yPoints, int numPoint); and The Syntax for BasicStroke(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase) ; setStroke is an inteface defined in the java.awt package.

Here is the java code for the program DashedStrokeApplet:.

import java.applet.Applet; 
import java.awt.*;
import java.awt.BasicStroke;
import java.awt.event.*;
public class DashedStrokeApplet extends Applet
{
           public static void main(String[] args)
        {
    DashedStrokeApplet DashedStroke = new DashedStrokeApplet();
    Frame StrokeApplet = new Frame("Draw Dashed Stroke Polygon Applet Window Example");
    StrokeApplet.add(DashedStroke);
    StrokeApplet.setSize(350, 250);
    StrokeApplet.setVisible(true);
    StrokeApplet.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
   System.exit(0); } });
       }
  public void paint (Graphics g)
    {
     g.setColor(Color.darkGray);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Stroke Polygon Applet Window Example", 10, 20);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
Graphics2D Gr2D = (Graphics2D) g;
    // Array of a dash pattern 40-pixel line, 10-pixel gap, 20-pixel line, 10-pixel gap
    float [] d1 = {40, 10, 20, 10};
     int[] X_polygon = {70, 210, 170, 210, 70, 110};
     int[] Y_polygon = {50,50, 120, 190, 190, 120};
    Gr2D.setPaint(Color.green);  
//The Syntax for BasicStroke(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase) ;
BasicStroke BasicS1 = new BasicStroke (9, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
    Gr2D.setStroke (BasicS1);
   Gr2D.drawPolygon (X_polygon, Y_polygon, 6);
    Gr2D.setPaint(Color.blue);  
BasicStroke BasicS2 = new BasicStroke (7, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0F, d1, 0.F);
    Gr2D.setStroke (BasicS2);
     //The Syntax for drawPolygon(int[] xPoints, int[] yPoints, int numPoint);
    Gr2D.drawPolygon (X_polygon, Y_polygon, 6);
     }
}

 

Dashed Stroke Polygon Applet Window Example

 

BasicStroke Example – Draw Dashed Line in Applet Window in Java

By Dinesh Thakur

In the Following example DashedLinesApplet shows with Basic Stroke how to Draw Dashed Line in Applet and set foreground color of an Applet window using Graphics2D class and setPaint(),setStroke(), BasicStroke() method of Graphics class. The Syntax for BasicStroke(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase) ; setStroke is an inteface defined in the java.awt package.

import java.awt.BasicStroke;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class DashedLinesApplet extends Applet
 {
            public static void main(String[] args)
       {
                 DashedLinesApplet DashesLines = new DashedLinesApplet();
                 Frame DLApplet = new Frame("Draw Dashed Line in Applet Window Example");
           DLApplet.add(DashesLines);
                DLApplet.setSize(350, 250);
                DLApplet.setVisible(true);
               DLApplet.addWindowListener(new WindowAdapter() {
               public void windowClosing(WindowEvent e) {System.exit(0); }
                                                                        });
      }
          public void paint(Graphics g)
           {
                g.setColor(Color.darkGray);
                g.setFont(new Font("Arial",Font.BOLD,14));
                g.drawString("Draw Dashed Line in Applet Window Example", 10, 40);
                g.setFont(new Font("Arial",Font.BOLD,10));
                g.drawString("http://ecomputernotes.com", 200, 205);
               Graphics2D Gr2D = (Graphics2D) g;
   
              // Array of a dash pattern 40-pixel line, 10-pixel gap, 20-pixel line, 10-pixel gap
              float[] d1 = { 40, 10, 20, 10 };
             //The Syntax for BasicStroke(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase) ;
          BasicStroke BasicS = new BasicStroke(1, BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL, 10, d1,0);
             Gr2D.setStroke(BasicS);
             Gr2D.setPaint(Color.red);
             Gr2D.drawLine(60, 80, 250, 80);
BasicStroke BasicS1 = new BasicStroke(1, BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER, 10, d1,0);
             Gr2D.setStroke(BasicS1);
             Gr2D.setPaint(Color.green);
             Gr2D.drawLine(60, 100, 250, 100);
BasicStroke BasicS2 = new BasicStroke(1, BasicStroke.CAP_SQUARE,BasicStroke.JOIN_ROUND, 10, d1,0);
             Gr2D.setStroke(BasicS2);
             Gr2D.setPaint(Color.blue);
             Gr2D.drawLine(60, 120, 250, 120);
        }
}

 

BasicStroke Example - Draw Dashed Line in Applet Window in Java

Draw Image in Applet Window Java Example

By Dinesh Thakur

In the Following example Draw Image in Applet shows how to Draw Image on Applet window using getDefaultToolkit(),drawImage(), method of Graphics class. To draw the images, we use java.awt.Graphics package comes with a drawImage() of Graphics class. The syntax for drawImage(image, int x, int y);

 

Here is the java code for the program Draw Image in Applet:.

[Read more…] about Draw Image in Applet Window Java Example

Drawing Polygons in Applet Window Example

By Dinesh Thakur

A polygon is a closed geometrical figure which can have any number of sides. A polygon can be drawn by using the drawPolygon () method. This method takes three parameters.

The general form of the drawPolygon () method is:

void drawPolygon (int a [], int b [], int n)

where,

a [ ] is the array of integers having x-coordinates

b [ ] is the array of integers having y-coordinates

n is the total number of coordinate points required to draw a polygon.

Here is the java code for the program AppletDrawPolygon :.

[Read more…] about Drawing Polygons in Applet Window Example

Draw Rounded Corner Rectangle & Square in Applet Window Example

By Dinesh Thakur

In the Following example AppletDrawRect shows how to Draw Rectangle or Fill Rectangle and draw filled rounded corner rectangle or set foreground color of an Applet window using drawRect,fillRect, drawRoundRect, fillRoundRect method of Graphics class. The Syntax for The Syntax for drawRect(int xTopLeft, int yTopLeft, int width, int height); and The Syntax for fillRect(int xTopLeft, int yTopLeft, int width, int height); and The Syntax for round cornered square drawRoundRect(int xTopLeft, int yTopLeft, int width, int height, int arcWidth, int arcHeight), and The Syntax for fillRoundRect(int xTopLeft, int yTopLeft, int width, int height, int arcWidth, int arcHeight)

Following example demonstrates how to set  color’s with RGB values, for this you can use a Color object. For AppletDrawRect example, an RGB(R for Red, G for Green and B

for Blue) value for dark blue Color is 0 red, 0 is green, and 200 is blue. Following example AppletDrawRect shows of an applet that displays dark blue Rectangle .

 

Here is the java code for the program AppletDrawRect :.

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class AppletDrawRect extends Applet
{
        public static void main(String[] args)
       {
           Frame RectApplet = new Frame("Draw Rectangle in Applet Window Example");
           RectApplet.setSize(350, 250);
           Applet AppletDrawRect = new AppletDrawRect();
           RectApplet.add(AppletDrawRect);
           RectApplet.setVisible(true);
           RectApplet.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);} });
        }
               public void paint(Graphics g)
                   {
                         g.setColor(Color.darkGray);
                         g.setFont(new Font("Arial",Font.BOLD,14));
                         g.drawString("Draw Rounded Corner Rectangle", 50, 40);
                         g.setFont(new Font("Arial",Font.BOLD,10));
                         g.drawString("http://ecomputernotes.com", 200, 205);
                          //set color to Blue
                         Color darkblue = new Color(0, 0, 200);
                         g.setColor(darkblue);
                         //this will draw a Rectangle of width 50 & height 70 at (10,50)
                         //The Syntax for drawRect(int xTopLeft, int yTopLeft, int  width, int height);
                          g.drawRect(10,50,50,70);
               
                         //this will Fill Rectangle of width 50 & height 70 at (70,50)
                         //The Syntax for fillRect(int xTopLeft, int yTopLeft, int width, int height);
                         g.fillRect(70,50,50,70);
                        // If you speficy same width 50 and height 50, the drawRect method will draw a square!
                  //The Syntax for  square drawRect(int xTopLeft, int yTopLeft, int width, int height);
                          g.drawRect(130,50,50,50);
                         //this will draw a round cornered rectangle of width 50 & height 70 at (10,130)
                        //The Syntax for round cornered square drawRoundRect(int xTopLeft, int yTopLeft, int width, int height, int arcWidth, int arcHeight)
                         g.drawRoundRect(10,130,50,70,20,20);
             
                        //draw filled rounded corner rectangle
                       //The Syntax for fillRoundRect(int xTopLeft, int yTopLeft, int  width, int height, int arcWidth, int arcHeight)
                        g.fillRoundRect(70,130,50,70,20,20);
             
                      //this will draw a round cornered square
                //The Syntax for round cornered square drawRoundRect(int xTopLeft, int yTopLeft, int width, int height, int arcWidth, int arcHeight)
                     g.drawRoundRect(130,130,50,50,10,10);
             
           }
}

 

 

Draw Rectangle in Applet Window Example

Draw Oval in Applet Window Example

By Dinesh Thakur

DrawOval The method can be used to draw ellipses or circles of  drawRect manner analogous to the method, ie by defining the location of the vertex  upper left (x, y) of the rectangle which fits the definition of ellipse and its width  width and height height, as indicated: 

In the Following example AppletDrawOval shows how to Draw Oval or Fill Oval and set foreground color of an Applet window using drawOval , fillOval, method of Graphics class.

The Syntax for drawOval(int xTopLeft, int yTopLeft, int width, int height); and The Syntax for fillOval(int xTopLeft, int yTopLeft, int width, int height);

Following example demonstrates how to set  color’s with RGB values, for this you can use a Color object. For AppletDrawOval example, an RGB(R for Red, G for Green and B

for Blue) value for dark red Color is 235 red, 50 is green, and 50 is blue. Following example AppletDrawOval shows of an applet that displays dark red Oval .

 

Here is the java code for the program AppletDrawOval :.

[Read more…] about Draw Oval in Applet Window Example

How to Draw a Line In Java with drawline() method.

By Dinesh Thakur

The simplest shape that you can draw with Graphics class is a line. The drawLine() method of the Graphics class is used to draw a line with current color between two points. This method takes the following form

void drawLine(int x1, int y1, int x2, int y2)

The DrawLine method can be used for drawing straight lines between two points (x1, y1) and (x2, y2) data. Following example DrawLine shows how to  Draw a Line on Applet window using drawLine method of Graphics class.

To draw a point at (x, y) we could write: g.drawLine (x, y, x, y); The point thus drawn using the current color of the graphics context, which can be changed using the setColor method.

For example, the statement g.drawLine (20, 100, 90, 100) will draw a straight line from the coordinate point (20, 100) to (90, 100) as shown in Figure

                       Figure A Straight Line Having Coordinates (20,100) and (90,100)

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
public class DrawLine extends Applet
{
      public static void main(String[] args)
    {
        Frame drawLineApplet = new Frame("Draw Line in Applet Window Example");
        drawLineApplet.setSize(350, 250);
        Applet DrawLine = new DrawLine();
        drawLineApplet.add(DrawLine);
        drawLineApplet.setVisible(true);
        drawLineApplet.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {System.exit(0); }
                                                                                   });
    }
  public void paint(Graphics g)
  {
          // Now we tell g to change the font
         g.setFont(new Font("Arial",Font.BOLD,14));
        //Syntax: drawString(String str, int xBaselineLeft, int yBaselineLeft);  
         g.drawString("This is Draw Line Example", 100, 70);  
        // draws a Line
         g.setColor(Color.blue);  // Now we tell g to change the color
        //Syntax For:- drawLine(int x1, int y1, int x2, int y2);
         g.drawLine(90, 135, 90, 180);
        g.setColor(Color.green);  // Now we tell g to change the color
        g.drawLine(0, 0, 100, 100);
   }
}

Draw Line in Applet Window Example

Draw Arc in Applet Window Example

By Dinesh Thakur

An arc can be drawn using the drawArc () method. This method takes six arguments in which the first four are same as the arguments of the drawoval () method and the next two represents the starting angle of the arc and the sweep angle around the arc, respectively.

The general form of the drawArc () method is:

void drawArc (int a1, int b1, int w, int h, int strt_angle, int sweep_angle)

where,

a1, b1 is the coordinate of the top left comer of the bounding rectangle

w is the width of the bounding rectangle

h is the height of the bounding rectangle

strt _angle is the starting angle of the arc (in degrees)

sweep_angle is the number of degrees (angular distance) around the arc (in degrees)

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class DrawArc extends Applet {
public static void main(String[] args) {
Frame DrawArcApplet = new Frame("Draw Arc in Applet Window Example");
DrawArcApplet.setSize(350, 250);
Applet DrawArc = new DrawArc();
DrawArcApplet.add(DrawArc);
DrawArcApplet.setVisible(true);
DrawArcApplet.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    // draws String
    g.drawString("This is Arc Shapes Example", 100, 70);
    // Draws an Arc Shape
    g.drawArc(30,5, 200, 200, 0, -90); //Syntax For:- drawArc(int xTopLeft, int yTopLeft, int width, int height, int startAngle, int arcAngle);
   
    // Fill an Arc Shape
    g.fillArc(30, 5, 200, 200, 0, -90);  //Syntax For:- fillArc(int xTopLeft, int yTopLeft, int width, int height, int startAngle, int arcAngle);
  }
}

 

Draw Arc in Applet Window Example

How to Change Background And Foreground Color of Applet in Java

By Dinesh Thakur

The Color class provides various methods to use any color you want in display. It defines various color constants which can be directly used only by specifying the color of your choice. In addition, the Color class allows creation of millions of colors. The Color class contains three primitive colors namely, red, blue and green and all other colors are a combination of these three colors. One of the constructors that is used to create color of your choice is

Color (int red, int green, int blue)

where,

red, green, blue can take any value between 0 and 255.

Setting Background and Foreground Color

To set the color of the background of an applet window, setBackground () method is used.

The general form of the setBackground () method is

void setBackground(mycolor)

Similarly, to set the foreground color to a specific color, that is, the color of text,

setForeground () method is used.

The general form of the setForeground () method is

void setForeground(mycolor)

where,

mycolor is one of the color constants or the new color created by the user

The list of color constants is given below:

• Color.red

• Color.orange

• Color.gray

• Color.darkGray

• Color.lightGray

• Color.cyan

• Color.pink

• Color.white

• Color.blue

• Color.green

• Color.black

• Color.yellow

import java.applet.Applet;
import java.awt.*;                //Color and Graphics belongs to java.awt,
import java.awt.event.*;
public class ColorApplet extends Applet
{
        public static void main(String[] args)
       {
        Frame ForegroundBackgroundColor = new Frame("Change Background and Foreground Color of Applet");
        ForegroundBackgroundColor.setSize(420, 180);
        Applet ColorApplet = new ColorApplet();
        ForegroundBackgroundColor.add(ColorApplet);
        ForegroundBackgroundColor.setVisible(true);
ForegroundBackgroundColor.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0); }
                                                                                            });
     }
  public void paint(Graphics g)
{
    Color c = getForeground();
    setBackground(Color.yellow);  /*There are many predefined colors, or you can create your own predefined colors to Change are
black,blue,cyan,darkGray,gray,green,lightGray,magenta,orange,pink,red,white,yellow */
    setForeground(Color.red);    /*There are many predefined colors, or you can create your own predefined colors to Change are
black,blue,cyan,darkGray,gray,green,lightGray,magenta,orange,pink,red,white,yellow */
    g.drawString("Foreground color set to red", 100, 50); // Drawing texts on the graphics screen:
    g.drawString(c.toString(), 100, 80); /*The drawString() method, takes parameters as instance of String containing where the text to be drawn, and two integer values specifying
the coordinates where the text should place.*/
   g.drawString("Change Background and Foreground Color of Applet", 50, 100);
  }
}

 

    Change Background and Foreground Color of Applet

Java Applet – How to Shapes Drawn in applet.

By Dinesh Thakur

In this shapeDrawingApplet program we see how to draw the different shapes like line, circle and rectangle.

GraphicsObject.drawLine() :
The drawLine() method is used in this program to draw the line. Here is the syntax:

GraphicsObject.drawLine(int x_coordinate, int y_coordinate, int x1_coordinate, int y1_coordinate);

GraphicsObject.drawString() :
The drawSring() method is used in this program to draw string . Here is the syntax :

GraphicsObject.drawString(String str, int x_coordinate, int y_coordinate);

GraphicsObject.drawOval() :
The drawOval() method is used to draws the circle. Here is the syntax :

GraphicsObject.drawOval(int x_coordinate, int y_coordinate, int width, int height);

GraphicsObject.drawRect() :
The drawRect() method is used to draws the rectangle. Here is the syntax :

GraphicsObject.drawRect(int x_coordinate, int y_coordinate, int Wdth, int height);

Here is the shapeDrawingApplet program java code :.

[Read more…] about Java Applet – How to Shapes Drawn in applet.

How to Show Status Message in java Applet Example?

By Dinesh Thakur

In this showStatusMessage example You can see an message is shown on the Status bar of  applet.

 /* Show status message in an Applet window code using void showStatus(String Message) method of an applet class. */

[Read more…] about How to Show Status Message in java Applet Example?

How to run the applet from command line?

By Dinesh Thakur

In this CommandLineApplet example You can see an applet program run from command Line without using any web browser. [Read more…] about How to run the applet from command line?

Java Applet Hello World Examples

By Dinesh Thakur

An applet is a class file that is specially written to display graphics in a web browser. Applets are embedded in web pages using the HTML tag <Applet>. Java applets are downloaded automatically and run by the web browser. Any web-browser that supports Java, such as Applet Viewer, can be used to transport applets to the Internet. An applet can perform many functions such as graphics, play sound, animation, arithmetic operations, play games etc. [Read more…] about Java Applet Hello World Examples

Difference Between JRE ,JDK and JVM

By Dinesh Thakur

Java Virtual Machine (JVM)

[Read more…] about Difference Between JRE ,JDK and JVM

Write a java program changing the case of a string(Lower case)

By Dinesh Thakur

 

 

import java. util. *;
class InputDemo
{
public static void main(String args[])
{
String l;
Scanner s=new Scanner(System.in);
System.out.println("Enter the string");
l=s.next( );
String res=l.toLowerCase( );
System. out. println( res);
}}

Write a program to print the name and roll no of the student using static members

By Dinesh Thakur

 

 

import java.lang. *;
class InputDemo
{
static int rollno=1553;
static String name ="Xavier";
static void display( )
{
System.out.println("Rollno: " + rollno);
System.out.println("Name : "+ name);
}
public static void main(String args[] )
{
display ( );
}
}
« Previous Page
Next Page »

Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW