Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that displays a circle with radius 10 pixels. You can point the

ID: 663867 • Letter: W

Question

Write a program that displays a circle with radius 10 pixels. You can point the mouse inside the circle and drag (i.e., move with mouse pressed) the circle wherever the mouse goes, as shown below:

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.*;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Exercise16 extends JFrame{

public static void main(String[] args) {

Exercise16 frame = new Exercise16();

frame.setLocationRelativeTo(null); //center frame

frame.setTitle("Exercise16");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(300,250);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

Exercise16(){

add(new RandomCircle());

}

class RandomCircle extends JPanel

{

private int x =20;

private int y =20;

public int RADIUS = 10;

private long count =0L;

private long startTime =System.currentTimeMillis();

public RandomCircle()

{

addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e){

if (insideCircle(x,y,e.getX(),e.getY()))

repaint();

}

});

}

public boolean insideCircle(double x1, double y1, double x2, double y2) {

return Math.sqrt((x2-x2)*(x2-x1)+(y2-y1)*(y2-y1))<RADIUS;

}

public void paintComponent(Graphics g)

{

super.PaintComponent(g);

if (count>20L){

long endTime- System.currentTimeMillis();

g.drawString("Time spent:"+(endTime -startTime)+"milliseconds",20,20);

}else{

count++;

x-(int)(getWidth()*Math.random());

y-(int)(getHeight()*Math.random());

g.setColor(new Color((int)(Math.random()*255.0D)));

g.fillOval(x -RADIUS, y - RADIUS,2 * RADIUS, 2 * RADIUS);

}}}

}

// i'm doing something wrong

Exercise16 26 Exercise16 26 Exercise16 27 Exercise16 27 Time spent 51515 milliseconds

Explanation / Answer

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.*;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Exercise16 extends JFrame{

public static void main(String[] args) {

Exercise16 frame = new Exercise16();

frame.setLocationRelativeTo(null); //center frame

frame.setTitle("Exercise16");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(300,250);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

Exercise16(){

add(new RandomCircle());

}

class RandomCircle extends JPanel

{

private int x =20;

private int y =20;

public int RADIUS = 10;

private long count =0L;

private long startTime =System.currentTimeMillis();

public RandomCircle()

{

addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e){

if (insideCircle(x,y,e.getX(),e.getY()))

repaint();

}

});

}

public boolean insideCircle(double x1, double y1, double x2, double y2) {

return Math.sqrt((x2-x2)*(x2-x1)+(y2-y1)*(y2-y1))<RADIUS;

}

public void paintComponent(Graphics g)

{

super.PaintComponent(g);

if (count>20L){

long endTime= System.currentTimeMillis();

g.drawString("Time spent:"+(endTime -startTime)+"milliseconds",20,20);

}else{

count++;

x=(int)(getWidth()*Math.random());

y=(int)(getHeight()*Math.random());

g.setColor(new Color((int)(Math.random()*255.0D)));

g.fillOval(x -RADIUS, y - RADIUS,2 * RADIUS, 2 * RADIUS);

}}}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote