Results 1 to 5 of 5
  1. #1
    AryanArs is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Unable to Display Two Objects in Frame

    I want to display two rectangle at different position but i cannot ....Any help please?
    New to Java........
    I have to Class Three Class Rect, AnRect and Viewer ...Rect contains Rectangle and AnRect contains another Rect and Viewer to display....But cannot
    Here are Codes
    Java Code:
    Rect Class
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import javax.swing.JComponent;
    class Rects extends JComponent
    {
    	Rectangle rect = new Rectangle(50,80,100,150);
    	private int x;
    	private int y; 
    public  Rects()
    {}
    public void paintComponent(Graphics g)
       {
          Graphics2D g2 = (Graphics2D) g;
          
          g2.draw(rect);
       }
    }
    
    AnRect Class
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    
    import javax.swing.JComponent;
    class AnRect extends JComponent
    {
    	Rectangle rect1 = new Rectangle(150,180,300,550);
    	private int x;
    	private int y; 
    public  AnRect()
    {
    }
    	 public void paintComponent(Graphics g)
       {
          Graphics2D g2 = (Graphics2D) g;
          
          g2.draw(rect1);
       }
    }
    And Viewer as follows
    Java Code:
    import java.awt.BorderLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class Viewer
    {
       public static void main(String[] args)
       {
    	  JFrame frame = new JFrame();
    	      frame.setSize(600, 600);
    	      frame.setTitle("SuperEllipse Viewer");
    	      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	   
    	      Rects component = new Rects();
    	      frame.add(component);
    	     
    	      JPanel panel = new JPanel();
    	   
    	      frame.add(panel,BorderLayout.NORTH);  
    	      JButton rectss = new JButton("Click");
    	      panel.add(rectss);
    	 
    	      AnRect component2 = new AnRect();
    	      frame.add(component2);
    	      JButton rect2 = new JButton("Click");
    	      panel.add(rect2);
    	      frame.setVisible(true);
    	   
       }  
    }
    Last edited by pbrockway2; 02-26-2012 at 07:42 AM. Reason: code tags added

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

    Default Re: Unable to Display Two Objects in Frame

    Hi AryanArs, welcome to the forums!

    When you post code, use the "code" tags. You put [code] at the start of the code and [/code] at the end: that way the code is more readable when it appears here.

    -----

    Why do you have separate classes Rects and AnRect which, unless I have missed something, do exactly the same thing? Better to have a single class and make two instances of it each with the appropriate rectangle dimensions. The job of the constructor would be to initialise the rect of a particular instance.

    You are adding component and component2 to the JFrame instance frame. This means that both are added to the centre of the content pane of that frame, the second effectively replacing the first.

    -----

    Since you are using a frame it would be a good idea to review the content on the How to Make Frames page of Oracle's Tutorial. It has a link ("Adding Components to the Content Pane") which discusses what is involved in calling the add() method of a frame. It also contains code that shows how the frame can be constructed and shown using the event dispatch thread. It is a good idea to use the code there as a "recipe" - the reason is explained more on the Initial Threads page (and following).

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

    Default Re: Unable to Display Two Objects in Frame

    Also moving to the Awt/Swing forum - that's where all the Swing experts are.

  4. #4
    AryanArs is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: Unable to Display Two Objects in Frame

    Thank you Very Much

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

    Default Re: Unable to Display Two Objects in Frame

    You're welcome.

    For anyone interested, this thread seems to have morphed into this one.

Similar Threads

  1. Unable to display gif image
    By rameshiit19 in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 05-09-2011, 11:43 AM
  2. Display filenames in frame
    By cool in forum AWT / Swing
    Replies: 1
    Last Post: 11-19-2010, 03:51 PM
  3. i want to display decimal values in frame window
    By santhosh_el in forum AWT / Swing
    Replies: 4
    Last Post: 03-16-2009, 10:07 AM
  4. Unable to display JDialog from JFrame
    By jv5 in forum NetBeans
    Replies: 2
    Last Post: 02-04-2009, 05:33 AM
  5. I m unable to connect one frame 2 the other
    By ankitraghuvanshi in forum NetBeans
    Replies: 2
    Last Post: 10-23-2008, 03:24 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
  •