【问题标题】:Drawing on top of grid在网格顶部绘图
【发布时间】:2020-06-04 16:29:23
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: java swing draw grid-layout


    【解决方案1】:

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

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

    也许您可以使用Layered Pane 在游戏面板顶部绘制圆圈。 Light 面板必须是不透明的,这样它的背景就不会涂在游戏板上。

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

    【讨论】:

    • 我试图让灯光在网格顶部闪烁。我想隐藏大部分网格。
    • 然后开始看JLayer
    • 我试图添加一个JLayer,但我意识到repaint() 不适用于paint(g),那么我如何在使用LayerUI 时调用paint(g)
    • 你的问题太含糊了。本教程有很多例子,包括一个有亮点的例子。下载演示代码并根据您的要求进行修改。如果您对示例中的代码有具体问题,也许我们可以提供帮助。
    • 不知道为什么你认为 repaint() 不起作用。我猜你应该在JLayer 上调用repaint,它是一个类似于JPanel 的组件。本教程有很多示例,包括一个有亮点的示例。下载演示代码并根据您的要求进行修改。如果您对示例中的代码有具体问题,也许我们可以提供帮助
    猜你喜欢
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    相关资源
    最近更新 更多