I am making a “game” in which the player must click on the image bouncing around the screen. The trick is that the screen is in the dark, and the mouse cursor is a "flashlight" that "lights up" around it in a small circle.
I have JFramein one class consisting of:
public class GameFrame {
public static void main(String[] args) throws IOException {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
JFrame jf = new JFrame("Flashlight Game");
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(d);
jf.setLocationRelativeTo(null);
GamePanel gp = new GamePanel();
jf.add(gp);
}
}
I have another class that extends JPanel. Here are its fields related to my problems:
private Point mouse;
private BufferedImage myImage;
private int imageX;
private int imageY;
private int imageSpeedX;
private int imageSpeedY;
My first problem is flashlight. In my method, paintI set the background color of the graphic to the background color of the panel and use the method clearRectto clear the area around the mouse cursor.
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
super.paint(g2);
checkBounce();
move();
g2.drawImage(myImage, imageX, imageY, null);
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, this.getWidth(), this.getHeight());
g2.setBackground(Color.WHITE);
g2.clearRect((int) mouse.getX() - 25, (int) mouse.getY() - 25, 50, 50);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
.
1.) clearOval, ,
2.) , ? , g2.setBackground(Color.WHITE) "" , , JFrame JPanel.
, , int, , - .