Simple invoice swing

I'm in the process of creating a 2D game in which a player wanders through a maze. GUI screenshot

I want to realize some kind of "darkness", even as simple as a transparent form around the player, surrounded by black, like this: screenshot layout

The problem I found with Swing is that, although it is possible, it means that you need to redraw everything that causes an annoying “flickering” effect every time this happens. Is there a way to make some kind of invoice or just a good way to do it at all in Swing? I'm not very experienced with GUI / visual stuff right now, so if possible I would stick with Swing.

EDIT: This is my background painting method, i.e. floor, walls and exit:

    public final void paintBG(Graphics g){
    g.setColor(Color.LIGHT_GRAY); // Screen background
    g.fillRect(0, 0, getWidth(), getHeight());
    // Draw the Walls of the maze
    // scalex and y are for scaling images/walls within the maze since I let users specify how big they want the maze
    for (int j = 0; j < this.height; j++, y += scaley) {
        x = 20;
        for (int i = 0; i < this.width; i++, x += scalex) {
            if (!(maze[j][i].northwall.isBroken())) // If the north wall isn't broken
            {
                g.drawImage(walltile, x, y, scalex, scaley / 5, null); // Draw a wall there (image, xpos, ypos, width, height, observer)
            }
            if (!(maze[j][i].eastwall.isBroken())) // etc
            {
                g.drawImage(walltile, x + scalex, y, scalex / 5, scaley, null);
            }
            if (!(maze[j][i].southwall.isBroken())) {
                g.drawImage(walltile, x, y + scaley, scalex, scaley / 5, null);
            }
            if (!(maze[j][i].westwall.isBroken())) {
                g.drawImage(walltile, x, y, scalex / 5, scaley, null);
            }

            if ((j == mazeinfo.getTargetM()) && (i == mazeinfo.getTargetN())) {
                // Draw the exit
                g.drawImage(jeep, x + (scalex / 2), y + (scaley / 2), cx, cy, null);
                g.setColor(Color.LIGHT_GRAY);
                if (maze[j][i].northwall.isEdge()) {
                    // Paint over the edge creating a 'way out'
                    g.fillRect(x, y, scalex, scaley / 4);
                } else if (maze[j][i].eastwall.isEdge()) {
                    g.fillRect(x + scalex, y, scalex / 4, scaley);
                } else if (maze[j][i].southwall.isEdge()) {
                    g.fillRect(x, y + scaley, scalex, scaley / 4);
                } else if (maze[j][i].westwall.isEdge()) {
                    g.fillRect(x, y, scalex / 4, scaley);
                }
            }
        }
    }
}

paintPlayer paintEnemy , . , .

+4
2

:

  • , JFrame. , paintComonent JPanel, .
  • , , . .
  • , BufferedImage , de-novo, , . BufferedImage Graphics#drawImage(...).
  • , . repaint() paintComponent, .
  • ...

, - . BufferedImage paintComponent. BufferedImage, .

, ( , , , ) , .

+4

LayerUI Oracle Swing UI. AlphaComposite - .

LayerUI, , .

class SpotlightLayerUI extends LayerUI<JPanel> {
  private boolean mActive;
  private int mX, mY;

  @Override
  public void installUI(JComponent c) {
    super.installUI(c);
    JLayer jlayer = (JLayer)c;
    jlayer.setLayerEventMask(
      AWTEvent.MOUSE_EVENT_MASK |
      AWTEvent.MOUSE_MOTION_EVENT_MASK
    );
  }

  @Override
  public void uninstallUI(JComponent c) {
    JLayer jlayer = (JLayer)c;
    jlayer.setLayerEventMask(0);
    super.uninstallUI(c);
  }

  @Override
  public void paint (Graphics g, JComponent c) {
    Graphics2D g2 = (Graphics2D)g.create();

    // Paint the view.
    super.paint (g2, c);

    if (mActive) {
      // Create a radial gradient, transparent in the middle.
      java.awt.geom.Point2D center = new java.awt.geom.Point2D.Float(mX, mY);
      float radius = 72;
      float[] dist = {0.0f, 1.0f};
      Color[] colors = {new Color(0.0f, 0.0f, 0.0f, 0.0f), Color.BLACK};
      RadialGradientPaint p =
          new RadialGradientPaint(center, radius, dist, colors);
      g2.setPaint(p);
      g2.setComposite(AlphaComposite.getInstance(
          AlphaComposite.SRC_OVER, .6f));
      g2.fillRect(0, 0, c.getWidth(), c.getHeight());
    }

    g2.dispose();
  }

  @Override
  protected void processMouseEvent(MouseEvent e, JLayer l) {
    if (e.getID() == MouseEvent.MOUSE_ENTERED) mActive = true;
    if (e.getID() == MouseEvent.MOUSE_EXITED) mActive = false;
    l.repaint();
  }

  @Override
  protected void processMouseMotionEvent(MouseEvent e, JLayer l) {
    Point p = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), l);
    mX = p.x;
    mY = p.y;
    l.repaint();
  }
}

, LayerUI . . setLayerEventMask() JLayer.

source: JLayer

+3

Source: https://habr.com/ru/post/1625158/


All Articles