Solving Cubic Equations and Animating along Polynomial Curves in Java | Senior Secondary Mathematics
Using Polynomial equations in Java
In this Java polynomial equation tutorial, you'll learn how to
model and animate a moving object along a cubic curve using mathematical formulas.
This hands-on project demonstrates how to solve polynomial equations
and translate them into visual motion-ideal for senior secondary students
learning both math and coding.
Understanding Polynomial and Cubic Equations | Maths Explanation for Java Kids
A polynomial equation expresses a relationship involving powers of a variable.
For a cubic curve, the general form is: y = ax3 + bx2 + cx + d;
Here, a, b, c, and d are constants. Every third-degree polynomial equation
has both a maximum and a minimum point.
These turning points are useful in generating smooth motion when graphing or animating curves with Java.
Figure: Graph of cubic equation and polynomial curve in Java
Deriving the Equation of a Cubic Curve | Maths Explanation for Java Kids
To generate a cubic equation, all we will need are the
maximum and minimum points of the curve.
y = ax3 + bx2 + cx + d ----- (eqn 0)
By differentiating y = ax³ + bx² + cx + d, we get
dy/dx = 3ax² + 2bx + c.
Setting the derivative equal to zero at both the maximum and minimum points allows us
to calculate a, b, c, and d.
dy/dx = yI = 3ax2 + 2bx + c
At maximum point, yI = 0
yI|(x = xmax) = 0
3axmax2 + 2bxmax + c = 0 ----- (eqn 1)
At minimum point, yI = 0
yI|(x = xmin) = 0 ----- (eqn 2)
3axmin2 + 2bxmin + c = 0
Subtracting both derived equations
yI|(x = xmax) -
yI|(x = xmin)
⇒
3a(xmax2 - xmin2)
+ 2b(xmax - xmin) = 0
2b(xmax - xmin) =
-3a(xmax2 - xmin2)
b =
-3a(xmax - xmin)(xmax + xmin)
2(xmax - xmin)
b = -3/2a(xmax + xmin)
Substituting b in (eqn 1)
3axmax2 + 2bxmax + c = 0
3axmax2 +
2(-3a/2)(xmax + xmin)xmax
+ c = 0
3axmax2 -
3axmax(xmax + xmin)
+ c = 0
3axmax2 - 3axmax2
- 3axmaxxmin + c = 0 c = 3axmaxxmin
From the general equation(eqn 0)
y = ax3 + bx2 + cx + d
ymax = axmax3 +
bxmax2 + cxmax + d
Substituting for b & c
⇒ ymax = axmax3 -
3/2a(xmax + xmin)xmax2
+ 3axmaxxminxmax + d
ymax = axmax3 -
3/2axmax3 -
3/2axmax2xmin
+ 3axmax2xmin + d
ymax = 1/2[2axmax3
- 3axmax3 - 3axmax2xmin
+ 6axmax2xmin + 2d]
ymax = 1/2[
-axmax3 +
3axmax2xmin + 2d]
2ymax = -a(xmax -
3axmin)xmax2 + 2d
2d = 2ymax + a(xmax -
3axmin)xmax2 d = ymax + a/2(xmax -
3axmin)xmax2
b = -3/2a(xmax + xmin) c = 3axmaxxmin & d = ymax + a/2(xmax -
3axmin)xmax2
These formulas form the mathematical basis of our Java polynomial solver.
Generating and Animating along a Cubic Polynomial Curve in Java
Once we determine the constants, we can implement a Java cubic equation solver
to animate motion along the curve. The following example shows how to code a
polynomial equation in Java using simple variables and Java canvas graphics.
To animate an object along a polynomial curve, increment x continuously and
compute its corresponding y value using the cubic polynomial equation.
This Java code allows you to visualize the trajectory of a polynomial equation
by plotting the curve dynamically on a Java canvas. The roots of the polynomial equation and
the coefficients determine the shape and symmetry of the curve.
Create a new Java project;
call it Dymetric.
Create 3 new Java classes; File, New.
Call them Facet, PanelsCubicPath and CubicPath.
Type out the adjoining Java code for animating an image body through
the path of a cubic / polynomial curve.
Key Takeaways on Cubic Path Animation in Java
In this tutorial, you learned how to:
Understand and derive cubic polynomial equations
Find coefficients from maximum and minimum points
Implement a polynomial equation solver using Java
Animate an object along a polynomial curve
By combining algebraic reasoning with code, senior secondary students can see
how mathematics powers real-world applications like animation, computer graphics, and game design.
Applications of Polynomial Equations Java Programming and STEM Education
Polynomial equations are used in:
Data modeling and curve fitting
Graphics programming for drawing smooth curves
Physics simulations and motion paths
Machine learning and optimization problems
Learning how to solve polynomial equations in Java provides a strong foundation for both mathematics and computational thinking.
Summary: Visualizing Polynomial Equations in Java
Polynomial equations are powerful tools for generating smooth, curved motion in graphics and animations. In this tutorial,
you've learnt how to solve polynomial equations in Java, understand the mathematics of cubic curves,
and create a simple animation that moves an image body along a polynomial equation path.
This interactive Java polynomial solver visually demonstrates
how mathematical equations can be represented as real motion on a graph.
It's a simple yet powerful example of combining coding and mathematics
for educational purposes.
So! Java Fun Practice Exercise - Animate along Cubic Path
As a fun practice exercise, try modifying the values of xmax, xmin, ymax,
and ymin to observe how they affect the polynomial equation graph. You can also:
Write a function to calculate the roots of the polynomial.
Compare your results with a quadratic equation solver.
Build a reusable polynomial equation solver in Java.
Java Cubic Path Code - Main Class
package dymetric;
public class Dymetric {
public static void main(String[] args) {
Facet lcd = new Facet();
lcd.setVisible(true);
}
}
Java Cubic Path Window Setup - Facet Class
package dymetric;
import java.awt.*; import javax.swing.*;
/**
* This class just creates a display window to attach our canvas to.
*/ public class Facet extends JFrame {
public Container face; public ButtonandCanvasPanels components; public ImageIcon logo;
public Facet() { // Give our window a title super("A window that will hold a Canvas and Button"); // set window start points and dimensions
setSize(780,400); // close the window when told to
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(true); // should this window be resizable?
// It'll be nice to have our own logo (feel free to use yours) logo = new ImageIcon(getClass().getResource("studyingPays.png")); this.setIconImage(logo.getImage());
face = this.getContentPane(); // background colour - may not be necessay since our canvas will have a colour of its own face.setBackground(Color.PINK);
components = new ButtonandCanvasPanels(); // using the default layout manager face.add(components.button_panel, BorderLayout.NORTH); // attach appropriate drawing component face.add(components.canvas_panel, BorderLayout.CENTER);
}
}
public class ButtonandCanvasPanels implements ActionListener { public JPanel button_panel, canvas_panel; public JButton motion_bttn; public CubicPath cub_path;
public ButtonandCanvasPanels() { button_panel = new JPanel(); // pick a background colour button_panel.setBackground(Color.PINK); button_panel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0)); // O my; but for convenience sake let's add our control button here motion_bttn = new JButton("Move"); motion_bttn.setBackground(new Color(255, 0, 255)); motion_bttn.addActionListener(this); // using the default layout manager button_panel.add(motion_bttn);
canvas_panel = new JPanel(); canvas_panel.setLayout(new BorderLayout()); cub_path = new CubicPath(); // attach appropriate drawing component canvas_panel.add(cub_path, BorderLayout.CENTER);
}
/**
* Respond to the button click event
*/ public voidactionPerformed(ActionEvent evt) { cub_path.moveCubic();
}
}
public CubicPath() {
setBackground(Color.LIGHT_GRAY); // canvas color ball_colour = Color.RED; aWIDTH = aHEIGHT = 10;
// constants a = (double) (-2 * (y_max - y_min)) / Math.pow((x_max - x_min), 3); b = -((double) 3 / 2) * a * (x_max + x_min); c = 3 * a * x_max * x_min; d = y_max + ((double) a / 2) * (x_max - 3 * x_min) * Math.pow(x_max, 2);
y = (int)Math.round(a * Math.pow(x, 3) + b * Math.pow(x, 2) + c * x + d);
}
// Feel free to double buffer if flickering occurs public voidpaint(Graphics g) {
g.setColor(ball_colour); // draw a dot
g.fillOval(x, y, aWIDTH, aHEIGHT);
}
public voidmoveCubic() { // condition for continuing motion while (x <= 750 && y >= y_max) {
paint(this.getGraphics());
x += 20; y = (int)Math.round(a * Math.pow(x, 3) + b * Math.pow(x, 2) + c * x + d); // introduce a delay between renderings try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
}
}