JPanel上Graphics2D对象的相对定位

时间:2015-08-20 19:41:50

标签: java swing graphics2d

我正在使用Swing制作GUI,并希望使用相对定位配置Graphics2D对象。我想这样做,当我调整窗口大小时,对象重新绘制在相对于初始锚位置的新位置(可能在某处的左上角)。我曾尝试使用布局管理器来执行此操作,但它不会影响实际绘图,因为这些点几乎是硬编码的。

最好的方法是什么?谁能提供一个很好的例子?感谢。

这是我自包含的例子:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class DrawPanelMain extends JPanel {

    public static final double version = 0.0;
    private JPanel btnPanel = new JPanel();
    private JPanel switchPanel = new JPanel();
    private JScrollPane switchPanelScrollPane = new JScrollPane(switchPanel);
    //private JPanel[] panelArray = new JPanel[3];

    DrawEllipses drawEllipses = new DrawEllipses(POINT_LIST);

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                initializePointList();
                createAndShowGui();
            }
        });
    }

    public static java.util.List<Point> POINT_LIST = new ArrayList<>();

    /*
    * This loop will initialize POINT_LIST with the set of points for drawing the ellipses.
    * The for each loop initializes points for the top row and the second for loop draws the
    * right triangle.
    */
    private static void initializePointList() {

        int ellipsePointsYCoordinate[] = {140, 200, 260, 320, 380, 440, 500, 560, 620};
        int ellipsePointsXCoordinate[] = {140, 200, 260, 320, 380, 440, 500, 560, 620, 680};
        int xx = 80;

        for (int aXt : ellipsePointsXCoordinate) {
            POINT_LIST.add(new Point(aXt, xx));
        }

        for (int i = 0; i < ellipsePointsYCoordinate.length; i++) {
            for (int j = i; j < ellipsePointsYCoordinate.length; j++) {
                POINT_LIST.add(new Point(ellipsePointsXCoordinate[i], ellipsePointsYCoordinate[j]));
            }
        }
    }

    public DrawPanelMain() {

        switchPanel.setBorder(BorderFactory.createLoweredSoftBevelBorder());
        switchPanel.setBackground(Color.DARK_GRAY);
        switchPanel.add(drawEllipses);
        switchPanelScrollPane.setPreferredSize(new Dimension(600,600));

        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        // first column
        c.gridx = 0;
        add(switchPanelScrollPane, c);

        // second column
        c.gridx = 1;

        // first row
        c.gridy = 0;

        // second row
        c.gridy = 1;
        c.gridx = 0;
        add(btnPanel, c);

        btnPanel.add(new JButton(new AddSwitchAction("Add Switch Panel")));
    }

    public static void createAndShowGui() {

        JFrame frame = new JFrame("RF Connection Panel " + version);

        frame.setLayout(new BorderLayout());
        frame.add(new DrawPanelMain());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(false);
        //frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

    /*
    * AddSwitchAction will add a new pane to the tabbedPane when the add switch button is clicked
    */
    private class AddSwitchAction extends AbstractAction {
        public AddSwitchAction(String name) {
            super(name);
            int mnemonic = (int) name.charAt(0);
            putValue(MNEMONIC_KEY, mnemonic);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            String title = "Switch ";
            DrawEllipses tabComponent = new DrawEllipses(POINT_LIST);
            switchPanel.add(title, tabComponent);

        }
    }

}

