Create a java program in bluej called RandomBox with the following features: • W
ID: 3813468 • Letter: C
Question
Create a java program in bluej called RandomBox with the following features:
• When the program begins it creates a filled rectangle of random size (make your limits 20 - 120 pixels) centered on the screen
• Hint: center is
o canvasWidth – boxWidth/2
o canvasHeight – boxHeight/2
• When the mouse exits the window the box disappears
• When the mouse re-enters the window, it generates a new size for the box and re-centers it on the screen
• If time, make the color of the box random. Change each time a new box is drawn.
and also Write a program called RandomBox2.
• Same as RandomBox except for box position
• Position box some value D within 150 px of center (changing both X and Y axis by the D value
Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.applet.*;
class Rectangle extends Applet
implements MouseListener,MouseMotionListener {
private JFrame frame;
private CanvasPane canvas;
private Graphics2D graphic;
private Color backgroundColour;
private Image canvasImage;
public Rectangle(String title, int width, int height, Color bgColour){
this.addMouseListener(this);
this.addMouseMotionListener(this);
frame = new JFrame();
canvas = new CanvasPane();
frame.setContentPane(canvas);
frame.setTitle(title);
canvas.setPreferredSize(new Dimension(width, height));
backgroundColour = bgColour;
frame.pack();
}
public void setVisible()
{
Dimension size = canvas.getSize();
canvasImage = canvas.createImage(size.width, size.height);
graphic = (Graphics2D)canvasImage.getGraphics();
graphic.setColor(backgroundColour);
Random randomGenerator = new Random();
int x = randomGenerator.nextInt(100) + 20;
int y = randomGenerator.nextInt(100) + 20;
int startX, startY;
startX = 250 - (x/2);
startY = 250 - (y/2);
graphic.fillRect(startX, startY, x, y);
graphic.setColor(Color.black);
frame.show();
}
public void setInvisible()
{
Dimension size = canvas.getSize();
canvasImage = canvas.createImage(size.width, size.height);
graphic = (Graphics2D)canvasImage.getGraphics();
graphic.setColor(backgroundColour);
graphic.fillRect(0, 0, size.width, size.height);
graphic.setColor(backgroundColour);
frame.show();
}
public void mouseEntered(MouseEvent m)
{
setVisible();
repaint();
}
public void mouseExited(MouseEvent m)
{
setInvisible();
repaint();
}
public void mousePressed(MouseEvent m)
{
}
public void mouseReleased(MouseEvent m)
{
}
public void mouseMoved(MouseEvent m)
{
}
public void mouseDragged(MouseEvent m)
{
}
public void mouseClicked(MouseEvent m)
{
}
public static void main(String[] args){
Rectangle rect = new Rectangle("Canvas", 500, 500, Color.white);
rect.setVisible();
}
private class CanvasPane extends JPanel
{
public void paint(Graphics g)
{
g.drawImage(canvasImage, 0, 0, null);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.