RHEE
RHEE

Reputation: 15

Java: How to put both Paintcomponet and other Components?

I'd like to put both paintcomponent(triangle) with other components in center of Frame's BorderLayout.Center. I made code like this, but I could only see other components and I cannot see paintcomponent(triangle). Could you help me?

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.Graphics;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;

    public class OrderMachine extends JFrame {
        private MyPanel panel = new MyPanel();
        private JPanel input = new JPanel();

    public OrderMachine(){
        setTitle("Practice");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container c = getContentPane();     
        c.setLayout(new BorderLayout());

        JButton btn1 = new JButton("order");
        btn1.setBackground(Color.RED);
        c.add(btn1,BorderLayout.NORTH);

        JButton btn2 = new JButton("complete");
        btn2.setBackground(Color.GREEN);
        c.add(btn2,BorderLayout.WEST);

        JButton btn3 = new JButton("calculate");
        btn3.setBackground(Color.ORANGE);
        c.add(btn3,BorderLayout.EAST);

        JButton btn4 = new JButton("ready");
        btn4.setBackground(Color.YELLOW);
        c.add(btn4,BorderLayout.SOUTH);


        c.add(panel, BorderLayout.CENTER);


        setSize(400,300);
        setVisible(true);

    }

I put other components and paintcomponent in same class. Would it be the reason of disfunction?

    class MyPanel extends JPanel{
        private JTextField tf = new JTextField(7);
        private JTextArea ta = new JTextArea(5,8);
        private JLabel jl = new JLabel("menu: ");

        MyPanel(){
        setLayout(new FlowLayout());
        ta.setBackground(Color.YELLOW);
        JButton rot = new JButton("Rotate Button");
        rot.setBackground(Color.CYAN);
        add(ta);
        add(jl);
        add(tf);
        add(rot);
        }
        @Override
        public void paintComponents(Graphics g) {
            super.paintComponents(g);
            g.setColor(Color.BLUE);
            int[] x = {100,120,140};
            int[] y = {50,20,50};       
            g.fillPolygon(x, y, 3);
        }
    }

    public static void main(String[] args) {
    new OrderMachine();
    }

}

Upvotes: 0

Views: 33

Answers (1)

camickr
camickr

Reputation: 324147

    public void paintComponents(Graphics g) {
        super.paintComponents(g);
        g.setColor(Color.BLUE);
        int[] x = {100,120,140};
        int[] y = {50,20,50};       
        g.fillPolygon(x, y, 3);
    }

Custom painting is done by overriding the paintComponent() (without the "s") method. Then the background triangle will the painted and the other components will be painted on top.

Upvotes: 1

Related Questions