• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scale displayed Image via JCheckBox

 
Eli Kook
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I need help with Scale displayed Image via JCheckBox:
It;s a project as home work (in German and I suck in German),Its a code that I need to change so that when I have a photo already uploaded/ displayed as I press the CheckBox "Anpassen" the photo will be scaled.
So far only before I upload the photo and as I marked the JCheckBox, the photo will be uploaded already scaled but if I do not mark the CheckBox before it will not scale the photo and that is what I need to change.
The code is messy and confusing but that is how I received the assignment (I don't understand some parts of it).
Please be patient with me, my knowledge is limited as I am a student.
What is it that I need to add to the code so that the listener will trigger the scale? in public class Bildbetrachter extends JFrame{
if (e.getSource() instanceof JCheckBox){
bildAnzeigeSchau.grafikLaden(nameOfTheLastDatei, skalieren.isSelected());

I thank you for every tip or advice and I will appreciate it that when you give me any information, example code will be very helpful and useful to my understaning.
Here is my code in 3 parts:





 
Campbell Ritchie
Marshal
Posts: 52559
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Various things:
Maybe your filter class can be simplified. You can write this sort of thingDon't write a listener. Write one listener for each action. You can add a listener to a check box; look in the Java® Tutorials to find out what sort of event a check box produces. If it is an action event, you can add an action listener. If not you will have to use an item listener and in the changed state method, simply use an if‑else to decide whether to scale the image or not.
 
Eli Kook
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello and thank you for your answer
I can see what you mean in a case of if else

But what I need is that ones I click the JCheckBox it is immediately scaling the displayed photo.
That is my problem I do not know how to do that.
 
Darryl Burke
Bartender
Posts: 5154
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eli Ben wrote:But what I need is that ones I click the JCheckBox it is immediately scaling the displayed photo.
That is my problem I do not know how to do that.


Campbell Ritchie wrote:You can add a listener to a check box; look in the Java® Tutorials to find out what sort of event a check box produces.
 
Eli Kook
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its not really helping me, I can also just google it but there is a reason why I post my question here so that I can get hopefully some explanation or some example of a code.
 
Piet Souris
Rancher
Posts: 1631
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Eli,

well, looking at the tutorials is always useful, so I wouldn't say that the supplied links
are not helping.

But if I give the code a quick scan, then I think the code is already working.

What is happening is:

you have a class called 'MeinListener' (line 40/41) that contains, among other,
a listener for your checkbox. In line 115/116 you add this listener to your checkbox.

Now, what happens when the checkbox is being clicked is: (see line 67)
the method 'grafikLaden()' from the bildAnzeigeSchau is invoked, with
two parameters: the name of the last file, and a boolean that indicated if
the checkbox is set or not. Strangely enough, that line is currently
commented out. I'd say: put that line back into action.

If you look at the 'BildPanel' class, you see that in the method 'grafikLaden'
the boolean parameter is checked. If it found to be true, then the size (width
and height) of the image are scaled such that the aspect ratio of the
scaled image and of the panel in which it gets drawn match.

So for instance, say the panel is 400 X 400 pixels, while the image is
800 * 600. Then the image is scaled to 400 X 300 and then displayed.
If the JCheckbox is not selected, then (I'm not sure here, but try it) then
either the image is drawn at the full size of 800 x 600 (so that you will
only see a part of it), or it will be scaled to 400 x 400, so that you get
a distortion.

Greetz,
Piet

 
Campbell Ritchie
Marshal
Posts: 52559
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case the panel the image is shown on should be wrapped in a scroll pane. Again, you can read about scroll panes in the tutorials.
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. never to block an EDT by Thread.sleep(showImage * 1000); //must double the number mili second, in Java7 and never verson nothing will be repainted in the screen, use Swing Timer exclusivelly, - more in Oracle tutorial Concurency in Swing

2. use ItemListener for JCheckBox //What do I need to change or add here? - more in Oracle tutorial

3. use CardLayout instead of private void bilderSchau() {

4. whats reason(s) for, because paiunting is done in paintComponent by invoking JPanel.repaint(after image is loaded)

//Get the graphic context
Graphics grafikKontext = this.getGraphics();

//Clear the display
grafikKontext.clearRect(0, 0, this.getSize().width, this.getSize().height);

//The method drawImage to show the new output scaled image
grafikKontext.drawImage(bild, 0, 0, (int) neueBildBreite, (int) neueBildHoehe, this);

5. override getPreferredSize for public class BildPanel extends JPanel{, then g.drawImage(bild, 0, 0, (int) neueBildBreite, (int) neueBildHoehe, this); will be getHeight/Weight instead of hunting for (int) neueBildBreite, (int) neueBildHoehe

___________________________________________________

6. create an JFrame and JDialog(modal true, only one JDialog, reuse for another image by setVisible(true/flase)) for showing an Image with JLabel inside JScrollPane, don't forget to call flush() before ImageIcon/Icon is added to JLabel.setIcon(), - more in Oracle tutorials How to use Labels and Icon
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!