Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Displaying Graphics in Applet

  • We can use java.awt.Graphics class to draw shapes, lines, and text onto Java applets in order to display graphics.
    Here's a more detailed explanation of the steps involved
  • Creating a Graphics object : A Graphics object must first be created before you can draw graphics on an applet. When the applet needs to be redrawn, the paint() method, which is called, typically performs this. The getGraphics() method is used to create the Graphics object and is invoked on the applet itself. An example of how to create a graphics object is provided below.

    public void paint(Graphics g) {
        Graphics graphics = getGraphics();
    }
    





  • Drawing shapes : Once you have a Graphics object, you can draw shapes on the applet using its methods. Draw a circle using the fillOval() method and a rectangle using the drawRect() method, for instance. Here is an example of how to draw a circle and a rectangle.

    public void paint(Graphics g) {
        Graphics graphics = getGraphics();
        graphics.drawRect(10, 10, 50, 50);
        graphics.fillOval(70, 10, 50, 50);
    }
    

    In this example, The drawRect() method is used in this example to draw a rectangle with the top-left corner at (10, 10) and width and height set to 50. The fillOval() method is used to draw a circle with a diameter of 50 and a centre at (70, 35).






  • Drawing lines : You can draw lines in the Graphics class in addition to shapes. To draw a line connecting two points, use the drawLine() method. Here is a sample of a line that was drawn.
    public void paint(Graphics g) {
        Graphics graphics = getGraphics();
        graphics.drawLine(10, 70, 110, 70);
    }
    

    In this example, the drawLine() method is used to draw a line between the points (10, 70) and (110, 70).



  • Drawing text : Finally, you can draw text on the applet using the Graphics class. To draw a string of text at a specific location, use the drawString() method. This is an illustration of how to draw text.
    public void paint(Graphics g) {
        Graphics graphics = getGraphics();
        graphics.drawString("Hello, world!", 10, 100);
    }
    

    In this example, the drawString() method is used to draw the string "Hello, world!" at the point (10, 100).





    It's important to remember that the whenever paint() method is called the applet needs to be redrawn, which can be quite frequently. As a result, it's crucial to ensure that your paint() method is effective and doesn't perform any extraneous tasks. For instance, instead of drawing a static image repeatedly as the applet is painted, you should think about drawing it once and saving it in an Image object.



Applet Parameter Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet

Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.