0% found this document useful (2 votes)
73 views

Spirograph

This Java code defines a Spirograph class that extends JApplet and contains a main method to launch the applet in a JFrame. It initializes the applet and adds it to the JFrame. It also defines an inner SpiroPanel class that extends JPanel and overrides the paintComponent method to draw a Spirograph pattern by calculating x and y coordinates using trigonometric functions in a for loop.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (2 votes)
73 views

Spirograph

This Java code defines a Spirograph class that extends JApplet and contains a main method to launch the applet in a JFrame. It initializes the applet and adds it to the JFrame. It also defines an inner SpiroPanel class that extends JPanel and overrides the paintComponent method to draw a Spirograph pattern by calculating x and y coordinates using trigonometric functions in a for loop.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//package chapter2;

import java.awt.*;
import javax.swing.*;
public class Spirograph extends JApplet {
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String s[]) {
JFrame frame = new JFrame();
frame.setTitle("Spirograph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new Spirograph();
applet.init();
frame.getContentPane().add(applet);
frame.pack();
frame.setVisible(true);
}
public void init() {
JPanel panel = new SpiroPanel();
getContentPane().add(panel);
}
}
class SpiroPanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
int nPoints = 1000;
double r1 = 60;
double r2 = 50;
double p = 70;
public SpiroPanel() {
setPreferredSize(new Dimension(400, 400));
setBackground(Color.white);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.translate(200,200);
int x1=(int)(r1+r2-p);
int y1=0;
int x2;
int y2;
for (int i=0; i<nPoints; i++) {
double t = i*Math.PI/90;
x2 = (int)((r1+r2)*Math.cos(t)-p*Math.cos((r1+r2)*t/r2));
y2 = (int)((r1+r2)*Math.sin(t)-p*Math.sin((r1+r2)*t/r2));
g2.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
}
}

You might also like