Java Graphics Programming

Home   »    Java   »    Java Graphics Programming


   Graphics is one of the most important features of Java. Java applets can be written to draw lines, arcs, figures, images and text in different fonts and styles. Different colors can also be incorporated in display.


Contents




The Graphics Class

The graphics class defines a number of drawing functions, Each shape can be drawn edge-only or filled. To draw shapes on the screen, we may call one of the methods available in the graphics class. The most commonly used drawing methods included in the graphics class are listed below. To draw a shape, we only need to use the appropriate method with the required arguments.

Drawing Methods of the Graphics Class

Sr.NoMethodDescription
1. clearRect() Erase a rectangular area of the canvas.
2. copyAre() Copies a rectangular area of the canvas to another area
3. drawArc() Draws a hollow arc
4. drawLine() Draws a straight line
5 drawOval() Draws a hollow oval
6 drawPolygon() Draws a hollow polygon
7 drawRect() Draws a hollow rectangle
8. drawRoundRect()Draws a hollow rectangle with rounded corners
9. drawString() Display a text string
10. FillArc() Draws a filled arc
11. fillOval() Draws a filled Oval
12. fillPolygon() Draws a filled Polygon
13. fillRect() Draws a filled rectangle
14. fillRoundRect() Draws a filled rectangle with rounded corners
15. getColor() Retrieves the current drawing color
16. getFont() Retrieves the currently used font
17. getFontMetrics() Retrieves the information about the current font
18. setColor() Sets the drawing color
19. setFont() Sets the font




Lines and Rectangles

Lines are drawn by means of the drawLine() method.

Syntax
void drawLine(int startX, int startY, int endX, int endY)

drawLine() displays a line in the current drawing color that begins at (start X, start Y) and ends at (endX, end Y).


After this you can comiple your java applet program as shown below:

"Output of Lines.class"


Download Complete Program

  -  




Rectangle

The drawRect() and fillRect() methods display an outlined and filled rectangle, respectively.

Syntax
void drawRect(int top, int left, int width, int height)
void fillRect(int top, int left, int width, int height)

The upper-left corner of the rectangle is at(top,left). The dimensions of the rectangle are specified by width and height.

Use drawRoundRect() or fillRoundRect() to draw a rounded rectangle. A rounded rectangle has rounded corners.

Syntax
void drawRoundRect(int top, int left, int width, int height int Xdiam, int YDiam)
void fillRoundRect(int top, int left, int width, int height int Xdiam, int YDiam)

The upper-left corner of the rounded rectangle is at (top,left). The dimensions of the rectangle are specified by width and height. The diameter of the ribdubg are along the X axis are specified by x Diam. The diameter of the rounding are along the Y axis is specified by Y Diam.



After this you can comiple your java applet program as shown below:


c:\jdk1.4\bin\javac Rectanlge.java
c:\jdk1.4\bin\appletviewer Rectanlge.java


"Output of Rectangle.class"




Download Complete Program

  -  




Circles and Ellipses

The Graphics class does not contain any method for circles or ellipses. To draw an ellipse, use drawOval(). To fill an ellipse, use fillOval().

Syntax
void drawOval(int top, int left, int width, int height)
void fillOval(int top, int left, int width, int height)

The ellipse is drawn within a bounding rectangle whose upper-left corner is specified by (top,left) and whose width and height are specified by width and height. To draw a circle, specify a square as the bounding rectangle i.e get height = width.

The following program draws serveral ellipses:


After this you can comiple your java applet program as shown below:


c:\jdk1.4\bin\javac Ellipses.java
c:\jdk1.4\bin\appletviewer Ellipses.java


"Output of Ellipses.class"




Download Complete Program

  -  



Drawing Arcs

An arc is a part of oval. Arcs can be drawn with draw Arc() and fillArc() methods.

Syntax
void drawArc(int top, int left, int width, int height, int startAngle, int sweetAngle)
void fillArc(int top, int left, int width, int height, int startAngle, int sweetAngle)

The arc is bounded by the rectangle whose upper-left corner is specified by (top,left) and whose width and height are specified by width and height. The arc is drawn from startAngle through the angular distance specified by sweepAngle. Angles are specified in degree. '0o' is on the horzontal, at the 30' clock's position. The arc is drawn conterclockwise if sweepAngle is positive, and clockwise if sweetAngle is negative.

The following applet draws several arcs:

After this you can comiple your java applet program as shown below:


c:\jdk1.4\bin\javac Arcs.java
c:\jdk1.4\bin\appletviewer Arcs.java


"Output of Arcs.class"


Download Complete Program

  -  




