Add Scale Components
Learn to set the scale values of an image according to user request and on load.
We'll cover the following
Focus listeners
A focus listener can listen for when a field comes into focus or goes out of focus. For example, if a program user types a value into the width field, then moves the cursor out of that field, the field will lose focus and a focus listener on the width field can update the height field to an appropriate value.
Adding scale components to the toolbar
ImageResizer
can now load and save images pretty well. Next, add code to scale and crop the image. First, set up the user interface.
- Add a private double instance variable called
ratio
, initialized to1.0
. - Add private
JTextField
s calledscaleWField
andscaleHField
, each with an initial value of"1"
and a size of 5. - Add private
JTextField
s calledcropXField
,cropYField
,cropWField
, andcropHField
, each with an initial value of“0”
and a size of5
.
...
private ImagePanel imagePanel = new ImagePanel(this);
private File file = new File("/usercode/");
_________ ratio = _________;
_________ scaleWField = _________;
_________ scaleHField = _________;
_________ cropXField = _________;
_________ cropYField = _________;
_________ cropWField = _________;
_________ cropHField = _________;
...
Get hands-on with 1200+ tech skills courses.