Share & learn

Through Innovative Digital Library

How to draw semi circle in Java?

Write a piece of code in Java to draw a semi circle.
 



Answers

Posted by Paul on April 24th 2018 13:24 PM

Program to draw a semi circle in Java : Java provides a set of classes under java.awt.* libraries for drawing shapes. java.awt.geom library which is most important library using which normal geometric shapes like rectangle, line, circle can be drawn. Here is the code to draw semi circle in Java :

// Java program to draw semi circle

import javax.swing.JComponent;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Arc2D;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;

public class 
DrawSemiCircle extends JFrame {

    public static 
void main(String[] args) {
        new 
DrawSemiCircle();
    }

    
// method to draw semi circle
    
public DrawSemiCircle() {
        
        
this.setSize(300300);
        
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
this.add(new PaintSurface(), BorderLayout.CENTER);
        
this.setVisible(true);
    
    }

    private class 
PaintSurface extends JComponent {

        public 
void paint(Graphics g) {

            
// create object of shape and provide parameters
            
Shape shape = new Arc2D.Float(60901801800180Arc2D.CHORD);
            
Graphics2D graphics = (Graphics2Dg;
            
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            
RenderingHints.VALUE_ANTIALIAS_ON);
            
            
// set background color
            
graphics.setPaint(Color.LIGHT_GRAY);
            
            
// set border color
            
graphics.setColor(Color.BLACK);
            
graphics.setStroke(new BasicStroke(2));
            
            
// draw shape
            
graphics.draw(shape);
            
        }
    }
    
}
Answer Question

Your email address will not be published. Required fields are marked *

  • Minimum 20 character
CCKI

related questions

Most liked questions

Most answered questions

Login
CCKI Refresh
Register
CCKI Refresh
Forgot password
Ask a Question
Minimum 20 character
Minimum 20 character
Reply
Minimum 20 character
CCKI Refresh