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

Thread: adding rectangles in images

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Location
    Arnhem, Netherlands
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question adding rectangles in images

    hey guys, I'm new to Java, and I need to highlight a part of an image with a rectangle, but I don't know if it is possible to modify a jpg picture this way. I found out on the internet that I could add a rectangle to an image and store it something called TexturePaint. I tried to put it on my JPanel, but it didn't compile. Anyone knows a simple way to display a TexturePaint or even better, how to add a simple rectangle in an image??


    Rectangle Rect = new Rectangle(60, 70, image.getWidth(), image.getHeight());
    TexturePaint TextureAux = new TexturePaint(image, Rect);

    JPanel panel = new JPanel ();
    panel.add(new JLabel(new ImageIcon(image)));
    panel.add(new JLabel(new TexturePaintIcon(TextureAux))); //---> THIS DOESN'T COMPILE!
    JFrame frame = new JFrame ("Images");
    frame.getContentPane().add(panel);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: adding rectangles in images

    If you want help, you'll have to provide the actual compiler error, as well as an SSCCE that demonstrates the problem. Don't forget the highlight tags.

    Also, this thread is in the wrong section. I'll move it for you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: adding rectangles in images

    how to add a simple rectangle in an image
    If you mean to display the image with a rectangle on it,
    In a paintComponent method, draw the image first and then draw the rectangle

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

    KevinWorkman (August 31st, 2011)

  5. #4
    Junior Member
    Join Date
    Aug 2011
    Location
    Arnhem, Netherlands
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: adding rectangles in images

    Don't you know where I can find a simple tutorial for paintComponent?

    I ended up modifying the pixels of my images by using 2 fors. Well, I know it's not the best solution, but it worked as well.

    // x and y are the top left corners of the rectangle
    // Xsize and Ysize are respectively, the width and the height of the rectangle
    // ImageDest is the image where I'm adding the blue rectangles

    for (int xi=x; xi<(x+Xsize); xi++) {
    ImageDest.setRGB(xi, y, 255);
    ImageDest.setRGB(xi, y+Ysize, 255);
    }
    for (int yi=y; yi<(y+Ysize); yi++) {
    ImageDest.setRGB(x, yi, 255);
    ImageDest.setRGB(x+Xsize, yi, 255);
    }

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: adding rectangles in images

    There are a lot of code samples on the forum that use the paintComponent method. Try doing a Search here.

Similar Threads

  1. Drawing Rectangles and Lines
    By andreizeus in forum AWT / Java Swing
    Replies: 21
    Last Post: October 28th, 2010, 12:59 PM
  2. How to form a rectangle from the union of two Rectangles.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 3rd, 2010, 07:22 AM
  3. [SOLVED] Help needed with Images.
    By Brt93yoda in forum What's Wrong With My Code?
    Replies: 19
    Last Post: August 16th, 2010, 04:46 PM
  4. Images not going in email
    By anjali09s in forum Java SE APIs
    Replies: 3
    Last Post: August 2nd, 2009, 06:06 PM
  5. What are the best way of placing images in GUI?
    By Ciwan in forum AWT / Java Swing
    Replies: 5
    Last Post: February 26th, 2009, 05:19 PM

Tags for this Thread