NEED help with part B only. Draw a randombox with the following features. a) *Wh
ID: 3619242 • Letter: N
Question
NEED help with part B only.Draw a randombox with the following features.
a) *When the program begins, it creates a filled rectangle ofrandom size (make your limits 20-120 pixels) centered on thescreen
*When the mouse is clicked, the console reports the dimensions andlocation of the box
*When the mouse exits the window, the box disappears
*When the mouse re-enters the window, it generates a new size forthe box and recenters it on the screen
b) Modify the above code so that it generates a random color inaddition to the random size for the box. Add code so that the red,green, and blue color components are reported in the console aswell. Make sure your box has a frame so that it can be seen even ifit is given a very light color.
NEED help with part B only. NEED help with part B only.
a) *When the program begins, it creates a filled rectangle ofrandom size (make your limits 20-120 pixels) centered on thescreen *When the mouse is clicked, the console reports the dimensions andlocation of the box *When the mouse exits the window, the box disappears *When the mouse re-enters the window, it generates a new size forthe box and recenters it on the screen
b) Modify the above code so that it generates a random color inaddition to the random size for the box. Add code so that the red,green, and blue color components are reported in the console aswell. Make sure your box has a frame so that it can be seen even ifit is given a very light color.
NEED help with part B only.
Explanation / Answer
please rate - thanks import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.Random; public class mouseexp extends Applet implements MouseListener {Random generator = new Random(); private int width, height,x,y,size=500,n=1,r,g,b; public void init() { setSize(size, size); addMouseListener(this); n=1; } public void mouseEntered(MouseEvent e) {n=1; repaint(); } public void mouseClicked(MouseEvent e) {System.out.println("Box Location: x="+x+", y= "+y); System.out.println("Box size: width= "+width+", height= "+height); System.out.println("Window size: "+size+" square"); System.out.println("Box color: red="+r+", green= "+g+", blue= "+b); System.out.println(); n=2; } public void mouseExited(MouseEvent e) {n=3; repaint(); } public void mousePressed(MouseEvent e){} public void mouseMoved(MouseEvent e){} public void mouseDragged(MouseEvent e){} public void mouseReleased(MouseEvent e){ repaint();} public void paint(Graphics p) {if(n==1) { width=generator.nextInt(100)+20; height=generator.nextInt(100)+20; x=size/2-(height/2); y=size/2-(width/2); r=generator.nextInt(256); g=generator.nextInt(256); b=generator.nextInt(256); } if(n!=3) {Color myColor = new Color (r,g,b); p.setColor (myColor); p.fillRect(x, y, width, height); p.setColor(Color.black); p.drawRect(x, y, width, height); } } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.