Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 664825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:38:31+00:00 2026-05-13T23:38:31+00:00

I am making a game that needs a crosshair. I have been playing with

  • 0

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);
}
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:38:31+00:00Added an answer on May 13, 2026 at 11:38 pm

    but the problem is that I do not want
    the crosshairs to be able to leave the
    window I create for my game,

    It doesn’t. The cursor gets reset when the mouse moves off the frame or component.

    Again, post your SSCCE showing the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a game that needs to be updated. I have two JAR
Am am making a wap-game that will have a minimap. It is basically a
I am making an android game that should display a message that you have
Trying to implement a timer for my game that I'm making. I have a
I am making a game that has its own level editor. The editor simply
I am making an android game that should delete some falling images from the
I'm making a new game that generates a huge grid (lets say 1000x1000). The
I am looking into making a text based game that I wrote in Haskell
I'm making a multiplayer flash game that has a lot of graphics/code. I'm having
I am trying to use AJAX for a web app game that im making.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.