Java swing Paint method not being called












0















I've setup this really bare-bones View class to test out the Paint method from java swing. However, I noticed that even though I create an instance of view from another class and keep calling the update method below, the paint method is never executed. EDIT: I added code from main and controller.



import java.awt.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class View extends JPanel {

//Attributes
private static int frameWidth = 500;
private static int frameHeight = 500;
private JFrame frame; // the frame
private JPanel menu;
private JPanel game;
private JPanel summary;


//Constructor
public View(ControlListener controlListener) {
this.frame = new JFrame();
frame.setLayout(new GridLayout(3,1));
frame.addKeyListener(controlListener);
frame.setTitle("MyFrame");
frame.setBackground(Color.blue);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(frameWidth, frameHeight);
frame.getContentPane().setBackground(Color.blue);
frame.setVisible(true);

}

//Methods
public JFrame getFrame() {
return frame;
}

public static int getFrameWidth() {
return frameWidth;
}

public static int getFrameHeight() {
return frameHeight;
}

//frame updater
public void update() {
frame.repaint();

}

@Override
public void paint(Graphics g) {
System.out.println("test");
}


Snippet from main:



public static void main(String args) {
Controller v = new Controller();
v.start();
}


Snippet from controller:



public class Controller {

//Attributes
private Model model;
private View view;


//Constructor
public Controller(){
view = new View(controlListener);

}

//run
public void start(){
run();
}

public void run(){

while(true){
view.update();
}
}









share|improve this question

























  • Mention your class with main method as well.

    – Varun Jain
    Nov 13 '18 at 7:00











  • @VarunJain Gotcha :)

    – the_martian
    Nov 13 '18 at 7:06











  • public void paint(Graphics g) { System.out.println("test"); 1) For any JComponent, the correct method for custom painting is public void paintComponent(Graphics g).. 2) Best practice when doing so is to immediately call the super method to ensure the previous drawing is cleared and the BG and borders correctly painted.

    – Andrew Thompson
    Nov 13 '18 at 8:15











  • Possible Duplicate stackoverflow.com/questions/44269435/…

    – Varun Jain
    Nov 13 '18 at 10:09
















0















I've setup this really bare-bones View class to test out the Paint method from java swing. However, I noticed that even though I create an instance of view from another class and keep calling the update method below, the paint method is never executed. EDIT: I added code from main and controller.



import java.awt.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class View extends JPanel {

//Attributes
private static int frameWidth = 500;
private static int frameHeight = 500;
private JFrame frame; // the frame
private JPanel menu;
private JPanel game;
private JPanel summary;


//Constructor
public View(ControlListener controlListener) {
this.frame = new JFrame();
frame.setLayout(new GridLayout(3,1));
frame.addKeyListener(controlListener);
frame.setTitle("MyFrame");
frame.setBackground(Color.blue);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(frameWidth, frameHeight);
frame.getContentPane().setBackground(Color.blue);
frame.setVisible(true);

}

//Methods
public JFrame getFrame() {
return frame;
}

public static int getFrameWidth() {
return frameWidth;
}

public static int getFrameHeight() {
return frameHeight;
}

//frame updater
public void update() {
frame.repaint();

}

@Override
public void paint(Graphics g) {
System.out.println("test");
}


Snippet from main:



public static void main(String args) {
Controller v = new Controller();
v.start();
}


Snippet from controller:



public class Controller {

//Attributes
private Model model;
private View view;


//Constructor
public Controller(){
view = new View(controlListener);

}

//run
public void start(){
run();
}

public void run(){

while(true){
view.update();
}
}









share|improve this question

























  • Mention your class with main method as well.

    – Varun Jain
    Nov 13 '18 at 7:00











  • @VarunJain Gotcha :)

    – the_martian
    Nov 13 '18 at 7:06











  • public void paint(Graphics g) { System.out.println("test"); 1) For any JComponent, the correct method for custom painting is public void paintComponent(Graphics g).. 2) Best practice when doing so is to immediately call the super method to ensure the previous drawing is cleared and the BG and borders correctly painted.

    – Andrew Thompson
    Nov 13 '18 at 8:15











  • Possible Duplicate stackoverflow.com/questions/44269435/…

    – Varun Jain
    Nov 13 '18 at 10:09














0












0








0








I've setup this really bare-bones View class to test out the Paint method from java swing. However, I noticed that even though I create an instance of view from another class and keep calling the update method below, the paint method is never executed. EDIT: I added code from main and controller.



