Results 1 to 4 of 4
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    307
    Rep Power
    2

    Default Question on creating instances

    Guys I wanted to know, in a swing/awt program, does awt call the paint method when you are running the program? Is this correct? Is it true that you just have to make an instance to call the constructor, and awt will take over the rest? *I so hope the answer to this is yes*
    Last edited by MW130; 01-14-2013 at 04:16 AM.

  2. #2
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    307
    Rep Power
    2

    Default Re: Question on creating instances

    guys, I get everything except how the code knows to run. Im so confused as to this. I have another code to show to see if maybe you can explain how these methods are being called? Thanks!

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    class Rect{
    public static void main(String[] args) {
    Rect draw = new Rect();
    }
    public Rect() {
    JFrame frame = new JFrame("Drawing with Alph!");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new MyComp());
    frame.setSize(500,500);
    frame.setVisible(true);
    }
    public class MyComp extends JComponent{
    public void paint(Graphics mw) {
    int length = 200;
    int width = 130;
    mw.setColor(Color.red);
    mw.drawRect(20,30,length,width);
    mw.fillRect(20,30,length,width);
    mw.setColor(Color.red);
    mw.drawOval(250,100, length, width);
    mw.setColor(Color.cyan);
    mw.fillOval(249, 99, length, width);
    }
    }
    }

  3. #3
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    307
    Rep Power
    2

    Default Re: Question on creating instances

    Guys, I still don't get how these methods are being invoked. I wrote another code, and I still can't seem to figure out how the code is being invoked, or called... Can someone please point me towards where I can learn this or explain it to me thanks guys heres another prog if it matters :P.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    class Rect {
    	public static void main(String[] args) {
    		Rect draw = new Rect();
    	}
    
    	public Rect() {
    		JFrame frame = new JFrame("Drawing with Alph!");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().add(new MyComp());
    		frame.setSize(500, 500);
    		frame.setVisible(true);
    	}
    
    	public class MyComp extends JComponent {
    		public void paint(Graphics mw) {
    			int length = 200;
    			int width = 130;
    			mw.setColor(Color.red);
    			mw.drawRect(20, 30, length, width);
    			mw.fillRect(20, 30, length, width);
    			mw.setColor(Color.red);
    			mw.drawOval(250, 100, length, width);
    			mw.setColor(Color.cyan);
    			mw.fillOval(249, 99, length, width);
    		}
    	}
    }

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Question on creating instances

    I think you're right about the painting. Swing code will ensure that the method gets called at the appropriate time. This is something your application won't know about because it might involve other things happening on the desktop - like the user dragging a window over your app or whatever. Usually you call the method paintComponent() rather than paint(). paint() will call paintComponent() but may do other things, so paintComponent() is usually what we use.

    Basically there are two ways you will get an instance (1) call a constructor or (2) call some method of some class that returns an instance. So you may have expressed it backwards: you don't need an instance to call a constructor, but you will often find yourself calling a constructor to get an instance.

Similar Threads

  1. Creating new instances in a loop problems
    By monkeyhead in forum New To Java
    Replies: 6
    Last Post: 11-18-2011, 09:14 PM
  2. Replies: 6
    Last Post: 09-12-2011, 02:27 PM
  3. Question Creating a .jar
    By waterpolobro in forum New To Java
    Replies: 1
    Last Post: 05-18-2011, 08:15 PM
  4. Creating new instances of a Match over and over again
    By Che_Is_Alive in forum Advanced Java
    Replies: 2
    Last Post: 11-19-2009, 06:05 PM
  5. Object Reflection: Creating new instances
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •