views:

67

answers:

2

I've got a task to display painted 'eyes' with menu buttons to change their colors, and background color. Next animate them. But currently I'm stuck at painting, sinc in my JFrame I've Created JPanel containing panels with drawn eyes and buttons. Buttons are rendered properly but my eyes canvas is not shown. I've tried changing paint to paintComponent, setting contentPane differently but still nothing works.

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

public class Main extends JFrame {

    public static void main(String[] args) {
        final JFrame frame = new JFrame("Eyes");
        frame.setPreferredSize(new Dimension(600, 450));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel players = new JPanel(new GridLayout(1, 3));
                players.add(new JButton("Eyes color"));
                players.add(new JButton("Eye pupil"));
                players.add(new JButton("Background color"));

        JPanel eyes = new JPanel();
        Eyes e = new Eyes();
        eyes.add(e);
        eyes.setPreferredSize(new Dimension(600, 400));

        JPanel content = new JPanel();
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
        frame.setContentPane(content);
        content.add(players);
        content.add(eyes);

//        frame.getContentPane().add(content);
        frame.pack();
    frame.setVisible(true);

    }
}

class Eyes extends JPanel {

    public Eyes(){     
    }

    public void paint(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        BasicStroke bs = new BasicStroke(3.0f);
        g2d.setBackground(Color.green);
        g2d.setStroke(bs);
        g2d.setColor(Color.yellow);
        g2d.fillOval(50, 150, 200, 200);
        g2d.fillOval( 350, 150, 200, 200);
        g2d.setColor(Color.BLACK);
        g2d.drawOval(49, 149, 201, 201);
        g2d.drawOval(349, 149, 201, 201);
        g2d.fillOval(125, 225, 50, 50);
        g2d.fillOval(425, 225, 50, 50);
    }
}

This is what I should get : alt text This is what I have : alt text

When I've tried painting it directly in JFrame it works almost perfect, apart of background not being set. Why setBackgroundColor doesn't influence my drawing in any way ?

+3  A: 

Eyes class is already a panel. You don't need this additional panel: JPanel eyes = new JPanel();

Make it like this:

Eyes eyes = new Eyes();
eyes.setPreferredSize(new Dimension(600, 400));

JPanel content = new JPanel();
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
frame.setContentPane(content);
content.add(players);
content.add(eyes);

You could also make it work with your additional panel. Just set the preferred size of the e panel.

JPanel eyes = new JPanel();
Eyes e = new Eyes();
eyes.add(e);
e.setPreferredSize(new Dimension(600, 400));
eyes.setPreferredSize(new Dimension(600, 400));
Petar Minchev
and do you know why the background setting isn't working ?
mike_hornbeck
ok, found it myself in the docs.
mike_hornbeck
From Java doc: "Setting the background color in the Graphics2D context only affects the subsequent clearRect calls and not the background color of the Component. To change the background of the Component, use appropriate methods of the Component."
Petar Minchev
+1  A: 

The painting code in your Eyes panel is incorrect:

 public void paint(Graphics g) { 
        super.paintComponent(g); 

should be:

 public void paintComponent(Graphics g) { 
        super.paintComponent(g); 
camickr