• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do i change the size of my drawing using a button?

 
N Pretorius
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm drawing flowers and I need the 'head' to double in size when I press the 'fertilize' button and then to reset to their original size when
I press the 'reset size' button. I'm using Netbeans. Nothing I've tried so far has worked sooo....help?



Code for Panel:



code for Frame:


 
Winston Gutkowski
Bartender
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Pretorius wrote:I'm drawing flowers and I need the 'head' to double in size when I press the 'fertilize' button and then to reset to their original size when I press the 'reset size' button.

Well, if the image uses vector graphics, it can probably be done with a simple method call; otherwise, it will probably require a
scaling filter of some sort, and there are literally dozens of those.

The problem that you're likely to have is that with "lossy" image types like jpeg (and possibly even straight bitmaps) is that the
resizing process will introduce "noise", so even if you return it to its original size, it won't be exactly the same. Also: many filters
work better for reducing (eg, Lanczos) or for increasing (eg, Mitchell).
You can, of course, alleviate this by keeping a copy of the original.

However, image resizing is quite complex, so don't expect this to be a simple "click and resize" exercise.

PS: Please DontWriteLongLines. It makes your thread very difficult to read.
I'd break yours up myself, but you have tons of them, so I suggest you do it yourself.

Winston

[Edit] Oops, my apologies. I was assuming that your "flowers" were something like a GIF. Listen to Piet, he speaketh sense.
I leave my reply for anyone who might be dealing with proper images (eg, jpegs).
 
Piet Souris
Rancher
Posts: 1625
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi N,

you have the member 'size' to determine the size of the flowers.
In line 170, you double the value of it, so just do a repaint() at line 170 and a half.

To reset the size, you need to put an ActionListener to your 'Reset Size' button, in
the same way that you add ActionListeners to your other buttons.
In that ActionListener, just put 'size = 40' and a repaint() command.

Greetings,
Piet
 
N Pretorius
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I drew the flowers using fillOval, fillRect ect.
Isn't there a way I can just multiply the petals with 2? Like - "size = size * 2;" or something along those lines?
 
N Pretorius
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet,

I already tried the repaint(); (tried it again just now to be sure) and it still doesn't do anything.

N
 
Winston Gutkowski
Bartender
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Pretorius wrote:I drew the flowers using fillOval, fillRect ect.
Isn't there a way I can just multiply the petals with 2? Like - "size = size * 2;" or something along those lines?

I think Piet's already covered that. But don't forget that you may also have to change the position of each
component relative to its neighbours; otherwise you may get some odd-looking results.

Winston

PS: Please read the bit in my post about shortening lines, otherwise you may not get too many responses.
 
Piet Souris
Rancher
Posts: 1625
35
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm... I will try to run the code, see if I can find what's going wrong. So, bear with me.

Greetings,
Piet
 
Piet Souris
Rancher
Posts: 1625
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found it, and as always, it was a slight mistake, but one that keeps you looking for
hours!

The culprit is in line 25: it says:

So, it is overriding whatever value our 'member' size has. Remove this line.

I would advise you to set an initial value to your 'Amount', because you now
start with an empty pot, and clicking on 'grow' gives a fatal error.

And finally: the fertilize and reset button will work now, but there is some other
thingy to repair. Try it and see! (it has to do with something that Winston already talked about!)

Greetings,
Piet

 
N Pretorius
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I removed the line mentioned, thanks

"I would advise you to set an initial value to your 'Amount'"

How do I do this? (amount = 1;?)
 
Piet Souris
Rancher
Posts: 1625
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For instance, yes.
You would then also set the text of the JTextField 'txtamount' to this value, such as:


It is a bit fiddly to set 'amount' to the value that the user may input
into the textfield, though. Do you know how to code that?

Anyway, first thing to do is to get the flowers into their correct position,
after the resizing. Maybe

is a bit steep; a factor of 1.1 might do a little better.

Greetings,
Piet
 
Bernhard Goetz
Ranch Hand
Posts: 57
1
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Crosspost?
 
N Pretorius
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:
It is a bit fiddly to set 'amount' to the value that the user may input
into the textfield, though. Do you know how to code that?


No? Sorry, I'm not sure what you mean.


a factor of 1.1 might do a little better.


I get an error when I change it to 1.1. I have to change it to a double, yeah? But where do I 'declare' that?

Sorry, everything still looks like "thingy's" and "majigers" to me so I apologize for any incorrect lingo.
 
Piet Souris
Rancher
Posts: 1625
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, my mistake.
Well, every time you click the fertilize button, you do 'size = size * 2' (line 174).
That means that after 3 clicks, your flowers will be 8 times as big and run rapidly out of
the screen. Therefore I suggested to use a foctor of 1.1 instead of 2.
But I forgot the fact that you then get an error, because you put a double to an int
without using an explicit cast.
So the line shoud be:

You would then see your flowers fertilize at some lower speed.
But the point was to get the placing correct, after resizing the flowers.

And with respect to the textfield:
the only time you read that typed-in value is when you click the 'grow'
button (see the ActionListener of that button). The way you do it is in principle correct.
So it would be wise to change the text of the grow buttin to something more meaningful,
like 'set number of flowers' or simply 'set'.

Sorry, everything still looks like "thingy's" and "majigers" to me so I apologize for any incorrect lingo.

Don't know wat 'majigers' are (certinly hope they're not ugly words, this site IS a decent site )
but you got your code working, so I would say"Well done, Mission Completed!".

Greetz,
Piet
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!