import java.awt.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class View extends JPanel {

//Attributes
private static int frameWidth = 500;
private static int frameHeight = 500;
private JFrame frame; // the frame
private JPanel menu;
private JPanel game;
private JPanel summary;


//Constructor
public View(ControlListener controlListener) {
this.frame = new JFrame();
frame.setLayout(new GridLayout(3,1));
frame.addKeyListener(controlListener);
frame.setTitle("MyFrame");
frame.setBackground(Color.blue);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(frameWidth, frameHeight);
frame.getContentPane().setBackground(Color.blue);
frame.setVisible(true);

}

//Methods
public JFrame getFrame() {
return frame;
}

public static int getFrameWidth() {
return frameWidth;
}

public static int getFrameHeight() {
return frameHeight;
}

//frame updater
public void update() {
frame.repaint();

}

@Override
public void paint(Graphics g) {
System.out.println("test");
}


Snippet from main:



public static void main(String args) {
Controller v = new Controller();
v.start();
}


Snippet from controller:



public class Controller {

//Attributes
private Model model;
private View view;


//Constructor
public Controller(){
view = new View(controlListener);

}

//run
public void start(){
run();
}

public void run(){

while(true){
view.update();
}
}









share|improve this question
















I've setup this really bare-bones View class to test out the Paint method from java swing. However, I noticed that even though I create an instance of view from another class and keep calling the update method below, the paint method is never executed. EDIT: I added code from main and controller.



import java.awt.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class View extends JPanel {

//Attributes
private static int frameWidth = 500;
private static int frameHeight = 500;
private JFrame frame; // the frame
private JPanel menu;
private JPanel game;
private JPanel summary;


//Constructor
public View(ControlListener controlListener) {
this.frame = new JFrame();
frame.setLayout(new GridLayout(3,1));
frame.addKeyListener(controlListener);
frame.setTitle("MyFrame");
frame.setBackground(Color.blue);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(frameWidth, frameHeight);
frame.getContentPane().setBackground(Color.blue);
frame.setVisible(true);

}

//Methods
public JFrame getFrame() {
return frame;
}

public static int getFrameWidth() {
return frameWidth;
}

public static int getFrameHeight() {
return frameHeight;
}

//frame updater
public void update() {
frame.repaint();

}

@Override
public void paint(Graphics g) {
System.out.println("test");
}


Snippet from main:



public static void main(String args) {
Controller v = new Controller();
v.start();
}


Snippet from controller:



public class Controller {

//Attributes
private Model model;
private View view;


//Constructor
public Controller(){
view = new View(controlListener);

}

//run
public void start(){
run();
}

public void run(){

while(true){
view.update();
}
}






java swing user-interface






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 7:06







the_martian

















asked Nov 13 '18 at 6:49









the_martianthe_martian

329113




329113













  • Mention your class with main method as well.

    – Varun Jain
    Nov 13 '18 at 7:00











  • @VarunJain Gotcha :)

    – the_martian
    Nov 13 '18 at 7:06











  • public void paint(Graphics g) { System.out.println("test"); 1) For any JComponent, the correct method for custom painting is public void paintComponent(Graphics g).. 2) Best practice when doing so is to immediately call the super method to ensure the previous drawing is cleared and the BG and borders correctly painted.

    – Andrew Thompson
    Nov 13 '18 at 8:15











  • Possible Duplicate stackoverflow.com/questions/44269435/…

    – Varun Jain
    Nov 13 '18 at 10:09



















  • Mention your class with main method as well.

    – Varun Jain
    Nov 13 '18 at 7:00











  • @VarunJain Gotcha :)

    – the_martian
    Nov 13 '18 at 7:06











  • public void paint(Graphics g) { System.out.println("test"); 1) For any JComponent, the correct method for custom painting is public void paintComponent(Graphics g).. 2) Best practice when doing so is to immediately call the super method to ensure the previous drawing is cleared and the BG and borders correctly painted.

    – Andrew Thompson
    Nov 13 '18 at 8:15











  • Possible Duplicate stackoverflow.com/questions/44269435/…

    – Varun Jain
    Nov 13 '18 at 10:09

















Mention your class with main method as well.

– Varun Jain
Nov 13 '18 at 7:00





Mention your class with main method as well.

– Varun Jain
Nov 13 '18 at 7:00













@VarunJain Gotcha :)

– the_martian
Nov 13 '18 at 7:06





@VarunJain Gotcha :)

– the_martian
Nov 13 '18 at 7:06













public void paint(Graphics g) { System.out.println("test"); 1) For any JComponent, the correct method for custom painting is public void paintComponent(Graphics g).. 2) Best practice when doing so is to immediately call the super method to ensure the previous drawing is cleared and the BG and borders correctly painted.

– Andrew Thompson
Nov 13 '18 at 8:15





public void paint(Graphics g) { System.out.println("test"); 1) For any JComponent, the correct method for custom painting is public void paintComponent(Graphics g).. 2) Best practice when doing so is to immediately call the super method to ensure the previous drawing is cleared and the BG and borders correctly painted.

– Andrew Thompson
Nov 13 '18 at 8:15













Possible Duplicate stackoverflow.com/questions/44269435/…

– Varun Jain
Nov 13 '18 at 10:09





Possible Duplicate stackoverflow.com/questions/44269435/…

– Varun Jain
Nov 13 '18 at 10:09












1 Answer
1






active

oldest

votes


















1














There is never an instance of View added to the JFrame, so Swing has no reason to paint an invisible component.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275327%2fjava-swing-paint-method-not-being-called%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    There is never an instance of View added to the JFrame, so Swing has no reason to paint an invisible component.






    share|improve this answer




























      1














      There is never an instance of View added to the JFrame, so Swing has no reason to paint an invisible component.






      share|improve this answer


























        1












        1








        1







        There is never an instance of View added to the JFrame, so Swing has no reason to paint an invisible component.






        share|improve this answer













        There is never an instance of View added to the JFrame, so Swing has no reason to paint an invisible component.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 7:20









        SurfManSurfMan

        863511




        863511






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275327%2fjava-swing-paint-method-not-being-called%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues