简体   繁体   中英

How to “paint” on JLabels on a JPanel?

I have a set of JLabel s, each containing a letter (via seText()), opaque and background set to white, on a JPanel with a GridLayout so the labels are forming a table. I am doing a simple animation of highlighting certain rows and columns then there intersection. I can use the setBackground() of labels for this purpose, but thought I'd have more "choices" if a was able to use a Graphics object (maybe drawing a circle around intersection, then clearing it). I tried to extend JLabel , or drawing on the JPanel directly(using getGraphics() in a method) but it didn't work, I think the drawing is behind the labels in this case. I can't figure out where should the "painting" code be placed in either case, nothing appeared on the screen.

in short, a method like the following, can be used to draw on top of labels?
should it be a JLabel or a JPanel method?

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

What if you override paintChildren() ?

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

You might want to try a JLayeredPane to paint your specific drawings on top of the existing JComponents

see example here http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html

I really don't know much about drawing stuff yet, but just created one small sample code for you to look at, hopefully you can get some information out of it. In order to paint on the JLabel you can use it's paintComponent(Graphics g) method.

A Sample Code :

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);
        }
    }
}

Use paintComponent(Graphics g) instead of paint(Graphics g). That will paint over the GUI

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM