Results 1 to 9 of 9
  1. #1
    aliencc is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default how to draw on Jwindow?

    I'm starting to learn how to use GUI and this is my first Problem: The line that i want to draw never show up. What am I doing wrong?

    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import javax.swing.*;
    import java.awt.event.*;
    	 
    	public class exp extends JPanel {
    	   private JWindow window;
               
    	   public exp() {
                window = new JWindow();
                window.setSize(600,350);
                          
         //Create Mouse Listener that listen when someone click on that JWindow  
                MouseListener ml=new MouseAdapter(){  
                public void mousePressed(MouseEvent evt){
                    System.exit(0);}
                };   
                window.addMouseListener(ml); 
                window.setVisible(true);               
                window.add(this);
                this.setVisible(true);
                }	     
    	    public void paint(Graphics g) {
    	        g.drawLine(50,50,100,50);
    	    }	 
    	    public static void main(String[] args) {
                    new exp();
    	      } 
    	}

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Before we spend time analyzing this, let's make sure we're not re-inventing the wheel. Have you cross-posted the same or a similar question elsewhere?

  3. #3
    aliencc is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default

    well yes i just posted this problem in another Forum but i havent got a satisfying aswer, so i just think its better to ask as many people as i can. Sorry about that:P

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by aliencc View Post
    well yes i just posted this problem in another Forum but i havent got a satisfying aswer, so i just think its better to ask as many people as i can. Sorry about that:P
    It's no problem as long as you notify all the folks in your thread at that forum and you thread in this forum. Can you provide a link to the thread?

  5. #5
    aliencc is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    You might want to call setVisible(true) on the JWindow after you've added the JPanel to it.

    Also, I second Darryl's recommendation at JavaRanch to read the Swing graphics tutorials. There you'll learn many things including that you should draw in your JPanel's paintComponent method, not the paint method.

  7. #7
    aliencc is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default

    cool!
    Thank you very much!
    But i really dont understand whats different here when you put setVisible in the last, can you explain that to me?
    btw i always do things in paint method and they do just fine, so why paintComponent?

  8. #8
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by aliencc View Post
    But i really dont understand whats different here when you put setVisible in the last, can you explain that to me?
    When you call setVisible(true) on the top-level container you render the container causing the container and all its children to be drawn. If you render the top-level container before all components have been added, then the late-added components won't be drawn. Simple as that.

    btw i always do things in paint method and they do just fine, so why paintComponent?
    paintComponent is responsible for painting the component that's it. paint is responsible for painting the component, the border and the component's children. So if your painting method doesn't concern itself with painting borders or your component's children, then don't override it or else you risk ruining the component's abilities to pain its borders or children.

  9. #9
    aliencc is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

Similar Threads

  1. How to lock Windows XP desktop using Window/JWindow
    By chyrl in forum Advanced Java
    Replies: 11
    Last Post: 08-19-2010, 09:46 PM
  2. Replies: 9
    Last Post: 05-05-2010, 04:33 PM
  3. Image with JWindow -> trails
    By ofir3dvb in forum AWT / Swing
    Replies: 2
    Last Post: 03-18-2008, 05:21 PM
  4. JWindow/JFrame with boarders
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 11:35 AM
  5. How to mouse-drag a JWindow?
    By cruxblack in forum New To Java
    Replies: 3
    Last Post: 08-06-2007, 09:52 AM

Posting Permissions

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