Need suggestions on scaling an image and making it transparent

View previous topic View next topic Go down

Java Java: Iftekhar's image scaling problem

Post by BIT0102-Mohaimin on Fri Oct 22, 2010 12:38 am

Iftekhar (2nd batch) called me few minutes ago asking for solution to a problem. I would request him to post the problems in forum so that everyone can share.

Whatever, his problem was about scaling images. Its quite simple task. You need to learn basic of painting. Just make sure you have understood Painting in swing components, part 1










import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ScaledImagePane extends JPanel{
   private Image img;
   private int top, left, height, width;
   private boolean autoScale;
   private final int border = 10;
   
   public ScaledImagePane(String path, int top, int left, int height, int width) {
      ImageIcon icon = new ImageIcon(path);
      img = icon.getImage();
      this.top = top;
      this.left  = left;
      this.height = height;
      this.width = width;
      
      autoScale = false;      
      setBorder(BorderFactory.createLineBorder(Color.RED, border));
   }
   
   public ScaledImagePane(String path) {
      ImageIcon icon = new ImageIcon(path);
      img = icon.getImage();
      
      top = 0;
      left = 0;
      autoScale = true;
      setBorder(BorderFactory.createLineBorder(Color.RED, border));
   }
   
   public void setImg(Image img) {
      this.img = img;
   }
   
   public Image getImg() {
      return img;
   }
   
   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      
      if (autoScale) {
         height = getHeight();
         width = getWidth();
      }
      
      g.drawImage(img, top + border, left + border, width, height, this);
   }
   
   public static void main(String[] args) {
      JFrame imageWindow = new JFrame("Test Images");
      imageWindow.setLayout(new GridLayout(22));
      
      String path = "/home/rokon/Desktop/myself.jpg";
      
      ScaledImagePane sip1 = new ScaledImagePane(path, 0020100);
      ScaledImagePane sip2 = new ScaledImagePane(path, 304010020);
      ScaledImagePane sip3 = new ScaledImagePane(path, 0209060);
      ScaledImagePane sip4 = new ScaledImagePane(path);
      
      imageWindow.add(sip1);
      imageWindow.add(sip2);
      imageWindow.add(sip3);
      imageWindow.add(sip4);
      
      imageWindow.setSize(300300);
      imageWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      imageWindow.setVisible(true);
   }
}




enjoy a screen-shot now



I have added no comments in the code. If you fail to understand something, please leave a post here. Actually I am a bit busy at this moment. I beg your pardon for an incomplete post.


Last edited by bit0112-rokon on Fri Oct 22, 2010 7:45 am; edited 2 times in total (Reason for editing : syntax higlighter used)
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Java Re: Need suggestions on scaling an image and making it transparent

Post by BIT0112-Rokon on Fri Oct 22, 2010 7:47 am

nice work
rep++

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Java Re: Need suggestions on scaling an image and making it transparent

Post by BIT0102-Mohaimin on Fri Oct 22, 2010 1:15 pm

Hei... Rokon, nice Very Happy
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Java Re: Need suggestions on scaling an image and making it transparent

Post by BIT0220-Iftekhar on Fri Oct 22, 2010 1:53 pm

thanks to both Mohaimin bhai and Rokon bhai for this helpful code on image scaling.
rep++
but currently I need to make the image transparent so that the graphics behind the image is visible
I would also request Mohaimin bhai to move this post to my new post in this section where I requested for help on this topic. This was as u suggested.
avatar
BIT0220-Iftekhar
Administrator-RC

Course(s) :
  • BIT

Blood Group : B+
Posts : 434
Points : 630

View user profile http://iftekharunplugged.blogspot.com

Back to top Go down

Java Need suggestions on scaling an image and making it transparent

Post by BIT0220-Iftekhar on Fri Oct 22, 2010 1:56 pm

I need some suggestions on how to scale an image to fit any frame,panel or other graphics component and also make the images' background transparent.


