Results 1 to 8 of 8
  1. #1
    Ivan is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default How to use no- Layout and paintComponent?

    Hello,

    I am trying to put two dices on panel with no layout using no-layout and paintComponent.I red that when using no-layout you have to use setBounds() method, but in my paintComponent I have already set sizes of my components. Code compiles with no errors but mainP(mainpanel) doesn't show anything.
    When i do the same thing with buttons it works perfectly. Can someone help me please? here is the code:

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


    public class Main {

    public static void main(String[] args) {

    JFrame frame;
    frame = new JFrame("my frame");
    frame.setSize(800,500);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    JPanel mainP = new JPanel();
    mainP.setLayout(null);
    Box1[] boxes = new Box1[3];
    boxes[0]= new Box1(12,(int)(Math.random()*6 +1));
    boxes[1]= new Box1(5,(int)(Math.random()*6 +1));
    mainP.add( boxes[0]);
    mainP.add( boxes[1]);
    boxes[0].setBounds(340, 100,100,100);
    boxes[1].setBounds(500,100,100,100);

    boxes[0].repaint();
    boxes[1].repaint();



    frame.getContentPane().add(BorderLayout.CENTER, mainP);
    frame.validate();





    }
    }

    ----------------------------------------

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



    public class Box1 extends JPanel {

    int i;
    int number;
    public Box1(int i2,int number2) {
    i=i2;
    number=number2;
    }

    public void paintComponent(Graphics g) {

    g.setColor(Color.red);
    g.fillRect(20*i + 100,100,100,100);
    g.setColor(Color.black);
    if(number==1) {
    g.fillOval(20*i+140,140,20,20);
    }
    else if(number==2) {
    g.fillOval(20*i + 110, 170, 20,20);
    g.fillOval(20*i + 170, 110, 20,20);
    }
    else if(number==3) {
    g.fillOval(20*i + 110, 170, 20,20);
    g.fillOval(20*i + 170, 110, 20,20);
    g.fillOval(20*i + 140, 140, 20,20);
    }
    else if(number==4) {
    g.fillOval(20*i + 110, 170, 20,20);
    g.fillOval(20*i + 170, 110, 20,20);
    g.fillOval(20*i + 110, 110, 20,20);
    g.fillOval(20*i + 170, 170, 20,20);
    }
    else if(number==5) {
    g.fillOval(20*i + 110, 170, 20,20);
    g.fillOval(20*i + 170, 110, 20,20);
    g.fillOval(20*i + 110, 110, 20,20);
    g.fillOval(20*i + 170, 170, 20,20);
    g.fillOval(20*i + 140, 140, 20,20);
    }
    else if (number==6) {
    g.fillOval(20*i + 110, 170, 20,20);
    g.fillOval(20*i + 170, 110, 20,20);
    g.fillOval(20*i + 110, 110, 20,20);
    g.fillOval(20*i + 170, 170, 20,20);
    g.fillOval(20*i + 110, 140, 20,20);
    g.fillOval(20*i + 170, 140, 20,20);

    }
    }
    }

    Thanks a lot

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: How to use no- Layout and paintComponent?

    First, please place your code between code tags [code][/code].


    The problem is in your paintComponent method. You are drawing the dice using the wrong coordinate system. Just draw them starting at 0,0. Your ovals are also drawn beyond the size of the square. The placement of the dice can be done using setBounds(). Note: I still recommend using a layout manager.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Ivan is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: How to use no- Layout and paintComponent?

    Quote Originally Posted by jim829 View Post
    First, please place your code between code tags [code][/code].


    The problem is in your paintComponent method. You are drawing the dice using the wrong coordinate system. Just draw them starting at 0,0. Your ovals are also drawn beyond the size of the square. The placement of the dice can be done using setBounds(). Note: I still recommend using a layout manager.

    Regards,
    Jim
    Can you show me that ovals are beyond I can't see that in code? But I don't want them to start at 0,0, I want them to be dependent of parameter "i". I was using the setBounds() and it looks like the program takes only cordinates of paintComponent(), should I use setBounds if I wrote cordinates in paintComponent()?
    Which layout should I use? I would like to do it without layouts because i wrote my own cordinates. I also wrote another version of program with ovals except dices, but Oval is painted somewhere else no matter of cordinates of setBounds() or paintComponent:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    
    	public class Main2 {
    		
    		public static void main(String[] args) {
    			JFrame frame;
    		 	 frame = new JFrame("my frame");
    			frame.setSize(800,500);
    			frame.setVisible(true);
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			JPanel mainP = new JPanel();
    			mainP.setLayout(null);
    			
    
    			Oval oval = new Oval();
    			mainP.add(oval);
    			oval.setBounds(20,20,100,100);
    			oval.repaint();
    				
    			
    			Oval oval2 = new Oval();
    			mainP.add(oval2);
    			oval.setBounds(10,100,100,100);	
    			oval.repaint();
    			
    			
    
    			frame.getContentPane().add(BorderLayout.CENTER, mainP);
    			frame.validate();
    			
    		
    			
    
    			
    			}
     	}
    
    -------------
    import java.awt.*;
    import javax.swing.*;
    public class Oval extends JPanel {
    	public void paintComponent(Graphics g) {
    		g.setColor(Color.red);
    		g.fillOval(0,0,100,100);
    	}
    }
    Thanks,
    Ivan

  4. #4
    Ivan is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: How to use no- Layout and paintComponent?

    Quote Originally Posted by jim829 View Post
    First, please place your code between code tags [code][/code].


    The problem is in your paintComponent method. You are drawing the dice using the wrong coordinate system. Just draw them starting at 0,0. Your ovals are also drawn beyond the size of the square. The placement of the dice can be done using setBounds(). Note: I still recommend using a layout manager.

    Regards,
    Jim
    Can you show me that ovals are beyond I can't see that in code? But I don't want them to start at 0,0, I want them to be dependent of parameter "i". I was using the setBounds() and it looks like the program takes only cordinates of paintComponent(), should I use setBounds if I wrote cordinates in paintComponent()?
    Which layout should I use? I would like to do it without layouts because i wrote my own cordinates. I also wrote another version of program with ovals except dices, but Oval is painted somewhere else no matter of cordinates of setBounds() or paintComponent:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    
    	public class Main2 {
    		
    		public static void main(String[] args) {
    			JFrame frame;
    		 	 frame = new JFrame("my frame");
    			frame.setSize(800,500);
    			frame.setVisible(true);
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			JPanel mainP = new JPanel();
    			mainP.setLayout(null);
    			
    
    			Oval oval = new Oval();
    			mainP.add(oval);
    			oval.setBounds(20,20,100,100);
    			oval.repaint();
    				
    			
    			Oval oval2 = new Oval();
    			mainP.add(oval2);
    			oval.setBounds(10,100,100,100);	
    			oval.repaint();
    			
    			
    
    			frame.getContentPane().add(BorderLayout.CENTER, mainP);
    			frame.validate();
    			
    		
    			
    
    			
    			}
     	}
    
    -------------
    import java.awt.*;
    import javax.swing.*;
    public class Oval extends JPanel {
    	public void paintComponent(Graphics g) {
    		g.setColor(Color.red);
    		g.fillOval(0,0,100,100);
    	}
    }
    Thanks,
    Ivan

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: How to use no- Layout and paintComponent?

    Draw the squares starting at 0,0. To draw the ovals, just subtract 100 from each x and y coordinate. And I am not talking about setBounds for the ovals. That only applies to the Box1 instances. I am talking about fillOval() methods.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    Ivan is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: How to use no- Layout and paintComponent?

    im sorry but i just can't make that thing works, can you writte your version of code with two boxes on panel?

    Thanks a lot,
    Ivan

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: How to use no- Layout and paintComponent?

    You are so close. I will tell you how to fix it but I won't fix it for you. That way, you will remember it better.

    First you must keep in mind one essential thing. Assume you have a JPanel of size 100 x 100. Now say you position that panel at a location 400, 550. Any coordinates you paint too in that panel must not exceed 100 in the x direction and 100 in the y direction. The painting refers to the panel coordinate system, not the containing frame or other panel's coordinate system.

    Start with the original code you first posted.
    So, first, in the Box1 class, get rid of all your 20 * i + values.
    Then, subtract 100 from all your x,y coordinates in your fillOval and fillRect statements.

    So change:

    Java Code:
    g.fillOval(20*i+140,140,20,20);// remember, 140,140 is beyond the bounds of your JPanel of 100 x 100
       // to
    g.fillOval(40,40,20,20); 
    // do it for all your fillOval and fillRect methods.
    Then put
    Java Code:
    super.paintComponent(g)
    as first statement in Box1 paintComponent method.

    If you still have problems, post your altered code and I or someone else will help you.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    Ivan is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: How to use no- Layout and paintComponent?

    It works now ! I was watching the wrong coordinate system all the time. It works even without repaint() and validate(). Thanks!

Similar Threads

  1. Grid Layout change layout alignment of control s
    By rellicott in forum SWT / JFace
    Replies: 1
    Last Post: 02-13-2012, 05:11 PM
  2. paintComponent
    By zock70 in forum AWT / Swing
    Replies: 1
    Last Post: 11-16-2011, 03:19 AM
  3. Help with paintComponent!
    By joeyea in forum Java 2D
    Replies: 6
    Last Post: 12-27-2010, 01:59 PM
  4. Grid Layout with paintComponent
    By mp06677 in forum New To Java
    Replies: 1
    Last Post: 03-06-2010, 01:20 PM
  5. Edit layout Layout please help me
    By manhtungtnk28@gmail.com in forum New To Java
    Replies: 4
    Last Post: 11-23-2009, 08:41 AM

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
  •