I am making a game that needs a crosshair. I have been playing with the java.awt.cursor class and that is easy enough, but the problem is that I do not want the crosshairs to be able to leave the window I create for my game, so I tried this instead:
private void drawCrossHair(Graphics g){
Ellipse2D ellipse = new Ellipse2D.Float();
ellipse.setFrame(crossHair.x, crossHair.y, 36, 36);
Color yellow = new Color (0xEDFF62);
g.setColor(yellow);
g.fillOval(crossHair.x, crossHair.y, 40, 40);
g.setClip(ellipse);
g.clip(ellipse);
Basically I am trying to remove the "ellipse" from "g" leaving only a small ring behind. The problem here is that "g.clip(ellipse);" gives me an error. My objective with this code is to create a circle with a transparent center, like a donut. Once the donut is created I will add some small points on the inside of it so it looks more like crosshairs. One thing that may or may not be an issue is that I plan on moving the crosshairs with a joystick, not a mouse... I do not know if that will limit my options for what kind of object my crosshairs can be.
EDIT:
Here is a SSCCE version (well almost... does not compile due to "g2 = bf.getDrawGraphics()")
package game;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import java.awt.geom.Ellipse2D;
public class Game extends JFrame {
private int windowWidth = 1280;
private int windowHeight = 1024;
private Ball crossHair;
public static void main(String[] args) {
new Game();
}
public Game() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(windowWidth, windowHeight);
this.setResizable(false);
this.setLocation(0,0);
this.setVisible(true);
this.createBufferStrategy(2);
initGame();
gameLoop();
}
private void initGame() {
crossHair = new Ball (windowWidth/2, windowHeight/2, 3, 3);
}
private void gameLoop() {
//game logic
drawFrame();
}
private void drawFrame() {
//Setting up Double Buffering
BufferStrategy bf = this.getBufferStrategy();
Graphics2D g2 = (Graphics2D)bf.getDrawGraphics();
try {
g2 = bf.getDrawGraphics();
Color darkBlue = new Color(0x010040);
g2.setColor(darkBlue);
g2.fillRect(0, 0, windowWidth, windowHeight);
drawCrossHair(g2);
} finally {
// dispose of graphic.
g2.dispose();
}
// show contents of backbuffer on screen
bf.show();
Toolkit.getDefaultToolkit().sync();
}
private void drawCrossHair(Graphics2D g2){
Color yellow = new Color (0xEDFF62);
g2.setColor(yellow);
g2.fillOval(crossHair.x, crossHair.y, 40, 40);
Ellipse2D ellipse = new Ellipse2D.Float();
ellipse.setFrame(crossHair.x, crossHair.y, 36, 36);
g2.setClip(ellipse);
g2.clip(ellipse);
}
}
And here is another class in the same package:
package game;
public class Ball {
public int x;
public int y;
public int dx;
public int dy;
public Ball(int x, int y, int dx, int dy) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
}
}
EDIT 2:
Here is my latest attempt, this seems to work ok... please let me know if this is bad coding (I got the idea here):
private void drawCrossHair(Graphics g){
Color yellow = new Color (0xEDFF62);
g.setColor(yellow);
for (int i = 0; i < 1; i++) {
g.drawOval(crosshair.x + i, crosshair.y + i, 40 - i - i, 40 - i - i);
}
g.fillArc(crosshair.x + 10, crosshair.y + 21 , 20, 20, -45, -90);
g.fillArc(crosshair.x - 1, crosshair.y + 10, 20, 20, -135, -90);
g.fillArc(crosshair.x + 10, crosshair.y - 1, 20, 20, -225, -90);
g.fillArc(crosshair.x + 21, crosshair.y + 10, 20, 20, -315, -90);
}