gpt4 book ai didi

java - Java 中的十字准线

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:29:01 26 4
gpt4 key购买 nike

我正在制作一款需要十字准线的游戏。我一直在玩 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);”给我一个错误。我使用这段代码的目的是创建一个中心透明的圆,就像一个 donut 。创建 donut 后,我将在其内部添加一些小点,使其看起来更像十字准线。可能会或可能不会成为问题的一件事是,我计划使用操纵杆而不是鼠标来移动十字准线……我不知道这是否会限制我对十字准线可以是哪种对象的选择。

编辑:

这是一个 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);
}

最佳答案

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

事实并非如此。当鼠标移出框架或组件时,光标会重置。

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

关于java - Java 中的十字准线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2452358/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com