Please add SCALING FUNTIONALITY when the Scale Button is pressed. As stated the
ID: 3683828 • Letter: P
Question
Please add SCALING FUNTIONALITY when the Scale Button is pressed. As stated the instructions.. Show an image that the code runs successfully
Please code the following using Java
Please add SCALING FUNTIONALITY when the Scale Button is pressed. As stated the instructions.
SHOW AN IMAGE THAT THE CODE RUNS SUCCESSFULLY
package ellipsedemo2d;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;
public class EllipseDemo2D extends JApplet {
final static BasicStroke stroke = new BasicStroke(2.0f);
JButton btnScale;
double x = 100;
double y = 100;
@Override
public void init() {
setBackground(Color.white);
setForeground(Color.white);
System.out.println("Init Invoked:");
}
@Override
public void paint(Graphics g) {
System.out.println("Paint Invoked:");
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.gray);
g2.setStroke(stroke);
g2.draw(new Ellipse2D.Double(x, y, 200, 200));
}
public void Scale()
{
JFrame f = new JFrame("");
btnScale=new JButton("Scale");
System.out.println("Scale method Invoked:");
JTextField txtx=new JTextField();
JTextField txty=new JTextField();
JLabel lblx=new JLabel("Scale factor for x:");
JLabel lbly=new JLabel("Y:");
txtx.setBounds(190, 390, 50, 30);
txty.setBounds(260, 390, 50, 30);
lbly.setBounds(240, 390, 30, 30);
lblx.setBounds(50, 390, 130, 30);
btnScale.setBounds(320, 390, 100, 30);
f.add(btnScale);
f.add(txtx);
f.add(txty);
f.add(lblx);
f.add(lbly);
btnScale.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked:");
x=Double.parseDouble(txtx.getText());
y=Double.parseDouble(txty.getText());
System.out.println(x);
System.out.println(y);
}
});
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JApplet applet = new EllipseDemo2D();
f.getContentPane().add("Center", applet);
applet.init();
f.pack();
f.setSize(new Dimension(550, 500));
f.show();
}
public static void main(String s[]) {
EllipseDemo2D ed=new EllipseDemo2D();
ed.Scale();
}
}
Explanation / Answer
Note: this code has been extracted from surrounding logic, which will be explored later.
Composing transforms refers to stacking transforms to be applied. The last transform fired against the AffineTransformobject is the first to be applied--a LIFO stack. In general, the order of these transformations is important to the outcome. Usually, the effect of applying transform T1 and then transform T2, is not the same as applying T2 first, then T1.
In our case the key operations that are sensitive to this ordering are translate, then scale. The translate transform adjusts the user space so that when the scaling takes place, the ellipse remains centered. Since rotation is done about the ellipse center point (xCent, yCent), the rotation can be done before or after this translate/scale pair.
The translation constant (defined earlier) is presented below again. This value, as an argument to the translate method, moves the user space so that the ellipse will appear centered after each scaling operation.
As before, a shape object is created by the following syntax:
The following code turns off the drawing functionality if the scaling calculations produce results that are wildly outside the viewable area. The scaleFactacc variable is an accumulator indicating passes through the scaling operation.
afn.scale( scaleFact, scaleFact ); afn.translate( -xTransCon , -yTransCon ); afn.rotate( Math.toRadians( degreeIncrement)*direction, xCent, yCent ); elpTrans = afn.createTransformedShape( elp ); g2d.draw( elpTrans ); if ( scaleFactacc > 300.0 || scaleFactacc < .001 ) { drawing = false; stopAnimation(); } Composing transforms refers to stacking transforms to be applied. The last transform fired against the AffineTransformobject is the first to be applied--a LIFO stack. In general, the order of these transformations is important to the outcome. Usually, the effect of applying transform T1 and then transform T2, is not the same as applying T2 first, then T1.
In our case the key operations that are sensitive to this ordering are translate, then scale. The translate transform adjusts the user space so that when the scaling takes place, the ellipse remains centered. Since rotation is done about the ellipse center point (xCent, yCent), the rotation can be done before or after this translate/scale pair.
The translation constant (defined earlier) is presented below again. This value, as an argument to the translate method, moves the user space so that the ellipse will appear centered after each scaling operation.
xTransCon = ( xCent*scaleFact - xCent )/scaleFact; yTransCon = ( yCent*scaleFact - yCent )/scaleFact;
As before, a shape object is created by the following syntax:
elpTrans = afn.createTransformedShape( elp );
g2d.draw( elpTrans );
The following code turns off the drawing functionality if the scaling calculations produce results that are wildly outside the viewable area. The scaleFactacc variable is an accumulator indicating passes through the scaling operation.
if ( scaleFactacc > 300.0 || scaleFactacc < .001 ) { drawing = false; stopAnimation(); } Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.