Java Swing和绘图问题

时间:2011-05-25 08:34:42

标签: java swing jbutton paintcomponent

我的绘图有问题。我有一个带一个按钮的框架。使用鼠标,我绘制一个变形矩形。但是我有一个小问题,因为在绘制这个矩形时,它正在按钮后面绘制,我希望这个矩形在按钮上方。

这是截图:

这是代码:

package draw;

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


public class Selection extends JPanel
    implements  ChangeListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static final int WIDE = 640;
    private static final int HIGH = 640;  
    private List<Node> nodes = new ArrayList<Node>();
    private Point mousePt = new Point(WIDE / 2, HIGH / 2);
    private Rectangle mouseRect = new Rectangle();
    private boolean selecting = false;

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

            public void run() {
                JFrame f = new JFrame("GraphPanel");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                Selection gp = new Selection(); 
                f.add(new JScrollPane(gp), BorderLayout.CENTER);                
                f.pack();
                f.setVisible(true);
            }
        });
    }

    Selection() {
        JButton but=new JButton("Button");
        add(but);

        this.setPreferredSize(new Dimension(WIDE, HIGH));      
        this.addMouseListener(new MouseHandler());
        this.addMouseMotionListener(new MouseMotionHandler());
    }

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(new Color(0x00f0f0f0));
        g.fillRect(0, 0, getWidth(), getHeight());


            g.setColor(Color.BLACK);
            ((Graphics2D) g).setComposite(AlphaComposite.getInstance(rule, alpha));
            g.fillRect(mouseRect.x, mouseRect.y,
                    mouseRect.width, mouseRect.height);

            g.drawRect(mouseRect.x, mouseRect.y,
                mouseRect.width, mouseRect.height);

    }
    int rule = AlphaComposite.SRC_OVER;
    float alpha = 0.85F;


    private class MouseHandler extends MouseAdapter {

        @Override
        public void mouseReleased(MouseEvent e) {
            selecting = false;
            mouseRect.setBounds(0, 0, 0, 0);
            if (e.isPopupTrigger()) {

            }
            e.getComponent().repaint();
        }

        @Override
        public void mousePressed(MouseEvent e) {
            mousePt = e.getPoint();

                Node.selectNone(nodes);
                selecting = true;          
            e.getComponent().repaint();
        }      
    }

    private class MouseMotionHandler extends MouseMotionAdapter {


        @Override
        public void mouseDragged(MouseEvent e) {
            if (selecting) {
                mouseRect.setBounds(
                    Math.min(mousePt.x, e.getX()),
                    Math.min(mousePt.y, e.getY()),
                    Math.abs(mousePt.x - e.getX()),
                    Math.abs(mousePt.y - e.getY()));

            } 
            e.getComponent().repaint();
        }
    }



    /** A Node represents a node in a graph. */
    private static class Node {

        private Color color;

        private boolean selected = false;
        private Rectangle b = new Rectangle();

        /** Draw this node. */
        public void draw(Graphics g) {
            g.setColor(this.color);

            if (selected) {
                g.setColor(Color.darkGray);
                g.drawRect(b.x, b.y, b.width, b.height);
            }
        }




       /** Mark this node as slected. */
         public void setSelected(boolean selected) {
            this.selected = selected;
        }

        /** Select no nodes. */
        public static void selectNone(List<Node> list) {
            for (Node n : list) {
                n.setSelected(false);
            }
        }
  }



    @Override
    public void stateChanged(ChangeEvent arg0) {
        // TODO Auto-generated method stub

    }

}

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:4)

您在按钮后面的面板上绘制矩形。 (无法让底层组件在子组件上绘制。)如果矩形应位于按钮顶部,则您正在绘制的组件必须位于按钮顶部。 (这样的组件必须是非透明的,因为如果它是不透明的,它将始终覆盖按钮,按钮将不可见。

我建议你在一些玻璃窗上画一个矩形或者什么东西,它位于整个框架的顶部。

答案 1 :(得分:2)

为什么不在玻璃窗格上绘制可以从框架中获取的图形。如何操作,请查看the tutorial.

您应该将所有绘画代码放在扩展JPanel的类中。然后创建一个对象并将其设置为框架的玻璃板。此外,您必须记住将其设置为可见,因为默认情况下它是隐藏的。

答案 2 :(得分:1)

通常,通过覆盖paintComponent()方法来完成自定义绘制。因此绘画顺序是绘制组件然后绘制子项。因此按钮被绘制在矩形的顶部。

在这种情况下,您希望在子项之后绘制矩形,以便您可以通过覆盖paint()方法来完成此操作。现在小组及其孩子将被绘制。然后,您的矩形将被绘制,以便在按钮顶部绘制。

所以你的基本代码应该是:

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

        g.setColor(new Color(0x00f0f0f0));
//      g.fillRect(0, 0, getWidth(), getHeight());

        g.setColor(Color.BLACK);
        ((Graphics2D) g).setComposite(AlphaComposite.getInstance(rule, alpha));
        g.fillRect(mouseRect.x, mouseRect.y, mouseRect.width, mouseRect.height);
        g.drawRect(mouseRect.x, mouseRect.y, mouseRect.width, mouseRect.height);
    }