Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Printing two rectangles on same Frame in Java

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb Printing two rectangles on same Frame in Java

    Hi All,

    Please help me in getting the both of the rectangels printed on the screen as in my code i am accesing one excel sheet into 2D array and want to print the rectangles equals to number of elements i have in single column in 2D array.

    When i am trying the below code i am getting the latest rectangle printed on the screen prior to this all got missed or roughed i am not sure.

    I want to print these two Rectangels one at 230 and another and 10,10 coordinates.

    Please help me in correcting my code else guide me whats the problem in this.
    //ReadExcel.java
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.util.ArrayList;
    import java.util.List;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
     
    public class ReadExcel 
        {
    	   public static String valueArray[][] = null;
           public static void main(String args[]) throws Exception
             {
        	   FileInputStream excelSheetInput = new FileInputStream("sample.xls");
               POIFSFileSystem myFileSystem = new POIFSFileSystem(excelSheetInput);
               Workbook myWorkBook = new HSSFWorkbook(myFileSystem);
               Sheet sheet = myWorkBook.getSheetAt(0);
               int LastRow=sheet.getLastRowNum();
               Row tempRow = sheet.getRow(0);
               int LastCol=tempRow.getLastCellNum();
               valueArray = new String[LastRow+1][LastCol];
               System.out.println(LastRow);
               System.out.println(LastCol);
               for(int i=0;i<=LastRow;i++)
                  {
                    Row myRow = sheet.getRow(i);
                     for (int j=0;j<LastCol;j++)
                      {
                        Cell myCell = myRow.getCell(j);
                        int type = myCell.getCellType();
                        String value = "";
                        if(type == myCell.CELL_TYPE_STRING) 
                          {
                           value = myCell.getStringCellValue();
                          } 
                        else if(type == myCell.CELL_TYPE_NUMERIC) 
                          {
                    	   value = new Double(myCell.getNumericCellValue()).toString();
                          } 
                           valueArray[i][j] = value;
                           System.out.print(valueArray[i][j]+"\t");
                       } //CLOSING of J LOOP
                     System.out.println();  
               }  // CLOSING of I loop
               JFrame frame = new JFrame();
               frame.setTitle("DrawRect");
               frame.setSize(640, 480);
              // private List<Rect> rectList = new ArrayList<Rect>();
               String Name = valueArray[1][0];
               DrawRectPanel rect =new DrawRectPanel(Name,240,320,280,350);
             frame.add(rect);
               //  Rect r1 = new Rect(100, 100, 100, 100, Color.red);
              // Rect r2 = new Rect(300, 100, 100, 100, Color.blue);
               //r.addRect(r1);
              // r.addRect(r2);
               //frame.add(r);
                String N = valueArray[1][3];
                DrawRectPanel rect1 =new DrawRectPanel(N,10,10,15,15);
              frame.add(rect1);
               frame.setVisible(true);
               /*for(int i=2;i<=2;i++)
               {
               String N = valueArray[i][3];
               System.out.println(N);
               DrawRectPanel rect1 =new DrawRectPanel(N,10,10,15,15);
               frame.add(rect1);
               } */
               frame.setVisible(true);
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             }// CLOSING OF MAIN FUNCTION
           /*class Rect {
        	   int x;
        	   int y;
        	   int height;
        	   int width;
        	   Color color;
        	   Rect(int x, int y, int width, int height, Color color) 
        	   {
        	     this.x = x;
        	     this.y = y;
        	     this.width = width;
        	     this.height = height;
        	     this.color = color;
        	   }
        	    public void draw(Graphics g) 
        	    {
        	     g.setColor(color);
        	     g.fillRect(x, y, width, height);
        	   }
        	  }*/
     
     } //CLOSING OF CLASS
     
     
    // DrawRectPanel.java
     
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.util.ArrayList;
    import java.util.List;
     
    import javax.swing.*;
     
    public class DrawRectPanel extends JPanel {
        String mystring = "";
        int x_cord,y_cord,str_x,str_y;
        //private List<Rect> rectList = new ArrayList<Rect>();
        //DrawRectPanel()
        //{
        //}
        DrawRectPanel(String Name,int x ,int y,int a ,int b) 
         {
    	  mystring=Name;	
    	  x_cord=x;
    	  y_cord=y;
    	  str_x=a;
    	  str_y=b;
         } 
        //public void addRect(Rect rect) 
          //{
            //rectList.add(rect);
          //}
        public void paintComponent(Graphics g) 
    	  {
           super.paintComponent(g);
           g.setColor(Color.blue);
           g.drawRect(x_cord,y_cord, 80, 30);
           g.drawString(mystring, str_x,str_y);
           //repaint();
    	  }
    }
    Thanks alot in advance/


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Printing two rectangles on same Frame in Java

    Can you make a program for testing that compiles, executes and shows the problem that does not require the 3rd party packages: org.apache.poi?
    The displaying of data should not require reading any files. Make a testing program that contains all the data that is needed for doing the display.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    harshendushridhar (April 6th, 2013)

  4. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing two rectangles on same Frame in Java

    okay give me a min

    --- Update ---

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.*;
     
    public class DrawRectPanel extends JPanel {
        String mystring = "";
        int x_cord,y_cord,str_x,str_y;
        DrawRectPanel(String Name,int x ,int y,int a ,int b)
         {
    	  mystring=Name;
    	  x_cord=x;
    	  y_cord=y;
    	  str_x=a;
    	  str_y=b;
         }
     
        public void paintComponent(Graphics g)
    	  {
           super.paintComponent(g);
           g.setColor(Color.blue);
           g.drawRect(x_cord,y_cord, 80, 30);
           g.drawString(mystring, str_x,str_y);
           //repaint();
    	  }
                public static void main (String args[])
                {
                JFrame frame = new JFrame();
                frame.setTitle("DrawRect");
                frame.setSize(640, 480);
     
                String Name = "harsh";
                DrawRectPanel rect =new DrawRectPanel(Name,240,320,280,350);
                frame.add(rect);
     
                String N = "shridhar";
                DrawRectPanel rect1 =new DrawRectPanel(N,10,10,15,15);
                frame.add(rect1);
                frame.setVisible(true);
     
    }
    }


    --- Update ---

    posted the new code. i want to display two rectangles as per the code.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Printing two rectangles on same Frame in Java

    What layout manager does the container use that you are adding the components to?
    How does the layout manager know where to put the components?
    Does the last component that is added replace the components that were added before it?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing two rectangles on same Frame in Java

    HI Norm ,

    Yes the first rectangle getting hide and another one is displaying for which i have used the different coordinates.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Printing two rectangles on same Frame in Java

    Try debugging the code by adding this in the paintComponent() method:
           System.out.println(mystring + " " + getBounds());
    Look at what is printed out.

    i have used the different coordinates.
    The coordinates are relative to the component itself.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing two rectangles on same Frame in Java

    Output.jpg

    sent you the screenshot

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Printing two rectangles on same Frame in Java

    What prints out after you added the statement I posted?
    Copy and paste the console's contents. No images.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing two rectangles on same Frame in Java

    shridhar java.awt.Rectangle[x=0,y=0,width=624,height=444]

    This is what i am getting on console after putting your statement in paintComponent function.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Printing two rectangles on same Frame in Java

    From that you can see that the other component is not being shown in the JFrame. The second component that was added replaced it.


    From post #4:
    What layout manager does the container use that you are adding the components to?
    How does the layout manager know where to put the components?
    Does the last component that is added replace the components that were added before it?

    Try setting the layout manager to a FlowLayout manager.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing two rectangles on same Frame in Java

    with the help of this FlowLayout both of the rectangles gone..... Nothing is there on frame now.
    Can you please help me in compiling this at your end and help me in correcting the code if possible for you please.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Printing two rectangles on same Frame in Java

    What are the messages printed out by the println() method in the paintComponent() method?
    That will give you some information about where the JPanel components are located and how big they are.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Why doesn't the frame viewer print the rectangles?
    By jean28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 22nd, 2012, 02:01 PM
  2. Help With Moving A 2D Array Of Rectangles
    By billg118 in forum Collections and Generics
    Replies: 1
    Last Post: April 22nd, 2012, 02:23 PM
  3. Question on Rectangles
    By parkBENch in forum Java Theory & Questions
    Replies: 1
    Last Post: March 21st, 2012, 11:13 PM
  4. Replies: 1
    Last Post: January 19th, 2012, 03:44 PM
  5. adding rectangles in images
    By mserrao in forum AWT / Java Swing
    Replies: 4
    Last Post: September 1st, 2011, 06:27 AM