Java中的十字线

时间:2010-03-16 05:47:06

标签: java swing cursor

我正在制作一款需要十字准线的游戏。我一直在玩java.awt.cursor类,这很容易,但问题是我不希望十字准线能够离开我为我的游戏创建的窗口,所以我尝试了这个:

  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);

基本上我试图从“g”中删除“椭圆”,只留下一个小环。这里的问题是“g.clip(ellipse);”给了我一个错误。我对此代码的目标是创建一个带有透明中心的圆圈,就像甜甜圈一样。一旦甜甜圈被创建,我将在其内部添加一些小点,使其看起来更像十字准线。可能会或可能不会成为问题的一件事是我计划用操纵杆而不是鼠标移动十字准线......我不知道这是否会限制我选择我的十字准线可以是什么样的物体。

编辑:

这是一个SSCCE版本(差不多......由于“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);
        }  
}

这是同一个包中的另一个类:

包游戏;

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;
    }
}

编辑2:

这是我最近的尝试,这似乎工作正常...如果这是错误的编码,请告诉我(我明白了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);
}

2 个答案:

答案 0 :(得分:2)

出现什么样的错误?

我正在考虑编译错误,因为Graphics没有剪辑方法。 Graphics2D的确如此。

您可以绘制一个椭圆形的圆环形状,并且尺寸<1。半径。

g.setStroke(new BasicStroke(2.0f));
g.drawOval(crossHair.x, crossHair.y, 40, 40);

答案 1 :(得分:2)

  

但问题是我不想要   十字准线能够离开   我为我的游戏创建的窗口,

没有。当鼠标离开框架或组件时,光标会重置。

再次发布您的SSCCE显示问题。