Question: The following code creates a Rectangle in Java that is scaled to windowsize when you run it. Add a button to this code (a button

The following code creates a Rectangle in Java that is scaled to windowsize when you run it. Add a button to this code (a button with no functionality) that can only be seen when the mouse HOVERS over the rectangle.

import java.awt.*;

import java.awt.event.*;

public class Rectangle extends Frame{ public static void main(String[] args) {

// TODO Auto-generated method stub

new Rectangle(); }

Rectangle() {

super("Rectangle");

addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} });

setSize(600, 600);

Shapes s= new Shapes();

add("Center",s); setVisible(true); } }

class Shapes extends Canvas {

int centerX, centerY, maxX, maxY, xP, yP;

float pixelSize, rWidth = 10.0F, rHeight = 10.0F;

void initgr() {

Dimension d = getSize();

maxX = d.width - 1; maxY = d.height - 1;

pixelSize = Math.max(rWidth / maxX, rHeight / maxY);

centerX = maxX / 2; centerY = maxY / 2; }

int iX(float x) {return Math.round( x / pixelSize);}

int iY(float y) {return Math.round(y / pixelSize);}

public void paint(Graphics g) {

initgr();

g.setColor(Color.BLACK);

float w= (rWidth/4) ;

float h= w*2;

//MAIN RECTANGLE

g.drawRect(centerX/2, centerY/2, iX(w), iY(h));

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!