Welcome to Dream.In.Code
Become a Java Expert!

Join 150,397 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,012 people online right now. Registration is fast and FREE... Join Now!




Quck Explanation on why one program works and the other doesn't.

 
Reply to this topicStart new topic

Quck Explanation on why one program works and the other doesn't.

Rose_B
12 Mar, 2008 - 10:18 PM
Post #1

New D.I.C Head
*

Joined: 12 Mar, 2008
Posts: 4

Hi, it's my first post here..

I have a question

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

public class test  extends JFrame {

    public static void main(String args[]) {  

         new test();  

    }


    public test(){
        setSize(500, 500);
        Toolkit tk=Toolkit.getDefaultToolkit();
        Dimension d=tk.getScreenSize();
        setLocation(d.width/2-this.getWidth()/2, d.height/2-this.getHeight()/2);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    private Toolkit tk=Toolkit.getDefaultToolkit();
    private Image img=tk.getImage("scorpion.jpg");

    public void paint(Graphics g){
        g.drawImage(img, 50, 50, this);
    }
}


Why can't i just put all the contents of public test() into public static void main(String args[])?

here is my attempt


CODE

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

public class Test  extends JFrame {
    
    public static void main(String args[]) {  
        JFrame theFrame = new JFrame();
        
        theFrame.setSize(500, 500);
        Toolkit tk=Toolkit.getDefaultToolkit();
        Dimension d=tk.getScreenSize();
        theFrame.setLocation(d.width/2-theFrame.getWidth()/2, d.height/2-theFrame.getHeight()/2);
        theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theFrame.setVisible(true);
        
        

    }

    private Toolkit tk=Toolkit.getDefaultToolkit();
    private Image img=tk.getImage("werd.jpg");
    
    public void paint(Graphics g){
        g.drawImage(img, 50, 50, this);
    }
}




I ask this because I've been trying to learn to program and I don't understand why the first one works and the second one doesn't.. isn't it basically the same?

This post has been edited by Rose_B: 12 Mar, 2008 - 10:30 PM
User is offlineProfile CardPM
+Quote Post

beef
RE: Quck Explanation On Why One Program Works And The Other Doesn't.
14 Mar, 2008 - 08:42 AM
Post #2

D.I.C Head
Group Icon

Joined: 2 Nov, 2006
Posts: 128


My Contributions
Almost.

Your Test class is extending JFrame. In the first one you are creating a new instance of Test in main and that is what's showing.

In the second one though, you're making an instance of JFrame instead of Test so the Paint method is never getting called.

A simple fix would be to change this:

CODE
JFrame theFrame = new JFrame();


to this:

CODE
Test theFrame = new Test();

User is offlineProfile CardPM
+Quote Post

Rose_B
RE: Quck Explanation On Why One Program Works And The Other Doesn't.
18 Mar, 2008 - 08:33 PM
Post #3

New D.I.C Head
*

Joined: 12 Mar, 2008
Posts: 4

awesome that answers both questions I had! I was also trying to figure out how paint was being called.

Thank you very much!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 06:35PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month