在JFrame上绘画

时间:2014-10-22 15:39:22

标签: java swing

我有一个扩展JFrame的类,上面有十个单选按钮,我使用了各种JPanel和GridLayouts帮助我正确放置它们。我正在尝试这样做,以便当您选择单选按钮的组合时,程序将按照您选择它们​​的顺序在每个单选按钮之间画一条线。但是,我无法得到任何东西。我不确定我是不是要覆盖正确的方法,如果我应该使用Graphics2D,如果面板隐藏了我正在绘制的内容...最好,我想要一个没有让我覆盖的解决方案一个JPanel或类似的东西。

    public void paintComponent(Graphics g)
{
    super.update(g);
    if(buttonsSelected>1)
    {
        g.setColor(new Color(0xE3, 0xC9, 0x39));
        for(int k=0;k>4&&lastButton[k+1]!=-1;k++)
        {
            g.drawLine(buttonTest[lastButton[k]].getX(), buttonTest[lastButton[k]].getY(), buttonTest[lastButton[k+1]].getX(), buttonTest[lastButton[k]].getY());
            System.out.println("Ole!");
        }
    }
}

此外,这是我用于绘制窗格的代码的一部分

    int j=0;
    for(int k=0;k<10;k++)
    {
        buttonTest[k]=new JRadioButton();
        buttonTest[k].setActionCommand(Integer.toString(k));
        buttonTest[k].setToolTipText(powersDin[k]);
        buttonTest[k].addActionListener(new GoddessListener());
        buttonTest[k].setEnabled(false);
    }
    buttonTest[0].setEnabled(true);
    buttonTest[6].setEnabled(true);
    buttonTest[9].setEnabled(true);

    paneGrids[0]=new JPanel();
    paneGrids[0].setLayout(new GridLayout(1,7));
    paneGrids[0].add(new JLabel()); //adding a blank JLabel lets me pad out the empty cells I don't want to fill
    paneGrids[0].add(new JLabel());
    paneGrids[0].add(new JLabel());
    paneGrids[0].add(buttonTest[j++]);
    paneGrids[0].add(new JLabel());
    paneGrids[0].add(new JLabel());
    paneGrids[0].add(new JLabel());

1 个答案:

答案 0 :(得分:4)

您应该在@Override方法上使用paintComponent注释。您将看到您没有覆盖JFrame上的任何方法。这是因为J paintComponent是在JComponent上定义的,JFrame不会从中继承。因此,永远不会调用paintComponent方法。

不要尝试在顶级容器(which should generally not be done)上绘画,而是使用JPanel进行自定义绘画。

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    // your custom painting
}

使用JPanel并不复杂,只需拨打yourFrame.setContentPane(yourCustomPanel);

如果您想在孩子之上绘制 可以明智地覆盖paint paintComponent必然会吸引孩子,因为它是用于绘制组件。 paint将使用所有内容。在儿童上绘制的另一个选择是使用更难以使用的玻璃窗格或JLayeredPane之类的东西。

这是一个MCVE,它演示了如何做一些像我想你想做的事情。

connective grid

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JCheckBox;
import javax.swing.SwingUtilities;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.RenderingHints;

import java.util.Map;
import java.util.LinkedHashMap;
import java.util.Iterator;

class ConnectiveGrid implements Runnable {
    static final int GRID_H = 5;
    static final int GRID_W = 10;

    Map<JCheckBox, Point> path;
    GridPanel grid;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new ConnectiveGrid());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Connective Grid");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // (LinkedHashMap will maintain insertion order)
        path = new LinkedHashMap<JCheckBox, Point>();

        grid = new GridPanel();
        ActionListener listener = new Listener();

        for(int i = 0; i < GRID_H * GRID_W; i++) {
            JCheckBox check = new JCheckBox();
            check.addActionListener(listener);
            grid.add(check);
        }

        frame.setContentPane(grid);
        frame.pack();
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    class Listener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent ae) {
            Object sc = ae.getSource();
            if(!(sc instanceof JCheckBox)) {
                return;
            }

            JCheckBox check = (JCheckBox)sc;
            if(check.isSelected()) {

                Dimension sz = check.getSize();
                Point pt = check.getLocation();

                pt.x += sz.width / 2;
                pt.y += sz.height / 2;

                // if the check box is selected
                // put it and its associated point in the path
                path.put(check, pt);

            } else {

                // else remove the check box and point
                path.remove(check);
            }

            // prompt a clean repaint
            grid.repaint();
        }
    }

    class GridPanel extends JPanel {
        GridPanel() {
            super(new GridLayout(GRID_H, GRID_W));
        }

        @Override
        public void paint(Graphics g) {
            super.paint(g);

            // verify there are enough points to draw at least one line
            if(path.size() < 2) {
                return;
            }

            Graphics2D copy = (Graphics2D)g.create();
            copy.setPaint(Color.BLUE);
            copy.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON
            );

            // iterate the path and paint line-by-line
            Iterator<Point> it = path.values().iterator();
            Point prev = it.next();
            do {
                Point next = it.next();

                copy.drawLine(
                    prev.x, prev.y,
                    next.x, next.y
                );

                prev = next;
            } while(it.hasNext());

            copy.dispose();
        }
    }
}