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

Make a simple game where a shape moves around the screen every x seconds (use a

ID: 3838262 • Letter: M

Question

Make a simple game where a shape moves around the screen every x seconds (use a Timer, try a few different numbers for x to see where it is challenging) and if the person can click the mouse in the shape before it moves again, they get a point. After 10 seconds, show them their score. Call the files ClickBaitRunner and ClickBaitComponent

Consider:
Don't let the rectangle go off the screen.
Check to see if they clicked the Rectangle, not the ClickBaitComponent.
Both the size of the box and the speed of the Timer will determine difficulty.

Explanation / Answer

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cheggindia;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;

public class ScreenSaver extends JPanel implements ActionListener {

private JFrame frame = new JFrame("FullSize");
private final int DELAY = 9999999;
private Timer timer;
private Rectangle rectangle;
boolean full;
private Color color = Color.BLACK;
int i = 0;

// constructor sets window's title bar string and dimensions
public ScreenSaver() {

frame.setUndecorated(true);
// Add a Key Listener to the frame
frame.addKeyListener(new KeyHandler());
// Add this panel object to the frame
frame.add(this);
// Get the dimensions of the screen
rectangle = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().getDefaultConfiguration().getBounds();
// Set the size of the frame to the size of the screen

frame.setSize(rectangle.width, rectangle.height);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(color);

// Remember that we are currently at full size
full = true;

timer = new Timer(2, this); // create a new timer
timer.start();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
System.exit(0);
}
});
}

class KeyHandler extends KeyAdapter {

public void keyPressed(KeyEvent e) {
System.exit(0);
}
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

int shape;

for (int i = 0; i < 25; i++) {

shape = (int) (Math.random() * 4); // pick a random shape

switch (shape) {
case 0:
//makeLine(g);
break;
case 2:
makeOval(g);
break;
case 3:
//makeRoundRect(g);
break;
}

for (int q = 1; q < DELAY; q++) ;
}

}

public static void main(String args[]) {
ScreenSaver run = new ScreenSaver();

}

public void actionPerformed(ActionEvent actionEvent) {
repaint();
}

private void makeLine(Graphics g) {
Graphics2D g2d = (Graphics2D) g;

double x = Math.random() * 1600;
double y = Math.random() * 300;
double x1 = Math.random() * 800;
double y1 = Math.random() * 1500;

g2d.setPaint(new GradientPaint((int) x, (int) y,
new Color((float) Math.random(), (float) Math.random(),
(float) Math.random()), (int) x1, (int) y1,
new Color((float) Math.random(), (float) Math.random(),
(float) Math.random()), true));

g2d.draw(new Line2D.Double(x, y, x1, y1));
}

private void makeOval(Graphics g) {

Graphics2D g2d = (Graphics2D) g;

double x = Math.random() * 800;
double y = Math.random() * 1400;
double width = Math.random() * 300;
double height = Math.random() * 300;

g2d.setPaint(new GradientPaint((int) x, (int) y,
new Color((float) Math.random(), (float) Math.random(),
(float) Math.random()), (int) (x + width), (int) (y + height),
new Color((float) Math.random(), (float) Math.random(),
(float) Math.random()), true));

g2d.draw(new Ellipse2D.Double(x, y, width, height));

}

private void makeRoundRect(Graphics g) {

Graphics2D g2d = (Graphics2D) g;

double x = Math.random() * 1400;
double y = Math.random() * 800;
double width = Math.random() * 200;
double height = Math.random() * 200;
double arcWidth = Math.random() * width;
double arcHeight = Math.random() * height;

g2d.setPaint(new GradientPaint((int) x, (int) y,
new Color((float) Math.random(), (float) Math.random(),
(float) Math.random()), (int) (x + width), (int) (y + height),
new Color((float) Math.random(), (float) Math.random(),
(float) Math.random()), true));

g2d.draw(new RoundRectangle2D.Double(x, y, width, height,
arcWidth, arcHeight));
}

}

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