paint() in java with no display












0














import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;




class game extends JFrame {
public game(){ //this is constructor
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(500,500);
frame.setTitle("Hello world");
}

public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
Line2D line = new Line2D.Double(60,90,150,100);
g2.draw(line);


}


public static void main(String args) {

game l = new game();

}


}


The above code is compiling in java but on running the code it only displays the frame and its title, but does not include any of the lines being drawn using the Graphics2D and Line2D, what is the mistake that is being made??? The frame being displayed does not show any content, why is that???










share|improve this question









New contributor




vasu sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • BTW, where is the paint() ??
    – Common Man
    19 hours ago
















0














import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;




class game extends JFrame {
public game(){ //this is constructor
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(500,500);
frame.setTitle("Hello world");
}

public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
Line2D line = new Line2D.Double(60,90,150,100);
g2.draw(line);


}


public static void main(String args) {

game l = new game();

}


}


The above code is compiling in java but on running the code it only displays the frame and its title, but does not include any of the lines being drawn using the Graphics2D and Line2D, what is the mistake that is being made??? The frame being displayed does not show any content, why is that???










share|improve this question









New contributor




vasu sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • BTW, where is the paint() ??
    – Common Man
    19 hours ago














0












0








0







import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;




class game extends JFrame {
public game(){ //this is constructor
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(500,500);
frame.setTitle("Hello world");
}

public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
Line2D line = new Line2D.Double(60,90,150,100);
g2.draw(line);


}


public static void main(String args) {

game l = new game();

}


}


The above code is compiling in java but on running the code it only displays the frame and its title, but does not include any of the lines being drawn using the Graphics2D and Line2D, what is the mistake that is being made??? The frame being displayed does not show any content, why is that???










share|improve this question









New contributor




vasu sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;




class game extends JFrame {
public game(){ //this is constructor
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(500,500);
frame.setTitle("Hello world");
}

public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
Line2D line = new Line2D.Double(60,90,150,100);
g2.draw(line);


}


public static void main(String args) {

game l = new game();

}


}


The above code is compiling in java but on running the code it only displays the frame and its title, but does not include any of the lines being drawn using the Graphics2D and Line2D, what is the mistake that is being made??? The frame being displayed does not show any content, why is that???







java swing jframe paint graphics2d






share|improve this question









New contributor




vasu sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




vasu sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 12 hours ago









daedsidog

1,321724




1,321724






New contributor




vasu sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 20 hours ago









vasu sharma

31




31




New contributor




vasu sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





vasu sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






vasu sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • BTW, where is the paint() ??
    – Common Man
    19 hours ago


















  • BTW, where is the paint() ??
    – Common Man
    19 hours ago
















BTW, where is the paint() ??
– Common Man
19 hours ago




BTW, where is the paint() ??
– Common Man
19 hours ago












3 Answers
3






active

oldest

votes


















3














First, you are creating and displaying a JFrame which is not an instance of game, so there is no chance that it paints what you have in the paint method of game .



You usually don't want to create a subclass of JFrame for custom painting anyway, just create a subclass of JPanel, and set it as the content pane of the frame.



Also don't override paint, but paintComponent, which is the method responsible for painting the current component.



You should also call the parent method of paintComponent, to make sure that all the usual cleaning takes place correctly.



Also by convention, class names should start with an upper case letter.



One last thing, make the frame visible only once you have added all your components, or you may encounter visual glitches some day.



Putting it all together :



import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

class Game extends JPanel {

@Override
public void paintComponent(final Graphics g) {

super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
Line2D line = new Line2D.Double(60, 90, 150, 100);
g2.draw(line);

}

public static void main(final String args) {

Game l = new Game();

JFrame frame = new JFrame();

frame.setSize(500, 500);
frame.setTitle("Hello world");

frame.setContentPane(l);

frame.setVisible(true);

}

}





