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




48 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. Good information Thank you..

    ELearn Infotech offers Java Training in Hyderabad by Industry Experts. Our realtime Java Course includes from Basic to Advanced Level concepts. We have designed our Java course content based on students Requirement to Achieve their Goal. We offer both Java classroom training in Hyderabad and Java online training with real-time projects. We are one of the leading Java Training Institutes in Hyderabad.

    ReplyDelete
  31. 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
  32. 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
  33. 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
  34. 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
  35. I'm impressed by the depth of information provided. Here to start AWS  learning journey.

    ReplyDelete
  36. 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
  37. 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
  38. NS3Edu offers specialized CCIE course training tailored for networking professionals and individuals aspiring to establish a career in networking technology, right here in Gurgaon. With our unwavering commitment to excellence, we guarantee your success in achieving CCIE certification. Join us at NS3Edu to elevate your networking career to new heights with our CCIE certification in Gurgaon.

    ReplyDelete