130k views
5 votes
Write a method that draws a circle and a square that is centered in a DrawingPanel. The method will take in the width and height (they are the same for both the circle and square) and a Graphics object as parameters

IN java
current code: turn it into a method along with the main method
import java.awt.*;
public class Graphics {
static final int WIDTH = 300;
static final int HEIGHT = 200;
public static void drawGraphics () {
}
public static void main(String []args) {
DrawingPanel2 draw = new DrawingPanel2(WIDTH,HEIGHT); // Make a DrawginPanel2 of size 300 by 200.
Graphics2D g = draw.getGraphics(); // gets graphics from DrawingPanel so you can draw
g.setColor(Color.RED);
g.fillRect(45, 30, 200, 150);
g.setColor(Color.BLUE);
g.fillOval(70, 30, 150, 150);
}
}

User Teebes
by
5.2k points

1 Answer

2 votes

Answer:

public static void drawGraphics (Graphics g, int width, int height) {

int r = Math.round(width/2);

int x = 45;

int y = 30;

g.setColor(Color.RED);

g.fillRect(x, y, width, height);

g.setColor(Color.BLUE);

g.fillOval(Math.round(x/2), Math.round(y/2), r, r);

}

Step-by-step explanation:

The Java method "drawGraphics" of the Graphics class accepts draws a square with the "fillRect()" method of the Graphics class object and at its center, a circular path is drawn as well.

User Espenhogbakk
by
4.7k points