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 6 of 6

Thread: Fill in rectangle partially (clip?¿)

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Fill in rectangle partially (clip?¿) SOLVED

    Hello, I've searched the forum and I couldn't find any answers so here's my problem:

    I have to partially fill in a rectangle, i'll express the idea using O's:

    OOOOO
    O-O- O
    OOOOO
    O----O

    (-= white "squares/areas")

    more or less, basically I leave out certain white areas here's my code:


    import javax.swing.*;
    import java.awt.*;
     
    public class Drawing{
      public static void main(String[] args) {
        Drawing d = new Drawing();
      }
     
      public Color(){
        JFrame frame = new JFrame("faces");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MyComponent());
        frame.setSize(800,600);
        frame.setVisible(true);  
      }
     
      public class MyComponent extends JComponent{
        public void paint(Graphics g){
          int height;
          int width;
          g.clipRect(1,1,50,40);
          g.setColor(Color.black);
          g.fillRect(1,1,10,40);
          g.fillRect(2,1,50,10);
          //g.fillRect(6,2,50,20);
        }
      }
    }

    the two g.fillRect lines that are NOT commented out fill in the first collumn and first row respectively, however I cant manage to fill a different row (3rd)....
    (and individual squares within a given row column)

    Any ideas?

    pd g.clipRect(1,1,50,40) making the start coords 0,0 doesnt seem to make any difference...
    Last edited by OBLITERATOR; March 7th, 2010 at 09:40 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Fill in rectangle partially (clip?¿)

    What you can do is use fillRect to fill in a bounding box, and then use clearRect to remove the bits you don't want.

    You can also "adjust" the drawing location by using the create() method of the Graphics.

    Graphics clipped = g.create(1,1,50,40);

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Fill in rectangle partially (clip?¿)

    Code comments:
    1. Always construct, display and update Swing components on the EDT. Further reading: Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    2. For Swing components, the method to override is paintComponent(...), not paint(...). Further reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    There are several valid approaches to solving your problem, none of which are exceedingly sophisticated. That you have asked the question here strongly suggests that the code you posted is not your own.

    db

  4. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fill in rectangle partially (clip?¿)

    The idea of me posting here is because I have just started with Java, i.e. I needed help, however I have solved my issue and the error was that I was using wrong calculations to colour in the squares, as my squares are 10 x 10 the coordinates should be multiples of 10, not (1,2) fo example. But yeah, thanks anyways

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fill in rectangle partially (clip?¿)

    Hello Daryll sir I am really impressed by your answers can you kindly help through mails.Please reply me

  6. #6
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Fill in rectangle partially (clip?¿)

    No, Vinay, I'm not interested in helping anyone through mail. That would defeat the purpose of a forum, which is to share problems and their possible solutions.

    db
    Last edited by Darryl.Burke; March 27th, 2010 at 01:40 PM.

Similar Threads

  1. JSP partially renders sometimes
    By lisalu22 in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: February 23rd, 2010, 12:00 PM
  2. centering a label inside a rectangle
    By Brain_Child in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2009, 09:08 AM
  3. Fill array concurrently
    By mamba in forum Collections and Generics
    Replies: 2
    Last Post: October 15th, 2009, 07:08 PM
  4. How do I fix my program so I can use the keyboard to type in the numbers?
    By rocafella5007 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 29th, 2009, 02:39 PM
  5. Dropping to graphic element dragged from JList
    By tua1 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 29th, 2008, 08:22 AM