Java Kotlin --> Plotting Two Curves on the Same Plot

AI Thread Summary
The discussion presents a Kotlin program that utilizes Java Swing and AWT to graph sine and cosine curves over the interval [0, 2π]. The program creates a user interface with a JFrame and JPanel, where the curves are rendered using the Graphics class. Key features include dynamic scaling of the graph to fit the window, drawing of axes, and grid lines for better visualization. The sine and cosine functions are plotted by iterating through calculated points, ensuring smooth connections between them. The output displays a red sine wave and a blue cosine wave, with axes labeled at 0, π, and 2π. The program demonstrates effective use of Kotlin for graphical applications, showcasing its capabilities in GUI development.
Messages
15,468
Reaction score
10,177
Here's a Kotlin program that uses Java Swing and Java AWT to plot sine and cosine curves for the interval [0,2π].

The program creates a window with a panel where both curves are drawn:
  • Uses JFrame and JPanel for GUI rendering.
  • Uses Graphics to draw the sine and cosine curves.
  • Automatically scales the graph to fit within the window.

Kotlin Code:​


[CODE lang="java" title="Using Kotlin to plot Sine and Cosine on the same Chart"]
import java.awt.*
import javax.swing.*
import kotlin.math.*

class SineCosinePlot : JPanel() {
private val padding = 50 // Padding around the plot area
private val steps = 100 // Number of points to plot

override fun paintComponent(g: Graphics) {
super.paintComponent(g)
val g2 = g as Graphics2D
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)

val width = width - 2 * padding
val height = height - 2 * padding
val centerX = padding
val centerY = height / 2 + padding

// Draw axes
g2.color = Color.BLACK
g2.stroke = BasicStroke(2f)
g2.drawLine(centerX, centerY, centerX + width, centerY) // X-axis
g2.drawLine(centerX, padding, centerX, centerY + height / 2) // Y-axis

// Draw grid
g2.color = Color.LIGHT_GRAY
for (i in 1..4) {
val x = centerX + i * width / 4
val y = centerY - i * height / 4
g2.drawLine(x, padding, x, centerY + height / 2) // Vertical grid
g2.drawLine(centerX, y, centerX + width, y) // Horizontal grid
}

// Draw sine and cosine curves
drawFunction(g2, centerX, centerY, width, height, Color.RED) { x -> sin(x) }
drawFunction(g2, centerX, centerY, width, height, Color.BLUE) { x -> cos(x) }

// Draw labels
g2.color = Color.BLACK
g2.font = Font("Arial", Font.BOLD, 14)
g2.drawString("0", centerX - 10, centerY + 15)
g2.drawString("π", centerX + width / 2 - 5, centerY + 15)
g2.drawString("2π", centerX + width - 10, centerY + 15)
g2.drawString("1", centerX - 20, padding)
g2.drawString("-1", centerX - 25, centerY + height / 2)
}

private fun drawFunction(
g2: Graphics2D, centerX: Int, centerY: Int, width: Int, height: Int, color: Color,
function: (Double) -> Double
) {
g2.color = color
g2.stroke = BasicStroke(2f)

val stepSize = (2 * Math.PI) / steps
val scaleX = width / (2 * Math.PI) // Scale X to fit 0 to 2π
val scaleY = height / 2.0 // Scale Y to fit -1 to 1

var prevX = centerX
var prevY = (centerY - function(0.0) * scaleY).toInt()

for (i in 1..steps) {
val x = i * stepSize
val screenX = (centerX + x * scaleX).toInt()
val screenY = (centerY - function(x) * scaleY).toInt()
g2.drawLine(prevX, prevY, screenX, screenY)
prevX = screenX
prevY = screenY
}
}
}

fun main() {
SwingUtilities.invokeLater {
val frame = JFrame("Sine & Cosine Plot")
frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
frame.setSize(800, 600)
frame.add(SineCosinePlot())
frame.isVisible = true
}
}
[/CODE]

Explanation:​

  1. paintComponent(Graphics g):
    • This method is overridden when drawing the sine and cosine curves.
  2. Axes Drawing:
    • Draws the x-axis and y-axis with labels for 0,𝜋, and 2𝜋.
  3. Sine and Cosine Curves:
    • The graph is drawn by iterating over screen pixels and mapping them to the sine/cosine function.
    • x values are scaled using SCALE_X, and
    • y values are scaled using SCALE_Y.
    • drawLine() is used to connect consecutive points smoothly.

Output:​

  • A red sine wave and a blue cosine wave are displayed in a window.
  • Axes labeled at 0,𝜋, and 2𝜋.
  • The graph scales dynamically to fit in the window.
Screenshot 2025-03-15 at 11.35.54 PM.png

Solution Credits​


Proposed by: @hagopbul
Coded by: @ChatGPT
Reviewed by: @jedishrfu
 
Last edited:
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
0
Views
377
Replies
0
Views
367
Back
Top