double angle = Math.PI/4; // angle in radians double cosAngle = Math.cos(angle); System.out.println("Cosine of "+angle+" is "+cosAngle);
Cosine of 0.7853981633974483 is 0.7071067811865476
public void paint(Graphics g) { int w = getWidth(); int h = getHeight(); g.setColor(Color.BLACK); g.drawLine(0, h/2, w, h/2); // x-axis g.drawLine(w/2, 0, w/2, h); // y-axis g.setColor(Color.BLUE); double x, y, lastX = 0, lastY = h/2; for (int i = 0; i < w; i++) { x = (i - w/2) * 0.1; // angle in radians y = h/2 - 50 * Math.cos(x); g.drawLine((int)lastX, (int)lastY, i, (int)y); lastX = i; lastY = y; } }This example draws a cosine curve to the screen using the Graphics class in java.awt. The cosine function is used to calculate the height of the curve at each point along the x-axis.