java - drawString 和 Ellipse2D 兼容性问题

标签 java swing awt

我有一个 GUI,我正在使用 drawString 来标记 Ellipse2D 对象的行。问题是两者都没有像我希望的那样同时显示在同一个 tabbedPane 上。

问题:为什么会这样?

画椭圆.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.*;

@SuppressWarnings("serial")
class DrawEllipses extends JPanel {
    private static final int OVAL_WIDTH = 30;
    private static final Color INACTIVE_COLOR = Color.RED;
    private static final Color ACTIVE_COLOR = Color.green;
    private java.util.List<Point> points;
    private java.util.List<Ellipse2D> ellipses = new ArrayList<>();
    private Map<Ellipse2D, Color> ellipseColorMap = new HashMap<>();

    /*
    * This method is used to populate ellipses with initialized ellipse2D
     */
    public DrawEllipses(java.util.List<Point> points) {
        JLabel outBound = new JLabel("<html><font size=6>External Port</font></html>");
        this.points = points;
        for (Point p : points) {
            int x = p.x - OVAL_WIDTH / 2;
            int y = p.y - OVAL_WIDTH / 2;
            int w = OVAL_WIDTH;
            int h = OVAL_WIDTH;
            Ellipse2D ellipse = new Ellipse2D.Double(x, y, w, h);
            ellipses.add(ellipse);
            ellipseColorMap.put(ellipse, INACTIVE_COLOR);
        }

        MyMouseAdapter mListener = new MyMouseAdapter();
        addMouseListener(mListener);
        addMouseMotionListener(mListener);
        add(outBound);
    }

    /*
    * paintComponent is used to paint the ellipses
     */
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        for (Ellipse2D ellipse : ellipses) {
            g2.setColor(ellipseColorMap.get(ellipse));
            g2.fill(ellipse);
        }
    }

    /*
    * MouseAdapter is extended for mousePressed Event that detects if the x, y coordinates
    * of a drawn ellipse are clicked.  If the color is INACTIVE it is changed to ACTIVE and
    * vice versa.
     */
    private class MyMouseAdapter extends MouseAdapter {
        @Override
        /*
        * When mousePressed event occurs, the color is toggled between ACTIVE and INACTIVE
         */
        public void mousePressed(MouseEvent e) {
            Color c = null;
            for (Ellipse2D ellipse : ellipses) {
                if (ellipse.contains(e.getPoint())) {
                    c = (ellipseColorMap.get(ellipse) == INACTIVE_COLOR) ? ACTIVE_COLOR : INACTIVE_COLOR;
                    ellipseColorMap.put(ellipse, c);
                }
            }
            repaint();
        }
    }

    /*
    *Used for button click action to change all ellipses to ACTIVE_COLOR
     */
    public void activateAll(){
        for (Ellipse2D ellipse : ellipses){
            ellipseColorMap.put(ellipse, ACTIVE_COLOR);
        }
        repaint();
    }

    /*
    *Used for button click action to change all ellipses to INACTIVE_COLOR
     */
    public void deactivateAll(){
        for (Ellipse2D ellipse : ellipses){
            ellipseColorMap.put(ellipse, INACTIVE_COLOR);
        }
        repaint();
    }
}

绘制字符串.java

import javax.swing.*;
import java.awt.*;

class DrawString extends JPanel {

    private static final Color STRING_COLOR = Color.BLACK;

    public DrawString() {}

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
            g.setColor(STRING_COLOR);
            g.drawString("1", 40, 80);
            g.drawString("2", 40, 160);
        }
    }

最佳答案

问题可能出在您的布局管理器上。布局管理器负责设置面板上每个组件的大小/位置。

此外,当您进行自定义绘画时,您需要覆盖每个组件的 getPreferredSize() 方法,以便布局管理器可以使用该信息来设置每个组件的大小/位置。如果大小为 (0, 0) 则没有任何内容可绘制。

阅读 Custom Painting 上的 Swing 教程部分获取更多信息和工作示例。

如果您需要更多帮助,请发布适当的 SSCCE这说明了问题。您在上一个问题中发布的代码不是 SSCCE。我们不需要 50 分来证明一个概念。您不需要所有操作,因为它们与组件对齐无关。 SSCCE 的重点是简化代码。

关于java - drawString 和 Ellipse2D 兼容性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31565545/

相关文章:

java - 有什么方法可以在Applet的paint()方法之外进行绘制吗?

java - Spring MVC DefaultRequestToViewNameTranslator 大小写问题

java - 二进制 XML 文件行 #27 : Error inflating class androidx. appcompat.widget.ActionBarContainer?

java - 如何将输出发送到 HTML 中的文本区域?

java - 如何创建一个将标题作为第一列而不是第一行的 Java Swing JTable?

java - 鼠标点击监听器不触发

Java在小程序中显示图像

java - 如何借助 Table 组件显示 JRBeanCollectionDataSource 数据?

java - 在 Java 中解析和检索 JSON 信息——JSONObject 与 JSONArray?

java - 为什么资源包中的瑞典语文本显示为乱码?