In this lab you write one last graphical Swing component that displays a Lissajous curve on its surface, so that the user can control the
In this lab you write one last graphical Swing component that displays a Lissajous curve on its surface, so that the user can control the parameters a, b and delta that define the shape of this curve by entering their values into three JTextField components inside this component. Write a class Lissajous that extends JPanel, and the following methods in it:
public Lissajous(int size)
The constructor that sets the preferred size of this component to be size-by-size pixels. Then, three instances of JTextField are created and added inside this component. Initialize these text fields with values 2, 3 and 0.5. Add an ActionListener to each of the three text fields whose method actionPerformed simply calls repaint for this component.
@Override public void paintComponent(Graphics g)
Renders the Lissajous curve on the component surface, using the values for a, b and delta that it reads from the previous three text fields. This method should consist of a for-loop whose loop counter double t goes through the values from 0 to (a + b) * Math.PI using some suitably small increment. In the body of the loop, compute the coordinates x and y of the current point using the formulas
x = size/2 + 2*size/5 * Math.sin(a * t + delta);
y = size/2 + 2*size/5 * Math.cos(b * t);
and draw a line segment from the current point to the previous point.
Again, to get to admire your Lissajous curve and try out the effect of the values of a, b and delta on the shape of the curve as if you were inside the lair of a mad scientist in some 1970's dystopian science fiction movie, create a separate class LissajousMain whose main method creates a JFrame that contains your Lissajous component. The end result might look like this:
Advanced and motivated students can take on as an extra challenge to make the displayed image look smoother by eliminating some visual jagginess, and possibly even make this rendering look more artistic in other ways. (For example, instead of subdividing the curve into line segments, subdivide it into smoother cubic curves that continue seamlessly where there previous one left off...)
Lissajous demo a 5 b 2 delta 0.7
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started