Last edited by bit0220-Iftekhar on Fri Oct 22, 2010 1:57 pm; edited 1 time in total (Reason for editing : spelling mistake)
avatar
BIT0220-Iftekhar
Administrator-RC

Course(s) :
  • BIT

Blood Group : B+
Posts : 434
Points : 630

View user profile http://iftekharunplugged.blogspot.com

Back to top Go down

Java Re: Need suggestions on scaling an image and making it transparent

Post by BIT0102-Mohaimin on Fri Oct 22, 2010 3:10 pm

Topics merged.
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Java Re: Need suggestions on scaling an image and making it transparent

Post by BIT0102-Mohaimin on Fri Oct 22, 2010 3:18 pm

I cannot tell something straight about transparency at this moment. But I can suggest you to go through Herbert Schilt's Complete Reference, Chapters of Image and AWT.
There is something like setXORmode() defined by Graphics. I am not sure what it does. You can google that thing. I will try to find something tomorrow.
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Java Re: Need suggestions on scaling an image and making it transparent

Post by BIT0220-Iftekhar on Fri Oct 22, 2010 6:10 pm

I searched google for the problem but didn't find anything convincing at all.
But I will search Herbert Schildt right now. silent
avatar
BIT0220-Iftekhar
Administrator-RC

Course(s) :
  • BIT

Blood Group : B+
Posts : 434
Points : 630

View user profile http://iftekharunplugged.blogspot.com

Back to top Go down

Java Re: Need suggestions on scaling an image and making it transparent

Post by BIT0122-Amit on Fri Oct 22, 2010 8:57 pm

Erm, this is off topic, and I didn't want to do this, but lets do this anyway.

Rokon, I appreciate your effort at making the code look better. And except the mismatching background between code and forum, it does look good.

But there is a slight problem.

Google is apparently disliking it.
Click on the image link to see larger image.



I am not telling you to stop using this type of color code, but I can not encourage it as well. And now I am in dilemma.
So I will leave the choice of using this type of syntax highlighting to you guys.

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Java Re: Need suggestions on scaling an image and making it transparent

Post by BIT0102-Mohaimin on Fri Oct 22, 2010 9:56 pm

I didn't get. What was that image about. That was the code I wrote but what does it mean actually!
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Java Re: Need suggestions on scaling an image and making it transparent

Post by BIT0122-Amit on Fri Oct 22, 2010 10:32 pm

What we are seeing in the topic is the code generated by our browser.
What is shown in that image is the content of your post.
When google sees it, it sees those extra color codes Neutral

Edit:
I appear to be mistaken.
Google is just receiving all of the codes, not the HTML or other codes.
keep it up.

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Java Re: Need suggestions on scaling an image and making it transparent

Post by BIT0102-Mohaimin on Fri Oct 22, 2010 11:01 pm


What we are seeing in the topic is the code generated by our browser.
What is shown in that image is the content of your post.
When google sees it, it sees those extra color codes Neutral

I know it very well.
What did you mean my "Google see".

Edit:
I appear to be mistaken.
Google is just receiving all of the codes, not the HTML or other codes.
keep it up

Dont get it either.


Last edited by BIT0102-Mohaimin on Fri Oct 22, 2010 11:02 pm; edited 1 time in total (Reason for editing : Missed the last line)
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Java Re: Need suggestions on scaling an image and making it transparent

Post by BIT0122-Amit on Fri Oct 22, 2010 11:04 pm

Google actually gets all the new information from this website via this page:

http://iitdu.forumsmotion.com/rss

click on it, you will see what google receives.

And I mentioned somewhere else before, when someone happens to use the & symbol in topic title, google bot messes up everything, and starting from that topic, no new topics are indexed.

So I was fearing that there is a possibility of google messing up from that snippet of code as well, though it seems that it should not mess up.

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Java Re: Need suggestions on scaling an image and making it transparent

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum