Home > Net >  The method setColor(Color) is undefined for the type JFrame
The method setColor(Color) is undefined for the type JFrame

Time:05-07

Error code shown below :

The method setColor(Color) is undefined for the type JFrame

package task3;

class Execute {
    
    public static void main(String[] args) throws Exception {
        
        Controller c = new Controller();    
        c.simulate();
        
    }
}

class Model {
    
    int l; //line
    int c; //column
    int[] loc = {0,0}; //location
    JFrame frame;
    int m_width;
    int m_height;
    int R = (int)(Math.random()*256);
    int G = (int)(Math.random()*256); 
    int B = (int)(Math.random()*256);
    
    public int getRectWidth() {
        return frame.getContentPane().getWidth() / c;
    }
    public int getRectHeight() {
        return frame.getContentPane().getHeight() / l;
    }
    
    
    Model(int width, int height, int line, int column, JFrame w) {
        m_width = width;
        m_height = height;
        l = line;
        c = column;
        frame = w;
    }
}
class View extends JComponent {
    
    private Model m_Mod;

    View(Model mod) {
        m_Mod = mod;
    }
    
    
    @Override
    protected void paintComponent(Graphics g) {
        
        super.paintComponent(g);
        for(int i=0; i<m_Mod.l;   i) {
            for(int j=0; j<m_Mod.c;   j) {
                g.fillRect(m_Mod.loc[0], m_Mod.loc[1], m_Mod.getRectWidth(), m_Mod.getRectHeight());
                m_Mod.loc[0] = m_Mod.loc[0]   m_Mod.getRectWidth()   2;
            }
            m_Mod.loc[0] = 0;
            m_Mod.loc[1] = m_Mod.loc[1]   m_Mod.getRectHeight()   2;
        }
        m_Mod.loc[1] = 0;
    }
}
class Controller extends JFrame{
    
    private Model m_Mod;
    private View m_View;
    
    
        Controller(){
                
                JFrame window = new JFrame();
                Color color = new Color(m_Mod.R, m_Mod.G, m_Mod.B);
                m_Mod.frame.setColor(color); 

// Error code here : The method setColor(Color) is undefined for the type JFrame


                m_Mod = new Model(500,500,3,8, window);
                m_View = new View(m_Mod);
                window.add(m_View);
                window.setSize(m_Mod.m_width,m_Mod.m_height);
                window.setLocationRelativeTo(null);
                window.setVisible(true);
                window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
        }
        void simulate(){
            //m_View.repaint();
        }
        
}

If I comment the Color section out the program works perfectly. Any ideas? Furthermore the setColor() method is in the Java Frame class,no? So why is it not working? Some sites tell me to update my library

CodePudding user response:

Furthermore the setColor() method is in the Java Frame class,no?

Did you read the API? That is the first step you do when attempting to solve a problem.

You can't set the color of a JFrame. You can set the foreground/background of components added to the frame.

Typically you would use the setBackground() on the panel that you add to the frame:

m_View = new View(m_Mod);
m_View.setBackground(...); // added
window.add(m_View);

However this won't work because you are extending JComponent which doesn't paint the background by default.

If you want to extend JComponent then you need to add painting code:

super.paintComponent( g );

g.setColor( getBackground() );
g.fillRect(0, 0, getWidth(), getHeight());

g.setColor( getForeground() ); // default color for your custom painting

Or, the easier approach is to just extend JPanel since a JPanel will automatically paint the background with the color you set.

CodePudding user response:

The setColor() method needs to be called inside the paintComponent method. That was my mistake here. Now it works and the rectangles are drawed in color.

  • Related