share|improve this answer































    0














    In your constructor call method of JFrame class using this keyword because you extends JFrame class in your class.



    public game(){   //this is constructor
    /*JFrame frame = new JFrame();
    frame.setVisible(true);
    frame.setSize(500,500);
    frame.setTitle("Hello world");*/
    this.setVisible(true);
    this.setSize(500,500);
    this.setTitle("Hello world");
    }


    This solve your problem.






    share|improve this answer





























      0














      you dont need to create instance of JFrame class ,
      modified you constructor as shown below



      public game(){   //this is constructor
      setVisible(true);
      setSize(500,500);
      setTitle("Hello world");
      }





      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
        });


        }
        });






        vasu sharma is a new contributor. Be nice, and check out our Code of Conduct.










        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53942993%2fpaint-in-java-with-no-display%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














        First, you are creating and displaying a JFrame which is not an instance of game, so there is no chance that it paints what you have in the paint method of game .



        You usually don't want to create a subclass of JFrame for custom painting anyway, just create a subclass of JPanel, and set it as the content pane of the frame.



        Also don't override paint, but paintComponent, which is the method responsible for painting the current component.



        You should also call the parent method of paintComponent, to make sure that all the usual cleaning takes place correctly.



        Also by convention, class names should start with an upper case letter.



        One last thing, make the frame visible only once you have added all your components, or you may encounter visual glitches some day.



        Putting it all together :



        import java.awt.Color;
        import java.awt.Graphics;
        import java.awt.Graphics2D;
        import java.awt.geom.Line2D;

        import javax.swing.JFrame;
        import javax.swing.JPanel;

        class Game extends JPanel {

        @Override
        public void paintComponent(final Graphics g) {

        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        Line2D line = new Line2D.Double(60, 90, 150, 100);
        g2.draw(line);

        }

        public static void main(final String args) {

        Game l = new Game();

        JFrame frame = new JFrame();

        frame.setSize(500, 500);
        frame.setTitle("Hello world");

        frame.setContentPane(l);

        frame.setVisible(true);

        }

        }





        share|improve this answer




























          3














          First, you are creating and displaying a JFrame which is not an instance of game, so there is no chance that it paints what you have in the paint method of game .



          You usually don't want to create a subclass of JFrame for custom painting anyway, just create a subclass of JPanel, and set it as the content pane of the frame.



          Also don't override paint, but paintComponent, which is the method responsible for painting the current component.



          You should also call the parent method of paintComponent, to make sure that all the usual cleaning takes place correctly.



          Also by convention, class names should start with an upper case letter.



          One last thing, make the frame visible only once you have added all your components, or you may encounter visual glitches some day.



          Putting it all together :



          import java.awt.Color;
          import java.awt.Graphics;
          import java.awt.Graphics2D;
          import java.awt.geom.Line2D;

          import javax.swing.JFrame;
          import javax.swing.JPanel;

          class Game extends JPanel {

          @Override
          public void paintComponent(final Graphics g) {

          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D) g;
          g2.setColor(Color.BLACK);
          Line2D line = new Line2D.Double(60, 90, 150, 100);
          g2.draw(line);

          }

          public static void main(final String args) {

          Game l = new Game();

          JFrame frame = new JFrame();

          frame.setSize(500, 500);
          frame.setTitle("Hello world");

          frame.setContentPane(l);

          frame.setVisible(true);

          }

          }





          share|improve this answer


























            3












            3








            3






            First, you are creating and displaying a JFrame which is not an instance of game, so there is no chance that it paints what you have in the paint method of game .



            You usually don't want to create a subclass of JFrame for custom painting anyway, just create a subclass of JPanel, and set it as the content pane of the frame.



            Also don't override paint, but paintComponent, which is the method responsible for painting the current component.



            You should also call the parent method of paintComponent, to make sure that all the usual cleaning takes place correctly.



            Also by convention, class names should start with an upper case letter.



            One last thing, make the frame visible only once you have added all your components, or you may encounter visual glitches some day.



            Putting it all together :



            import java.awt.Color;
            import java.awt.Graphics;
            import java.awt.Graphics2D;
            import java.awt.geom.Line2D;

            import javax.swing.JFrame;
            import javax.swing.JPanel;

            class Game extends JPanel {

            @Override
            public void paintComponent(final Graphics g) {

            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.BLACK);
            Line2D line = new Line2D.Double(60, 90, 150, 100);
            g2.draw(line);

            }

            public static void main(final String args) {

            Game l = new Game();

            JFrame frame = new JFrame();

            frame.setSize(500, 500);
            frame.setTitle("Hello world");

            frame.setContentPane(l);

            frame.setVisible(true);

            }

            }





            share|improve this answer














            First, you are creating and displaying a JFrame which is not an instance of game, so there is no chance that it paints what you have in the paint method of game .



            You usually don't want to create a subclass of JFrame for custom painting anyway, just create a subclass of JPanel, and set it as the content pane of the frame.



            Also don't override paint, but paintComponent, which is the method responsible for painting the current component.



            You should also call the parent method of paintComponent, to make sure that all the usual cleaning takes place correctly.



            Also by convention, class names should start with an upper case letter.



            One last thing, make the frame visible only once you have added all your components, or you may encounter visual glitches some day.



            Putting it all together :



            import java.awt.Color;
            import java.awt.Graphics;
            import java.awt.Graphics2D;
            import java.awt.geom.Line2D;

            import javax.swing.JFrame;
            import javax.swing.JPanel;

            class Game extends JPanel {

            @Override
            public void paintComponent(final Graphics g) {

            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.BLACK);
            Line2D line = new Line2D.Double(60, 90, 150, 100);
            g2.draw(line);

            }

            public static void main(final String args) {

            Game l = new Game();

            JFrame frame = new JFrame();

            frame.setSize(500, 500);
            frame.setTitle("Hello world");

            frame.setContentPane(l);

            frame.setVisible(true);

            }

            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 19 hours ago

























            answered 19 hours ago









            Arnaud

            13.3k21730




            13.3k21730

























                0














                In your constructor call method of JFrame class using this keyword because you extends JFrame class in your class.



                public game(){   //this is constructor
                /*JFrame frame = new JFrame();
                frame.setVisible(true);
                frame.setSize(500,500);
                frame.setTitle("Hello world");*/
                this.setVisible(true);
                this.setSize(500,500);
                this.setTitle("Hello world");
                }


                This solve your problem.






                share|improve this answer


























                  0














                  In your constructor call method of JFrame class using this keyword because you extends JFrame class in your class.



                  public game(){   //this is constructor
                  /*JFrame frame = new JFrame();
                  frame.setVisible(true);
                  frame.setSize(500,500);
                  frame.setTitle("Hello world");*/
                  this.setVisible(true);
                  this.setSize(500,500);
                  this.setTitle("Hello world");
                  }


                  This solve your problem.






                  share|improve this answer
























                    0












                    0








                    0






                    In your constructor call method of JFrame class using this keyword because you extends JFrame class in your class.



                    public game(){   //this is constructor
                    /*JFrame frame = new JFrame();
                    frame.setVisible(true);
                    frame.setSize(500,500);
                    frame.setTitle("Hello world");*/
                    this.setVisible(true);
                    this.setSize(500,500);
                    this.setTitle("Hello world");
                    }


                    This solve your problem.






                    share|improve this answer












                    In your constructor call method of JFrame class using this keyword because you extends JFrame class in your class.



                    public game(){   //this is constructor
                    /*JFrame frame = new JFrame();
                    frame.setVisible(true);
                    frame.setSize(500,500);
                    frame.setTitle("Hello world");*/
                    this.setVisible(true);
                    this.setSize(500,500);
                    this.setTitle("Hello world");
                    }


                    This solve your problem.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 19 hours ago









                    Mostch Romi

                    357214




                    357214























                        0














                        you dont need to create instance of JFrame class ,
                        modified you constructor as shown below



                        public game(){   //this is constructor
                        setVisible(true);
                        setSize(500,500);
                        setTitle("Hello world");
                        }





                        share|improve this answer


























                          0














                          you dont need to create instance of JFrame class ,
                          modified you constructor as shown below



                          public game(){   //this is constructor
                          setVisible(true);
                          setSize(500,500);
                          setTitle("Hello world");
                          }





                          share|improve this answer
























                            0












                            0








                            0






                            you dont need to create instance of JFrame class ,
                            modified you constructor as shown below



                            public game(){   //this is constructor
                            setVisible(true);
                            setSize(500,500);
                            setTitle("Hello world");
                            }





                            share|improve this answer












                            you dont need to create instance of JFrame class ,
                            modified you constructor as shown below



                            public game(){   //this is constructor
                            setVisible(true);
                            setSize(500,500);
                            setTitle("Hello world");
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 19 hours ago









                            CRR

                            1018




                            1018






















                                vasu sharma is a new contributor. Be nice, and check out our Code of Conduct.










                                draft saved

                                draft discarded


















                                vasu sharma is a new contributor. Be nice, and check out our Code of Conduct.













                                vasu sharma is a new contributor. Be nice, and check out our Code of Conduct.












                                vasu sharma is a new contributor. Be nice, and check out our Code of Conduct.
















                                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.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • 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%2f53942993%2fpaint-in-java-with-no-display%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

                                Monofisismo

                                Angular Downloading a file using contenturl with Basic Authentication

                                Olmecas