Drawing Polygons

Polygons are shapes with many sides. It may be considered a set of lines connected together. The end of the first line is the beginning of the second line, the end of the second line is the beginning of the third line, and so on. Use drawPolygon() and fillPolygon() to draw arbitrarily shaped figures.

Syntax
void drawPolygon(int ,[] , int y[], int numPointer)
void fillPolygon(int ,[] , int y[], int numPointer)

The polygon's endpoints are specified by the coordinate pairs contained within the x and y arrays. The number of points defined by x and y is specified by numPoints.

It is obvious that x and y arrays should be of the same size and we must repeat the first point at the end of the array for closing the polygon.

The following appplet draws an hourglass shape:

After this you can comiple your java applet program as shown below:


c:\jdk1.4\bin\javac Polygon.java
c:\jdk1.4\bin\appletviewer Polygon.java


"Output of Polygon.class"




Download Complete Program

  -  






<< Previous Topic
Next Topic >>




75 comments:

  1. This information is really useful to me.Thanks for sharing.
    Java Training in OMR

    ReplyDelete


  2. Your blog was good and informative. Thanks for sharing your thoughts, keep posting!.
    Get Best web development Services

    ReplyDelete
  3. Best article, very useful and explanation. Your post is extremely incredible. Thank you very much.
    Visit us: Java Online Training Hyderabad
    Visit us: Core Java Online Course

    ReplyDelete
  4. Hi, Thank you for this informative blog, I have just started to learn java course and this blog is really informative for me. Thank you for this blog!

    ReplyDelete
  5. Hi, I have just started to Learn Java Online. and this blog is really informative for me. Thank you for this blog!

    ReplyDelete
  6. Great Blog thanks for sharing! You can also learn reactjs. Cdn Reactjs is a declarative, efficient, and flexible JavaScript library used by Facebook & instagram for development!

    ReplyDelete
  7. Java course in Delhi
    https://techplanet.today/post/how-to-find-the-right-why-is-java-important-for-your-specific-skills
    APTRON Delhi is an Authorized Java Training Center in Delhi for various Java Course in Delhi. APTRON Delhi is proud to say that we are rated as the best online and classroom training provider in Delhi. Our Java course in Delhi is designed as per the IT industry requirements. APTRON Delhi has a dedicated Placement team to ensure that every student trained gets an aspiring career. Students get unlimited support until they get placed.

    ReplyDelete
  8. Nice Blog. Thanks for sharing this article with us. Keep sharing in future.
    Do you want to be a JAVA Course?

    JAVA Course

    ReplyDelete
  9. I visited your blog and I found it is very useful for Java beginners, I recommend one of the best Java training course in Delhi that would be helpful to become a Java expert.

    ReplyDelete
  10. I have been following your blog since last few days and you have amazing posts. This is a wonderful thesis! The entire content is full of great information which strengthens my knowledge. Java Courses are Best for any career at this time.

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. This is a really good article about Java .I have just started to learn java course in kolkata . because in nowadays an java developer is an excellent career . I found this information to be of great value. This article has increased my knowledge even more.
    Thank you ...

    ReplyDelete
  14. Wow! Thank You for sharing your knowledge with us. This blog provides a proper
    explanation.I have one more information related with credit card generator now a days its very important for every individual.
    creditcardgenerator

    ReplyDelete

  15. Wow, the information you shared is absolutely amazing! Thank you so much for that. Speaking of sharing, I came across a really helpful link about roblox that I just have to pass on to you. Click on the link above and check it out! Keep up the great work on your blog, and keep us in the loop. Cheers! Visit here

    ReplyDelete
  16. Thanks for sharing this Great content.

    Car transport services are necessary for those who need to transport their vehicles from one location to another. These services are particularly useful for individuals or businesses that need to relocate vehicles over long distances. Car Transport in Chandigarh services provide various options, including open and enclosed trailers, door-to-door service, and terminal-to-terminal service. Professional car transport companies have the necessary equipment and expertise to ensure that vehicles are transported safely and efficiently. These services may also include insurance and tracking options to provide additional peace of mind for the customer. By hiring a professional car transport service, individuals can avoid the risks and hassle associated with driving their vehicle long distances and ensure that their vehicle arrives at its destination safely and on time.

    ReplyDelete
  17. I like your blog. Your way of explaining is really good. This blog helpful to those who want to learn java graphics. For further information about Java Design, Java Training Course in Lucknow

    ReplyDelete
  18. wow! it was very useful blog. this blog very informative thank you for sharing this. visit: Java Courses Online

    ReplyDelete
  19. Great blog The content is informative and engaging. The author's writing style is captivating. Visit my website to get best Information About HR Generalist Training in Noida and mention Below Technologies.

    HR Generalist Training in Noida

    ReplyDelete
  20. Thanks for sharing such informative guide. This post gives me detailed information.
    https://www.uncodemy.com/course/data-analytics-training-course-in-kolkata/

    ReplyDelete
  21. Great blog The content is informative and engaging. The author's writing style is captivating. Visit my website to get best Information About Salesforce Marketing Cloud Training in Noida and mention Below Technologies.

    Salesforce Marketing Cloud Training in Noida

    ReplyDelete
  22. I like your blog! This article provides a comprehensive overview of the Graphics Class in Java, explaining various drawing methods and their syntax.
    Java has endured over time

    ReplyDelete
  23. This comment has been removed by the author.

    ReplyDelete
  24. Best article, very useful and explanation. Your post is extremely incredible. Thank you very much.
    Visit us: Online Digital Marketing Course in South delhi

    ReplyDelete
  25. Thank you for sharing this. I always appreciate coming across such exceptional content filled with valuable insights. The ideas presented here are outstanding and quite innovative, making the post a truly enjoyable read. Keep up the fantastic work.
    visit: Data Science Tools and Technologies: A Comprehensive Overview

    ReplyDelete
  26. Cannabis Test Canada - refers to any testing options used to test tetrahydrocannabinol (THC). These methods could be used to test in the following areas: Medicine, Sport, and Law.

    ReplyDelete
  27. We sell any brand marked cards! Do not be afraid to ask if we sell a certain brand. Visit here: Fair trade koffiebonen

    ReplyDelete
  28. Great Post this help me a lot in my college project java Programming is best for backened language. If you know more about It course it include a Full stack development course in pune to enhance your carrer.

    ReplyDelete
  29. Thanks a bunch for sharing your content! I always love diving into exceptional material that provides valuable insights. The concepts you present are truly extraordinary and incredibly engaging, making your post a genuine source of delight. Keep up the fantastic work
    Visit- Java Programming for Beginners: Your Step-by-Step Course

    ReplyDelete
  30. Are you struggling to get your business noticed on Google Maps? Our Google My Business Optimization Service In Delhi can help! We'll optimize your GMB listing with relevant keywords, photos, and reviews to improve your local SEO ranking. Reach more customers and grow your business with our expert GMB optimization service. Contact us today for a free consultation!

    ReplyDelete
  31. Godrej Sector 49 Gurgaon is an upscale residential project developed by Godrej Properties, located in Sector 49 of Gurgaon, Haryana, India. Offering luxurious amenities, well-designed living spaces, and lush green surroundings, it aims to provide residents with a sophisticated and comfortable lifestyle in a prime location.

    ReplyDelete
  32. M3M Crown Sector 111 Gurgaon, offers modern living with luxury amenities. Its strategic location provides easy access to major hubs and amenities. With meticulously designed spaces and premium facilities, it promises a lifestyle of comfort and convenience, making it an ideal choice for discerning homebuyers.

    ReplyDelete
  33. Godrej Sector 89 Gurgaon offers a lifestyle of luxury, convenience, and tranquility amidst the dynamic urban landscape. With its prime location, architectural splendor, luxurious residences, world-class amenities, sustainability initiatives, and focus on safety, it redefines the concept of upscale living and provides residents with an unparalleled living experience.

    ReplyDelete
  34. I'm impressed by the depth of information provided. Here to start AWS  learning journey.

    ReplyDelete
  35. Delhi hosts diverse animation studios offering top-notch 2D and 3D animation services. From Digitoonz Media's global-scale projects to Animatzione Studio's versatile solutions, the city's animation landscape thrives. With Silver Stag Studio's creative flair and Venus VFX's technical prowess, Delhi's animation scene delivers excellence across industries

    ReplyDelete
  36. Godrej Sector 103 gurgaon offers unparalleled luxury living. With meticulously designed residences, world-class amenities, and sustainable practices, it epitomizes modern urban lifestyle. Enjoy seamless connectivity and a serene ambiance, redefining the essence of upscale living in one of India's most vibrant cities.

    ReplyDelete
  37. Whether you’re traveling for business, leisure, or a cultural experience, booking your flight tickets to Ahmedabad is the first step to discovering this fascinating city. In this blog, we’ll guide you through everything you need to know about flights to Ahmedabad and what to expect when you arrive

    ReplyDelete
  38. Bangalore, officially known as Bengaluru, is a bustling metropolis in southern India that blends innovation, nature, and history. Famous for its tech industry and often called the Silicon Valley of India, Bangalore is also home to lush parks, vibrant nightlife, and a thriving food scene. Whether you're traveling for business, leisure, or a bit of both, booking flight tickets to Bangalore is your first step toward exploring this dynamic city.

    ReplyDelete
  39. Kolkata, often referred to as the "City of Joy," is a vibrant metropolis in eastern India known for its rich cultural heritage, colonial architecture, and literary history. As the capital of West Bengal, Kolkata is a major cultural and commercial hub that attracts travelers for a variety of reasons—whether for its historical sites, art galleries, delicious Bengali cuisine, or the famed Durga Puja celebrations. If you're planning to visit this lively city, booking flight tickets to Kolkata is the first step toward experiencing its unique charm.

    ReplyDelete
  40. Mumbai, also known as the “City of Dreams,” is India’s bustling financial capital, home to Bollywood, iconic landmarks, and a rich cultural heritage. Whether you're traveling for business, leisure, or to experience the city's vibrant energy, Mumbai has something for everyone. Booking flight tickets to Mumbai is your gateway to exploring one of India's most exciting destinations.

    ReplyDelete
  41. Goa, India’s smallest state, is known for its stunning beaches, vibrant culture, and rich history. Whether you're drawn to the serene coastline, water sports, lively nightlife, or colonial architecture, Goa offers something for everyone. It is no wonder that it’s a top travel destination not only for Indian tourists but also for international travelers. Booking flight tickets to Goa is a key step in planning your trip, and in this guide, we'll walk you through everything you need to know, from finding the best deals to understanding what to expect when you land in this paradise.

    ReplyDelete
  42. New Delhi, the vibrant capital of India, is a city rich in history, culture, and modern attractions. Whether you are traveling for leisure, business, or family visits, getting affordable and convenient flight tickets to New Delhi is essential. As a major hub in Asia, the city is well-connected to global and domestic destinations, making it easily accessible by air. In this guide, we’ll explore everything you need to know about booking flight tickets to New Delhi, including tips on finding the best deals, major airlines, and when to travel.

    ReplyDelete
  43. Whether you're visiting for its architectural beauty, attending a business meeting, or immersing yourself in the local culture, booking flight tickets to Jaipur is the first step to discovering this colorful city.

    ReplyDelete
  44. Flying from Mumbai to Delhi Flights is efficient, with a variety of options to suit different preferences and budgets. By comparing flights, booking in advance, and exploring flexible options, you can find great deals for a seamless journey. Whether you’re traveling for work or leisure, Delhi awaits with its unique charm and endless experiences.

    ReplyDelete
  45. Traveling from Hyderabad to Delhi Flights by air is one of the most popular routes in India, connecting the southern city of Hyderabad with the nation's capital. With frequent flights, a variety of airlines, and competitive pricing, this route offers convenience and flexibility for both business and leisure travelers.

    ReplyDelete
  46.  flights from Mumbai to Ayodhya flights are limited, but travelers can still make the journey through connecting flights or alternative travel options. With Ayodhya’s growing prominence as a religious and historical destination, there may be more options for direct flights in the future. Until then, here’s an overview of the best options for reaching Ayodhya from Mumbai.

    ReplyDelete
  47. Ayodhya, the ancient city located on the banks of the River Saryu in Uttar Pradesh, holds a profound place in India’s spiritual and historical fabric. Known for being the birthplace of Lord Rama, Ayodhya attracts millions of pilgrims and history enthusiasts each year. If you’re planning a trip from Chennai — the cultural and economic hub of South India — to Ayodhya, flying is the quickest and most convenient way to begin your pilgrimage or exploration of this sacred city. This blog will guide you through everything you need to know about traveling from Chennai to Ayodhya Flight.

    ReplyDelete
  48. Last-minute flights are often necessary for unexpected travel, but they don’t always have to be expensive. Flexibility with travel dates and times is key to finding affordable last-minute options.

    ReplyDelete
  49. Mumbai to Goa Flight ensures you spend less time traveling and more time relaxing.

    ReplyDelete
  50. The Leh to Delhi flight is the perfect culmination of your Himalayan adventure. With breathtaking aerial views, quick travel time, and excellent connectivity upon arrival, it ensures that your journey from the high-altitude serenity of Ladakh to the dynamic vibrance of Delhi is smooth and memorable.

    ReplyDelete
  51. Ahmedabad to Delhi flight offers a fast, comfortable, and hassle-free option

    ReplyDelete
  52. The Goa to Delhi flight is the perfect way to travel from India’s tropical paradise to the vibrant capital city.

    ReplyDelete