Posted  by 

Program To Draw Shapes In Java

Program To Draw Shapes In Java 6,0/10 5462reviews

Learn How To Draw A Circle in Java Programming using AWT, Graphics and JFrame. Java Program To Make A Colorful Circle or Oval in AWT. Shapes and fills. In this part of the Java 2D tutorial. Antonio Zoli Shotgun Serial Number on this page. In this example, we draw six basic shapes on the panel: a square, a rectangle, a rounded rectangle.

How To Draw Shapes In Java

Just a few thoughts. You are using x and y coordinates. Why not take advantage of the? The has methods that return Point objects, and it would probably make your code more readable. X1 and y1 don't mean anything to me, but you could probably give meaningful names to the points represented by (x1, y1) and (x2, y2). The use of magic numbers (you storing 2 and 3 into SWITCH, for example) is a poor practice.

Use constant ints or (as Landei suggested in the comments) an enum. It will allow you to improve the readable and understandability of the code - at a first read-through, I didn't get what you were doing. As per Java standards, a class name should begin with a capital letter. Your class is currently called r. You should give it a meaningful name. You might also want to look into other Java standards for code formatting in terms of brace placement, indentation, and so forth.

To me, your code is just plain hard to read because of formatting. Rather than if and switch/ case statements, consider leveraging polymorphism in order to draw different shapes. Move the logic to draw different shapes into concrete subclasses, while perhaps maintaining common logic in a superclass.