aboutsummaryrefslogtreecommitdiff
path: root/VexRiscv/src/main/scala/vexriscv/test/Swing.scala
blob: b3a1637a5ac9c9f019f6a6a2b45ee60e9ad9f461 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package vexriscv.test

import java.awt.event.{MouseEvent, MouseListener}
import java.awt.{Color, Dimension, Graphics}
import javax.swing.JPanel

abstract class JLedArray(ledCount : Int,ledDiameter : Int = 20, blackThickness : Int = 2) extends JPanel{
  def getValue() : BigInt

  override def paintComponent(g : Graphics) : Unit = {
    val value = getValue()
    for(i <- 0 to ledCount-1) {
      g.setColor(Color.BLACK)
      val x = i*ledDiameter + 1
      g.fillOval(x,1,ledDiameter,ledDiameter)
      if (((value >> (ledCount-1-i)) & 1) != 0) {
        g.setColor(Color.GREEN.darker())
        g.fillOval(x+blackThickness,3,ledDiameter-blackThickness*2,ledDiameter-blackThickness*2);
      }
    }
    g.setColor(Color.BLACK)
  }
  this.setPreferredSize(new Dimension(ledDiameter*ledCount+2, ledDiameter+2))
}

class JSwitchArray(ledCount : Int,switchDiameter : Int = 20, blackThickness : Int = 2) extends JPanel{
  var value = BigInt(0)
  def getValue() = value
  addMouseListener(new MouseListener {
    override def mouseExited(mouseEvent: MouseEvent): Unit = {}
    override def mousePressed(mouseEvent: MouseEvent): Unit = {}
    override def mouseReleased(mouseEvent: MouseEvent): Unit = {}
    override def mouseEntered(mouseEvent: MouseEvent): Unit = {}
    override def mouseClicked(mouseEvent: MouseEvent): Unit = {
      val idx = ledCount-1-(mouseEvent.getX-2)/switchDiameter
      value ^= BigInt(1) << idx
    }
  })
  override def paintComponent(g : Graphics) : Unit = {
    for(i <- 0 to ledCount-1) {
      g.setColor(Color.GRAY.darker())
      val x = i*switchDiameter + 1
      g.fillRect(x,1,switchDiameter,switchDiameter)
      if (((value >> (ledCount-1-i)) & 1) != 0) {
        g.setColor(Color.GRAY)
      }else{
        g.setColor(Color.GRAY.brighter())
      }
      g.fillRect(x+blackThickness,3,switchDiameter-blackThickness*2,switchDiameter-blackThickness*2);

    }
    g.setColor(Color.BLACK)
  }
  this.setPreferredSize(new Dimension(switchDiameter*ledCount+2, switchDiameter+2))
}