1

I want to call my first method but it doesn't work. More, I have not the print. But I want to draw and nothing displays.

I edited it depending upon your answers...

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

public class Main extends JPanel {

    private static JPanel pan = new JPanel();
    private static JButton valider = new JButton("Valider");

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g); 
        System.out.println("ok"); //It doesn't display ok
        g.drawRect(10, 10, 50, 50);
        g.setColor(Color.GREEN);
    }


    public static void fenetre() {
        JFrame fenetre = new JFrame("Fenetre");
        fenetre.setVisible(true);
        fenetre.setSize(480,272);
        fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pan.add(valider);
        fenetre.add(pan);
        fenetre.revalidate();
        //fenetre.repaint();
        fenetre.setContentPane(pan);
        fenetre.getContentPane().setBackground(Color.BLUE);

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Lancement du programme");
        fenetre();

    }

}
MatR
  • 15
  • 1
  • 9
  • Is the class that declares that method extending `JPanel`? – Vince Nov 10 '15 at 16:06
  • I have had "public class Main extends JPanel" but nothings happens. There is not the println("ok"); – MatR Nov 10 '15 at 16:09
  • Have a look at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) for details about how painting in Swing/AWT works – MadProgrammer Nov 10 '15 at 22:07

6 Answers6

3

I have had "public class Main extends JPanel" but nothings happens.

Well, you never add the panel to the frame.

For working examples of custom painting, start by reading the section from the Swing tutorial on Custom Painting. It will show you how to override the paintComponent() method, how to override the getPreferredSize() method and how to add the panel to the frame.

The examples in the tutorial will also show you how to better structure your code. So basically get rid of your current code and download the examples from the tutorial and make the changes to that code. I would start with the 3rd example since it is the most complete.

camickr
  • 321,443
  • 19
  • 166
  • 288
2

The problem is calling setVisible before adding your panel. This invalidates the component hierarchy.

From the API docs for invalidate():

This method is called automatically when any layout-related information changes (e.g. setting the bounds of the component, or adding the component to a container).

Simply call setVisible after adding the panel, or call revalidate() (I recommend calling setVisible after adding everything)


pan should be declared as:

JPanel pan = new Main();

Not as JPanel pan = new JPanel(), as you currently have it.

Right now, you are currently using a plain JPanel, rather than an instance of Main, which is the class that contains your paint code.

Vince
  • 14,470
  • 7
  • 39
  • 84
  • Ok i moved setVisible after adding my JPanel, but... nothing called – MatR Nov 10 '15 at 16:26
  • For information, the System.out.println("ok"); is not called – MatR Nov 10 '15 at 16:26
  • @MatR You are adding `pan` to the frame. I do not see you actually adding the panel in question to the frame. You also never show where `pan` is declared. Please update your post with those details please, and I will edit my answer to fit. You should be adding the instance from the class with that paint method. It would also help to know what `valider` is. – Vince Nov 10 '15 at 16:31
2

You defined a paintComponent method in the Main class, but you are not adding any instances of Main to your frame. Writing a subclass of JPanel with a custom paintComponent method does not cause all ordinary JPanels (like JPanel pan = new JPanel()) to have that method. Only instances of your Main class will have it.

In short, change this:

private static JPanel pan = new JPanel();

to this:

private static JPanel pan = new Main();
VGR
  • 40,506
  • 4
  • 48
  • 63
  • 4
    @MatR, Well, I suggested that earlier. Yes, this will fix the problem, but it does nothing for the poor style of coding which continue to cause problems in the future. You should not be using static variable/methods. You should be invoking setVisible() AFTER you add the components to the frame. You should create the GUI on the EDT. You should set the preferred size of the panel so it will work properly with layout managers. There is no shortcut to learning how to use Swing effectively. Read the tutorial and start with the tutorial example!!! – camickr Nov 10 '15 at 17:01
1

I don't understand what you are trying to do but you do not need to call paintComponent yourself at all. It is called automatically when you display the component the method is in.

You need to create a JFrame, put the Component on it, and then show the JFrame. Try some Swing introductory tutorials.

Tim B
  • 40,716
  • 16
  • 83
  • 128
0

You don't call paint directly - see this answer.

You need to @Override the paintComponent method (it doesn't look like you've marked your method as overridden) and then whenever your component (in this case, the your frame's content pane) is repainted by Swing it will automatically call paintComponent.

To trigger this yourself, for whatever reason (i.e. if you're rendering animations), simply call repaint() on the component. I wouldn't overdo this, however.

EDIT: forgot one key part of paintComponent().

public void paintComponent(Graphics g) {
    super.paintComponent(g); // you need to be calling this to ensure the hierarchy is respected
    System.out.println("ok");
    g.drawRect(10, 10, 50, 50);
    g.setColor(Color.GREEN);
}
Community
  • 1
  • 1
Gorbles
  • 1,169
  • 12
  • 27
-1

So, I will show you my edits

package proj1;

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

public class Main extends JPanel {
 
 private static JPanel pan = new JPanel();
 private static JButton valider = new JButton("Valider");
 
 @Override
 public void paintComponent(Graphics g) {
  super.paintComponent(g); 
  System.out.println("ok"); //It doesn't display ok
  g.drawRect(10, 10, 50, 50);
  g.setColor(Color.GREEN);
 }
 
 
 public static void fenetre() {
  JFrame fenetre = new JFrame("Fenetre");
  fenetre.setVisible(true);
  fenetre.setSize(480,272);
  fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  pan.add(valider);
  fenetre.add(pan);
  fenetre.revalidate();
  //fenetre.repaint();
  fenetre.setContentPane(pan);
  fenetre.getContentPane().setBackground(Color.BLUE);
  
 }

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  System.out.println("Lancement du programme");
  fenetre();
  
 }

}
MatR
  • 15
  • 1
  • 9