Question

draw this shape by Java language

draw this shape by Java language

image.png
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Assuming this is based on Java swing (2D graphics). Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

// DrawShape.java

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.GradientPaint;

import java.awt.Graphics;

import java.awt.Graphics2D;

import javax.swing.JFrame;

public class DrawShape extends JFrame {

      // constructor to initialize the GUI

      public DrawShape() {

            // setting up and displaying the window

            setDefaultCloseOperation(EXIT_ON_CLOSE);

            setSize(500, 500);

            setVisible(true);

      }

      @Override

      public void paint(Graphics g) {

            super.paint(g);

            // creating a Graphics2D object from g

            Graphics2D g2d = (Graphics2D) g;

            // creating a gradient color which is a mix of yellow and blue ish color

            GradientPaint fill = new GradientPaint(0, 100,

                        new Color(255, 255, 100), 0, 500, new Color(135, 206, 235));

            // using this color as fill color

            g2d.setPaint(fill);

            // filling a rounded rectangle at 100,100 with width=300, height=300,

            // arc width=80 and arc height=100

            g2d.fillRoundRect(100, 100, 300, 300, 80, 100);

            // using a stroke width of 5

            g2d.setStroke(new BasicStroke(5));

            // using some brownish color as stroke color

            g2d.setColor(new Color(210, 105, 30));

            // drawing the rounded rect with same coordinates to draw the outline

            // only

            g2d.drawRoundRect(100, 100, 300, 300, 80, 100);

            // using white as fill color

            g2d.setPaint(Color.WHITE);

            // filling inner rounded rectangle at the top

            g2d.fillRoundRect(180, 150, 140, 70, 80, 100);

            // filling inner rounded rectangle at the bottom

            g2d.fillRoundRect(180, 275, 140, 70, 80, 100);

            // using black stroke color, drawing an outline for these rounded

            // rectangles

            g2d.setColor(Color.BLACK);

            g2d.drawRoundRect(180, 150, 140, 70, 80, 100);

            g2d.drawRoundRect(180, 275, 140, 70, 80, 100);

      }

      public static void main(String[] args) {

            // initializing the GUI

            new DrawShape();

      }

}

/*OUTPUT*/


UO X 0 -

Add a comment
Know the answer?
Add Answer to:
draw this shape by Java language
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT