如何在JPanel上的JLabel上“绘画”?

时间:2012-02-17 12:37:17

标签: java swing jlabel graphics2d paintcomponent

我有一组JLabel个,每个都包含一个字母(通过seText()),不透明,背景设置为白色,JPanel上有GridLayout所以标签正在形成一张桌子。 我正在做一个简单的动画,突出显示某些行和列,然后在那里交叉。我可以为此目的使用setBackground()标签,但如果能够使用Graphics对象(可能在交叉点周​​围绘制一个圆圈然后清除它),我认为我有更多“选择” 。 我尝试扩展JLabel,或者直接在JPanel上绘图(在方法中使用getGraphics()),但它不起作用,我认为在这种情况下绘图是在标签后面。我无法弄清楚在任何一种情况下,“绘画”代码应该放在哪里,屏幕上什么都没有出现。

简而言之,可以使用如下方法在标签上绘制?
它应该是JLabel还是JPanel方法?

public void drawsomething() {
    Graphics2D g2d = (Graphics2D) getGraphics();
    g2d.fillRect(100, 100, 100, 100);
    }

4 个答案:

答案 0 :(得分:4)

如果覆盖paintChildren()怎么办?

protected void paintChildren(Graphics g) {
  super.paintChildren(g);
//paint your lines here
}

答案 1 :(得分:3)

您可能想尝试使用JLayeredPane在现有JComponents之上绘制特定图形

请参阅此处示例http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html

答案 2 :(得分:2)

我对绘制内容还不太了解,但只是创建了一个小样本代码供您查看,希望您可以从中获取一些信息。为了在JLabel you can use it's paintComponent(Graphics g)方法上绘画。

示例代码:

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

public class DrawingOnJLabel extends JFrame
{
    private CustomLabel label;
    private int flag = 1;
    private JPanel contentPane;

    public DrawingOnJLabel()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        contentPane = new JPanel();
        contentPane.setBackground(Color.WHITE);
        label = new CustomLabel(200, 200);
        label.setLabelText("A");
        label.setValues(50, 50, 100, 100, 240, 60);

        final JButton button = new JButton("CLEAR");
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                SwingUtilities.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        if (flag == 1)              
                        {
                            label.setFlag(flag);
                            flag = 0;
                            button.setText("REPAINT");
                            contentPane.revalidate();
                            contentPane.repaint();
                        }   
                        else if (flag == 0) 
                        {
                            label.setFlag(flag);
                            flag = 1;
                            button.setText("CLEAR");
                            contentPane.revalidate();
                            contentPane.repaint();
                        }
                    }
                });
            }
        });     

        contentPane.add(label);

        add(contentPane, BorderLayout.CENTER);
        add(button, BorderLayout.PAGE_END);
        setSize(300, 300);
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new DrawingOnJLabel();
            }
        });
    }
}

class CustomLabel extends JLabel
{
    private int sizeX;
    private int sizeY;
    private int x, y, width, height, startAngle, arcAngle;
    private int flag = 0;
    private String text;

    public CustomLabel(int sX, int sY)
    {
        sizeX = sX;
        sizeY = sY; 
    }

    // Simply call this or any set method to paint on JLabel.
    public void setValues(int x, int y, int width, int height, int startAngle, int arcAngle)
    {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.startAngle = startAngle;
        this.arcAngle = arcAngle;
        repaint();
    }

    public void setFlag(int value)
    {
        flag = value;
        repaint();
    }

    public Dimension getPreferredSize()
    {
        return (new Dimension(sizeX, sizeY));
    }

    public void setLabelText(String text)
    {
        super.setText(text);
        this.text = text;
        flag = 0;
        repaint();
    }

    public void paintComponent(Graphics g)
    {
        if (flag == 0)
        {
            g.setColor(Color.RED);          
            g.drawString(text, 20, 20);
            g.setColor(Color.BLUE);
            g.drawOval(x, y, width, height);
            g.fillOval(x + 20, y + 20, 15, 15);
            g.fillOval(x + 65, y + 20, 15, 15);
            g.fillRect(x + 40, y + 40, 5, 20);
            g.drawArc(x + 20, y + 30, 55, 55, startAngle, arcAngle);            
        }
        else if (flag == 1)
        {
            g.clearRect(x, y, width, height);
        }
    }
}

答案 3 :(得分:0)

使用paintComponent(Graphics g)代替paint(Graphics g)。这将绘制在GUI上