Overlapping AWT lines and Swing JLabels

假装没事ソ 提交于 2019-12-20 04:53:51

问题


I have a problem in my application using line primitives and JLables. I try to explain it:

  • I have to draw a vehicle route using lines to represent roads and JLabels to represent cities. I need the use of JLabels because each JLabel has a Listener that shows a dialog with information about the city.
  • I redefine paint() method of my main JPanel. In that method I first in invoke the super.paint(), then I draw the lines and finally I add the Labels to the JPanel.

The problem is that the lines overlap the labels regardless the matter the order of painting them. Is there any suggestion?


回答1:


You can also override paintComponent() or paintChildren() methods of the JPanel. In the paintChildren() call your lines drawing and then super to draw JLabels.




回答2:


anothe way should be

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class AddVertexDemo {

    public AddVertexDemo() {
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("AddVertexDemo");
        frame.getContentPane().add(new Gui().getMainPanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowUI();
            }
        });
    }
}

class DrawingPanel extends JPanel {

    private static final int RADIUS = 6;
    private static final long serialVersionUID = 1L;
    private List<Shape> vertexList = new ArrayList<Shape>();

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        if (vertexList.size() > 1) {
            Shape s0 = vertexList.get(0);
            Shape s1 = null;
            for (int i = 0; i < vertexList.size(); i++) {
                s1 = vertexList.get(i);
                drawConnectingLine(g, s0, s1);
                s0 = s1;
            }
            s1 = vertexList.get(0);
            //drawConnectingLine(g2, s0, s1);
        }
        for (Shape shape : vertexList) {
            g2.setColor(Color.blue);
            g2.fill(shape);
            g2.setColor(Color.blue.darker().darker());
            g2.draw(shape);
        }
    }

    private void drawConnectingLine(Graphics g, Shape s0, Shape s1) {
        Rectangle r0 = s0.getBounds();
        Rectangle r1 = s1.getBounds();
        int x0 = r0.x + r0.width / 2;
        int y0 = r0.y + r0.height / 2;
        int x1 = r1.x + r1.width / 2;
        int y1 = r1.y + r1.height / 2;
        g.drawLine(x0, y0, x1, y1);
    }

    public void addVertex(Point p) {
        int x = p.x - RADIUS;
        int y = p.y - RADIUS;
        int w = 2 * RADIUS;
        int h = w;
        vertexList.add(new Ellipse2D.Double(x, y, w, h));
        repaint();
    }

    public void removeVertex(Point p) {
        if (vertexList.size() > 0) {
            for (int i = vertexList.size() - 1; i >= 0; i--) {
                if (vertexList.get(i).contains(p)) {
                    vertexList.remove(i);
                    repaint();
                    return;
                }
            }
        }
    }
}

 class Gui {

    private static final Dimension DRAWING_PANEL_SIZE = new Dimension(600, 500);
    private JPanel mainPanel = new JPanel(new BorderLayout());
    private DrawingPanel drawingPanel = new DrawingPanel();
    private JToggleButton addVertexBtn = new JToggleButton("Add Vertex");
    private JToggleButton removeVertexBtn = new JToggleButton("Remove Vertex");

    Gui() {
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(addVertexBtn);
        buttonPanel.add(removeVertexBtn);
        DrawPanelMouseListener mouseListener = new DrawPanelMouseListener();
        mouseListener.setDrawingPanel(drawingPanel);
        mouseListener.setGui(this);
        drawingPanel.addMouseListener(mouseListener);
        drawingPanel.setPreferredSize(DRAWING_PANEL_SIZE);
        drawingPanel.setBorder(BorderFactory.createLineBorder(Color.black));
        mainPanel.add(drawingPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }

    public boolean isAddingVertex() {
        return addVertexBtn.isSelected();
    }

    public boolean isRemovingVertex() {
        return removeVertexBtn.isSelected();
    }

    public void setAddingVertex(boolean addingVertex) {
        addVertexBtn.setSelected(addingVertex);
    }

    public void setRemovingVertex(boolean removingVertex) {
        removeVertexBtn.setSelected(removingVertex);
    }
}

class DrawPanelMouseListener extends MouseAdapter {

    private DrawingPanel drawingPanel;
    private Gui gui;

    public DrawPanelMouseListener() {
    }

    public void setGui(Gui gui) {
        this.gui = gui;
    }

    public void setDrawingPanel(DrawingPanel drawingPanel) {
        this.drawingPanel = drawingPanel;
    }

    @Override
    public void mousePressed(MouseEvent me) {
        if (gui.isAddingVertex() && gui.isRemovingVertex()) {
            gui.setAddingVertex(false);
            gui.setRemovingVertex(false);
            return;
        }
        if (gui.isAddingVertex()) {
            drawingPanel.addVertex(me.getPoint());
            gui.setAddingVertex(false);
        }
        if (gui.isRemovingVertex()) {
            drawingPanel.removeVertex(me.getPoint());
            gui.setRemovingVertex(false);
        }
    }
}



回答3:


I'm not sure that this is the right way to do this but you can try this:

  1. Create 2 panels. One for drawing lines and another for drawing buildings(labels).
  2. Add both panels in LayeredPane of JFrame. Add panel with line in lower layer then panel with labels.

You can use LayeredPanes in other ways also to solve your problem. Learn more here: How to use Layered Panes in java



来源:https://stackoverflow.com/questions/8587293/overlapping-awt-lines-and-swing-jlabels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!