Resizing jpg for text box or icon

  • Thread starter Dirk Bruere at NeoPax
  • Start date
K

Knute Johnson

Dirk said:
Is there any method for automatically resizing a jpg image to fit a box
or button?

If you are going to display it in a Component you can scale it with the
Graphics.drawImage() method that takes a size argument. For buttons and
other purposes where you need a simple scaled image,
Image.getScaledInstance() would work just fine.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JPanel {
final Image img;

public test() {
img = Toolkit.getDefaultToolkit().getImage("EOC_1.JPG");
setPreferredSize(new Dimension(400,300));
Image sub = img.getScaledInstance(100,75,Image.SCALE_FAST);
ImageIcon icon = new ImageIcon(sub);
JButton b = new JButton(icon);
add(b);
}

public void paintComponent(Graphics g) {
g.drawImage(img,0,0,getWidth(),getHeight(),this);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test t = new test();
f.add(t,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
}


knute...
 
A

Andrew Thompson

You did that on purpose!

(You got me, too.)

Huhh? I had just finished musing that Knute's
examples would be so much nicer to my eye if they
used class names of the form 'TestThisThing'
(e.g. TestImageScale) rather than the 'test' he
always seems to use.
(And also the image examples would be even more
handy if they used URLs to images directly off
the net.*)

* e.g.
<http://pscode.org/media/#image>
Though that example could also work OK even
with this little icon..
<http://forums.sun.com/im/bronze-star.gif>
 
D

Dirk Bruere at NeoPax

R

RedGrittyBrick

Dirk said:
Thanks for the replies.
On a related issue, if I do some simple graphics programming eg resizing
and moving pics about onscreen (2D), how does Java compare with C# in
terms of execution times?

I don't understand this sort of question. How can anyone who is really
interested in the answer not rush out and try it rather than wait for an
answer from potentially unreliable strangers?

Oh yes, the answer is it is 37.1237% faster, except on Tuesdays.
 
D

Dirk Bruere at NeoPax

RedGrittyBrick said:
I don't understand this sort of question. How can anyone who is really
interested in the answer not rush out and try it rather than wait for an
answer from potentially unreliable strangers?

Because I want to know whether an app I will have to code in future
would better be written in Java or C#. Oh... and I don't have time to
**** around and play little coding games like some bored student.

In fact, I don't know why you bother to post, or read, here at all. You
clearly can't answer a straightforward question, and taking your own
advice why not simply RTFM and stop leaching off other people's knowledge?
Oh yes, the answer is it is 37.1237% faster, except on Tuesdays.

Well, thanks a lot for that.

--
Dirk

http://www.transcendence.me.uk/ - Transcendence UK
http://www.theconsensus.org/ - A UK political party
http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
 
L

Lew

Dirk said:
Because I want to know whether an app I will have to code in future
would better be written in Java or C#. Oh... and I don't have time to
**** around and play little coding games like some bored student.

Well, aren't we Mr. PottyMouth?

If you're too lazy to try something yourself, and too mean to talk to people
in this forum, and too wrapped up in yourself to understand the answers you
get, I guess you aren't going to have much success.

There is no clear answer to your question. Basically, the execution times for
moving graphics around is not different between Java and C#, not that that
really would influence whether it's "better" to use one or the other. There
are many criteria for "better", and no clear way to tell which will be
"faster" for interactive tasks.
In fact, I don't know why you bother to post, or read, here at all. You

Right back at you.
clearly can't answer a straightforward question, and taking your own

There was absolutely nothing straightforward about your question.
advice why not simply RTFM and stop leaching off other people's knowledge?

Excuse us? You are accusing RGB of "leaching off other people's knowledge"
when you clearly have no knowledge yourself, and are trying to "leach off" the
knowledge of this forum because you can't be bothered to try some experiments
yourself, or even formulate a meaningful question?

Wow.
 
A

Andrew Thompson

....
Oh yes, the answer is it is 37.1237% faster, except on Tuesdays.

Bwah-ha-ha. You just made my day. ;-)

Many of your answers (to smart questions) are very much
more erudite and succinct than that one, but that one is
so much funnier (and v. well warranted).
 
J

Joshua Cranmer

Dirk said:
In fact, I don't know why you bother to post, or read, here at all. You
clearly can't answer a straightforward question, and taking your own
advice why not simply RTFM and stop leaching off other people's knowledge?

The simplest questions tend to have hard answers. Fermat's Last Theorem
and the Four Color Theorem took hundreds of years to solve, and both of
them have extremely simple problem statements.
Well, thanks a lot for that.

The answer you were looking for is this: it's faster, slower, and there
is no statistically significant difference in execution time. Yes, all
three at once.

Another way of putting it is that we have no way to answer your
question--you generally can't answer questions of "is ABC faster than
XYZ?" because there are a myriad of factors that could influence the
answer, chiefly that of how you're actually writing the code.
 
D

Dirk Bruere at NeoPax

Lew said:
Well, aren't we Mr. PottyMouth?

If you're too lazy to try something yourself, and too mean to talk to
people in this forum, and too wrapped up in yourself to understand the
answers you get, I guess you aren't going to have much success.

There is no clear answer to your question. Basically, the execution
times for moving graphics around is not different between Java and C#,
not that that really would influence whether it's "better" to use one or
the other. There are many criteria for "better", and no clear way to
tell which will be "faster" for interactive tasks.


Right back at you.


There was absolutely nothing straightforward about your question.


Excuse us? You are accusing RGB of "leaching off other people's
knowledge" when you clearly have no knowledge yourself, and are trying
to "leach off" the knowledge of this forum because you can't be bothered
to try some experiments yourself, or even formulate a meaningful question?

Wow.

I was pointing out his hypocrisy, not to mention his useless answer.
And the reason I'm a bit techy maybe has something to do with working 15
hours a day 7 days a week for the past 4 weeks without a break.

--
Dirk

http://www.transcendence.me.uk/ - Transcendence UK
http://www.theconsensus.org/ - A UK political party
http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
 
A

Andrew Thompson

...
And the reason I'm a bit techy maybe has something to do with working 15
hours a day 7 days a week for the past 4 weeks without a break.

Not our problem, not our concern. Please don't waste
the bandwidth with your (off-topic) irrelevant problems.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Pressed icon behaviour 6
Changing button icon on pressing 9
Official Java Classes 10
Can an Applet beep? 4
Popups and Dialogs 4
Accessing static field 21
ListModel name 10
Sorting a JList 4

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top