banner



How To Draw Shapes In Java

How to draw various shapes in Java Swing

In this programme, we will learn how to draw diverse types of shapes in Java swing using AWT. Coffee AWT package provides many methods of displaying the graphics. They are a few methods :

  • for string-: public abstract void drawString(Cord str, int x, int y): with this method, we can draw a specified string.
  • oval-: public abstruse void drawOval(int ten, int y, int width, int height): with this method, we can draw an oval with specified dimensions.
  • Make full oval-: public abstract void fillOval(int x, int y1, int x2, int y2): with this method, we can draw an oval with specified dimensions and color.
  • Line-: public abstract void drawLine(int x1, inty1, int x2, int y2): with this method, we can draw a line with specified dimensions.
  • Image-: public abstract void drawImage(Image img, int x. int y, ImageObserver observer): with this method, we tin draw an image.
  • Draw Arc-: public abstract void drawArc(int x, int y, int width, int height, int startAngle int arcAngle): with this method, we can draw circular things similar a circumvolve, eclipse, etc.
  • Fill up Arc-:
  • public abstract void fillArc(int x, int y, int width, int height, int startAngle): with this method, nosotros can draw eclipse, a circle with the specified colour.

Java program to draw shapes in Swing AWT

import coffee.awt.*;   import javax.swing.JFrame;      public grade DisplayGraphics extends Canvas{              public void pigment(Graphics yard) {           g.drawString("Hello",forty,40);           setBackground(Color.WHITE);           g.fillRect(130, 30,100, eighty);           m.drawOval(thirty,130,l, 60);           setForeground(Color.RED);           g.fillOval(130,130,fifty, 60);           g.drawArc(30, 200, 40,50,90,threescore);           one thousand.fillArc(thirty, 130, twoscore,l,180,xl);                  }           public static void main(String[] args) {           DisplayGraphics grand=new DisplayGraphics();           JFrame f=new JFrame();           f.add(m);           f.setSize(400,400);           //f.setLayout(null);           f.setVisible(true);       }      }        

The output of the Java program

draw shapes in java swing

output: java program to depict shapes using swing AWT

You may as well read these:

  • How to Create a digital watch using Java
  • How to draw a waveform pattern in java swing

Source: https://www.codespeedy.com/draw-shapes-in-java-swing/

Posted by: jamesinaboust.blogspot.com

0 Response to "How To Draw Shapes In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel