Skip to content

Commit 5ec57bf

Browse files
committed
Add the ability to scale the graph in the viewport
1 parent 5624431 commit 5ec57bf

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

src/main/kotlin/be/ugent/topl/mio/ui/GraphPanel.kt

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import java.awt.RenderingHints
1616
import java.awt.event.MouseEvent
1717
import java.awt.event.MouseListener
1818
import java.awt.event.MouseMotionListener
19+
import java.awt.event.MouseWheelEvent
20+
import java.awt.event.MouseWheelListener
1921
import java.awt.geom.Path2D
2022
import java.awt.image.BufferedImage
2123
import java.io.File
@@ -28,11 +30,12 @@ import javax.swing.UIManager
2830
import kotlin.math.min
2931

3032
class GraphPanel(private val graph: MultiverseGraph) : JPanel(),
31-
MouseListener, MouseMotionListener {
33+
MouseListener, MouseMotionListener, MouseWheelListener {
3234
private var selectionListeners = mutableListOf<() -> Unit>()
3335
init {
3436
addMouseListener(this)
3537
addMouseMotionListener(this)
38+
addMouseWheelListener(this)
3639
}
3740
private val textColour = UIManager.getDefaults().getColor("RadioButton.foreground")
3841
//private val borderColour = Color(125, 125, 125)
@@ -56,15 +59,15 @@ class GraphPanel(private val graph: MultiverseGraph) : JPanel(),
5659
data class Node(val x: Int, val y: Int, val w: Int, val h: Int, val value: MultiverseNode)
5760

5861
override fun getPreferredSize(): Dimension {
59-
return Dimension(renderedWidth, renderedHeight)
62+
return Dimension((renderedWidth * scaleFactor).toInt(), (renderedHeight * scaleFactor).toInt())
6063
}
6164

6265
override fun paintComponent(g: Graphics) {
6366
super.paintComponent(g)
6467
val g2 = g as Graphics2D
6568
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
6669
g2.stroke = BasicStroke(2.0f)
67-
//g2.scale(0.5, 0.5)
70+
g2.scale(scaleFactor, scaleFactor)
6871

6972
drawPaths(g, graph.rootNode)
7073
}
@@ -250,8 +253,10 @@ class GraphPanel(private val graph: MultiverseGraph) : JPanel(),
250253
return
251254
}
252255

256+
val x = e.x/scaleFactor
257+
val y = e.y/scaleFactor
253258
for (node in nodes) {
254-
if (e.x > node.x && e.y > node.y && e.x < node.x + node.w && e.y < node.y + node.h) {
259+
if (x > node.x && y > node.y && x < node.x + node.w && y < node.y + node.h) {
255260
selectedNode = node
256261
}
257262
}
@@ -293,11 +298,29 @@ class GraphPanel(private val graph: MultiverseGraph) : JPanel(),
293298
cursor = Cursor(Cursor.DEFAULT_CURSOR)
294299
var hit = false
295300
for (node in nodes) {
296-
if (e.x > node.x && e.y > node.y && e.x < node.x + node.w && e.y < node.y + node.h) {
301+
val x = e.x/scaleFactor
302+
val y = e.y/scaleFactor
303+
if (x > node.x && y > node.y && x < node.x + node.w && y < node.y + node.h) {
297304
hit = true
298305
break
299306
}
300307
}
301308
cursor = if(hit) Cursor.getPredefinedCursor(Cursor.HAND_CURSOR) else Cursor.getDefaultCursor()
302309
}
310+
311+
private var scaleFactor = 1.0
312+
313+
override fun mouseWheelMoved(e: MouseWheelEvent) {
314+
val oldScaleFactor = scaleFactor
315+
val adjustment = e.wheelRotation / 20.0
316+
scaleFactor += adjustment
317+
scaleFactor = kotlin.math.max(0.1, scaleFactor)
318+
associatedScrollPane?.horizontalScrollBar?.value = ((associatedScrollPane?.horizontalScrollBar?.value!! / oldScaleFactor) * scaleFactor).toInt()
319+
associatedScrollPane?.verticalScrollBar?.value = ((associatedScrollPane?.verticalScrollBar?.value!! / oldScaleFactor) * scaleFactor).toInt()
320+
println("Scale = $scaleFactor")
321+
repaint()
322+
323+
associatedScrollPane?.verticalScrollBar?.revalidate()
324+
associatedScrollPane?.horizontalScrollBar?.revalidate()
325+
}
303326
}

0 commit comments

Comments
 (0)