lundi 1 juin 2020

paintComponent() and paint() - drawing JButton with decorator pattern

I found a decorator pattern example with Swing component. The following code is drawing three buttons on JFram. One of the button has slash on it and implemented with Decorator pattern. The original one has paint() method, but I replaced paint() with paintComponent(Grphaics g), then it fails to draw lines on the button. Is it impossible to use paintComponent() instead of paint()? If possible how to do that? What is the problem of this trial? What I am missing?

Original code link is http://www.java2s.com/Code/Java/Design-Pattern/DecoratorpatterninJava.htm

Thank you.


public class DecoWindow extends JFrame implements ActionListener {
    JButton Quit;

    public DecoWindow() {
        super("Deco Button");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel jp = new JPanel();

        getContentPane().add(jp);
        jp.add(new CoolDDecorator(new JButton("Cbutton")));
        jp.add(new SlashDDecorator(new CoolDDecorator(new JButton("Dbutton"))));        
        jp.add(Quit = new JButton("Quit"));
        Quit.addActionListener(this);
        setSize(new Dimension(200, 100));
        setVisible(true);
        Quit.requestFocus();
    }

    public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }

    public static void main(String argv[]) {
        new DecoWindow();
    }
}

class DDecorator extends JComponent {
    public DDecorator(JComponent c) {
        setLayout(new BorderLayout());
        add("Center", c);
    }
}

class SlashDDecorator extends DDecorator {
    int x1, y1, w1, h1;

    public SlashDDecorator(JComponent c) {
        super(c);
    }

    public void setBounds(int x, int y, int w, int h) {
          x1 = x; y1 = y;
          w1 = w; h1 = h;
          super.setBounds(x, y, w, h);
    }

    public void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2d = (Graphics2D) g;
          g2d.setColor(Color.red);
          g2d.drawLine(0, 0, w1, h1);
    }
}

class CoolDDecorator extends DDecorator {
    boolean mouse_over; //true when mose over button

    JComponent thisComp;

    public CoolDDecorator(JComponent c) {
        super(c);
        mouse_over = false;
        thisComp = this; //save this component
        //catch mouse movements in inner class
        c.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                mouse_over = true; //set flag when mouse over
                thisComp.repaint();
            }

            public void mouseExited(MouseEvent e) {
                mouse_over = false; //clear flag when mouse not over
                thisComp.repaint();
            }
        });
    }

    //paint the button
    public void paintComponent(Graphics g) {
        super.paintComponent(g); //first draw the parent button
        Graphics2D g2d = (Graphics2D) g;

        //if the mouse is not over the button
        //erase the borders
        if (!mouse_over) {
            Dimension size = super.getSize();
            g2d.setColor(Color.lightGray);
            g2d.drawRect(0, 0, size.width - 1, size.height - 1);
            g2d.drawLine(size.width - 2, 0, size.width - 2, size.height - 1);
            g2d.drawLine(0, size.height - 2, size.width - 2, size.height - 2);
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire