0
package com.gautam.notepad;

import javax.swing.*;    

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
public class main {

    public static void main(String[] args) {
        panel1 p =new panel1();  // This is the panel1 class object
        new App("NOTEPAD",p);    // i'm trying to pass panel1 object



    }
}


class App extends JFrame {

    public App(String title,panel1 panel)
    {
        this.setTitle(title);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.setSize(800, 640);
        this.setLayout(new FlowLayout());
        this.add(panel);
        this.setResizable(false);
    }
}

class panel1 extends JPanel{

    public void paintComponent(Graphics g)
    {
        g.setColor(Color.green);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());



    }
}

it works fine but in the paintcomponent method it is g.fillRect() method is not working it is not painting the whole screen only small rectangle is painted in the middle of the screen.what is the problem in this code

2
  • 1
    Well you're missing a call to super.paintComponent(g), but the code is "fine". Its painting a rectangle the size of the JPanel. If you want the rectangle to be bigger, then you may need to investigate why your JPanel is not as big as you want it to be. Try commenting out that setLayout() line so the JFrame will still have its default BorderLayout, see if that makes a difference. Commented Jun 23, 2016 at 8:19
  • Yes, that will fix it. Commented Jun 23, 2016 at 8:30

4 Answers 4

1

You have problem on line this.setLayout(new FlowLayout());, just remove it and it will work. By default it will use BorderLayout which is what you need.

Sign up to request clarification or add additional context in comments.

1 Comment

Great :) You can also remove it since BorderLayout is used by default
0

This will work

import javax.swing.*;    
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
public class NotePad {

public static void main(String[] args) {

    App app = new App("NOTEPAD");  
    app.getContentPane().setBackground(Color.green);


}
}


class App extends JFrame {

public App(String title)
{
    this.setTitle(title);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setVisible(true);
    this.setLocationRelativeTo(null);
    this.setSize(800, 640);
    this.setLayout(new FlowLayout());
    //this.add(panel);
    this.setResizable(false);
}
}

1 Comment

I doesn't want to display it in this way i want to draw a panel and in the panel void paint component method i like to paint the whole screen by the green colour by using the g.fillrect method
0

The problem has to do with your selected Layout.Have a look at this https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html#flow to see the different layouts and how to use them.

If you change this.setLayout(new FlowLayout()); to this.setLayout(new BorderLayout()); for instance, it should work

1 Comment

Please mark your question as answered if one of the suggested answers worked for you
0

Ok Flowlayout() gets it's own size and it happens that that small size is what you're getting from the flowlayout(), if you want to be still able to use the Flowlayout() and control the dimensions of your green Rectangle than i suggest using the setPreferredSize() method on your panel which will eliminate this problem and will keep your FlowLayout too.

public static void main(String[] args) {
        panel1 p =new panel1();  // This is the panel1 class object
        Dimension size= new Dimension(800, 640);//here you can add the size you want 
        p.setPreferredSize(size);
        new App("NOTEPAD",p);    // i'm trying to pass panel1 object



    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.