gpt4 book ai didi

java - 在网格顶部绘图

转载 作者:行者123 更新时间:2023-12-01 16:23:50 24 4
gpt4 key购买 nike

我正在尝试使用 Swing 在 Java 中制作迷宫游戏。我的迷宫采用网格布局,并尝试在其顶部绘制圆圈,但每当我绘制时,圆圈都会出现在网格下方。有什么办法可以改变这个吗?这是我第一次使用swing。 screenshot of my game

这是我的课:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.plaf.LayerUI;

public class Light extends LayerUI<JPanel> implements KeyListener, ActionListener{
private javax.swing.Timer myTimer;
private ArrayList <Area> circles;
private Color[] colours;
private static double x,y,d,xV,yV,originalD,originalX,originalY;

public Light(){
x=200;
y=200;
d=70;
xV=0;
yV=0;
circles = new ArrayList <Area>();
colours = new Color[20];
for (int i=0;i<20;i++) colours[i]=new Color(240-i*12,240-i*12,240-i*12);
addKeyListener(this);
setFocusable(true);
myTimer = new javax.swing.Timer(120, this );
myTimer.start();
}

public void keyTyped(KeyEvent e) {}

public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_UP) yV=-20;
else if (key == KeyEvent.VK_DOWN) yV=20;
else if (key == KeyEvent.VK_LEFT) xV=-20;
else if (key == KeyEvent.VK_RIGHT) xV=20;
repaint();
}

public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_UP) yV=0;
else if (key == KeyEvent.VK_DOWN) yV=0;
else if (key == KeyEvent.VK_LEFT) xV=0;
else if (key == KeyEvent.VK_RIGHT) xV=0;
repaint();
}

public void actionPerformed(ActionEvent e) {
if (e.getSource()==myTimer) repaint();
}

private AlphaComposite makeComposite(float alpha) {
int type = AlphaComposite.SRC_OVER;
return(AlphaComposite.getInstance(type, alpha));
}

private void createCricles(Graphics2D g2d, float alpha,Color c,Area circle) {
Composite originalComposite = g2d.getComposite();
g2d.setPaint(c);
g2d.setComposite(makeComposite(alpha));
g2d.fill(circle);
g2d.setComposite(originalComposite);
}

private void move(){
x+=xV;
y+=yV;
}

public double getLightX(){
return x;
}

public double getLightY(){
return y;
}

public void drawCircles(Graphics g,JComponent c) {
super.paint(g,c);
Graphics2D g2d = (Graphics2D)g;
circles.clear();
//more code here
}
}

这些是我收到的错误:

./Light.java:23: error: cannot find symbol
addKeyListener(this);
^
symbol: method addKeyListener(Light)
location: class Light
./Light.java:24: error: cannot find symbol
setFocusable(true);
^
symbol: method setFocusable(boolean)
location: class Light
./Light.java:37: error: cannot find symbol
repaint();
^
symbol: method repaint()
location: class Light
./Light.java:46: error: cannot find symbol
repaint();
^
symbol: method repaint()
location: class Light
./Light.java:50: error: cannot find symbol
if (e.getSource()==myTimer) repaint();
^
symbol: method repaint()
location: class Light
5 errors

最佳答案

不要直接调用paintComponent()。当组件需要重新绘制时,Swing 将调用paintComponent()。

drawCircles(…) 是一个应该在 PaintComponent() 方法中调用的方法。

也许你可以使用Layered Pane在游戏面板顶部绘制圆圈。 Light 面板需要是不透明的,这样它的背景就不会覆盖游戏板。

或者您可以使用JLayer 。不太确定您想要达到什么效果。

关于java - 在网格顶部绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62199650/

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