@SuppressWarnings("serial")
class DrawEllipses extends JPanel {
    private final int PREF_W = 750; //Window width
    private final int PREF_H = 750; //Window height
    private 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 the initialized ellipse2D dimensions
     */
    public DrawEllipses(java.util.List<Point> points) {
        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);
    }

    /*
     * paintComponent is used to paint the ellipses
     */
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); // paints the background
        setBackground(Color.DARK_GRAY);
        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);
            g2.setColor(Color.BLACK);
            g2.setStroke(new BasicStroke(2));
            g2.draw(ellipse);
        }

        /*
         * Set the font characteristics, color, and draw the row labels.
         */
        g.setFont(new Font("TimesRoman", Font.BOLD, 18));
        g.setColor(Color.BLACK);

        //Along the top row
        g.drawString("External Port", 10, 50);
        g.drawString("1", 135, 50);
        g.drawString("2", 195, 50);
        g.drawString("3", 255, 50);
        g.drawString("4", 315, 50);
        g.drawString("5", 375, 50);
        g.drawString("6", 435, 50);
        g.drawString("7", 495, 50);
        g.drawString("8", 555, 50);
        g.drawString("9", 615, 50);
        g.drawString("10", 672, 50);

        //Along the Y-axis
        g.drawString("Radio 2", 40, 145);
        g.drawString("3", 90, 205);
        g.drawString("4", 90, 265);
        g.drawString("5", 90, 325);
        g.drawString("6", 90, 385);
        g.drawString("7", 90, 445);
        g.drawString("8", 90, 505);
        g.drawString("9", 90, 565);
        g.drawString("10", 90, 625);

        //Along the X-Axis
        g.drawString("1", 135, 670);
        g.drawString("2", 195, 670);
        g.drawString("3", 255, 670);
        g.drawString("4", 315, 670);
        g.drawString("5", 375, 670);
        g.drawString("6", 435, 670);
        g.drawString("7", 495, 670);
        g.drawString("8", 555, 670);
        g.drawString("9", 615, 670);

        //Draws a 3DRect around the top row of ellipse2D objects
        g2.setColor(Color.lightGray);
        g2.draw3DRect(120, 60, 580, 40, true);
        g2.draw3DRect(121, 61, 578, 38, true);
        g2.draw3DRect(122, 62, 576, 36, true);

    }

    /*
     * 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;
            for (Ellipse2D ellipse : ellipses) {
                if (ellipse.contains(e.getPoint())) {
                    c = (ellipseColorMap.get(ellipse) == INACTIVE_COLOR) ? ACTIVE_COLOR : INACTIVE_COLOR;
                    ellipseColorMap.put(ellipse, c);
                }
            }
            repaint();
        }
    }

    /*
* This method will set the dimensions of the JFrame equal to the preferred H x W
*/
    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }


    /*
     * 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();
    }
}

1 个答案:

答案 0 :(得分:3)

正如我之前建议的那样,如果你想要相对定位,那么你需要在每次绘画时(或调整组件大小时)计算相对位置。

基本上你知道宽度方向你有10个圆形,你想要每个30像素绘制一个标签,固定宽度为100个。这意味着你的最小宽度为400像素。

当然这是不合理的,因为你想要在每个圆圈之间留一个间隙,所以你需要决定你的首选差距是什么,再乘以9来确定你的真实最小尺寸。看起来你有大约30像素的间隙,这是另一个270像素,给出了一个670像素的首选大小。

所以问题是当宽度大于670时你会怎么做?你是增加差距还是留在30?

更难的情况是当宽度小于670时,您现在需要将间隙调整得更小,降低到某个最小值。

确定要使用的水平间隙后,您将对垂直间隙进行相同的分析。

现在,当您进行绘画时,您将获得起始位置,并且您绘制的每一行都会因您的垂直间隙而增加。并且行上的每个圆圈都会增加水平间隙。

我从未尝试过,但我相信您可以使用Ellipse的setFrame(...)方法动态移动其位置。所以当你进行绘画时,椭圆将不在正确的位置,它应该正确响应鼠标事件。

所以基本上你是在为Graphics编写自己的自定义布局管理器。

第二种方法是使用真实的组件而不是自定义绘画。

您可以轻松创建自定义图标来表示圆圈。那么也许你可以使用JToggleButton。然后,您可以通过切换按钮和两个不同颜色的图标来处理更改颜色的逻辑。

现在困难的部分。我以前从未尝试过,但我相信你可以使用Spring Layout

SpringLayout将组件相对于彼此定位。它还包含&#34;弹簧&#34;在组件之间允许间隙增长/缩小。

我认为这是两种方法。