[Groovy 및 Processing] 키보드와 마우스에서 입력
16397 단어 processingGroovy
마우스 왼쪽 클릭으로 상자 선택, 드래그하여 상자 이동, 오른쪽 클릭으로 선택 취소, 휠 회전으로 상자 색상 변경
동영상 목록 : LWJGL 튜토리얼
동영상은 코코
DisplayTest.groovy
package episode004
import groovy.swing.SwingBuilder
import processing.core.PApplet
import javax.swing.*
import java.awt.event.MouseWheelListener
class DisplayTest extends PApplet {
def shapes = []
def somethingIsSelected = false
def volatile randomColorCooldown = false
def void setup() {
frameRate(60)
addMouseWheelListener({ mwe ->
mouseWheel(mwe.wheelRotation)
} as MouseWheelListener)
shapes << new Box(this, 15, 15)
shapes << new Box(this, 100, 150)
}
def void draw() {
background(0, 0, 0)
shapes.each { box ->
if (box.selected) {
box.update(mouseX - pmouseX, mouseY - pmouseY)
}
box.draw()
}
}
def void keyPressed() {
switch (key) {
case ' ':
System.exit(0)
break
case 'c':
shapes << new Box(this, 15, 15)
break
}
}
def void mouseClicked() {
shapes.each { box ->
selectedBoxUpdate(box)
}
}
def void mouseDragged() {
shapes.each { box ->
selectedBoxUpdate(box)
}
}
def mouseWheel(delta) {
shapes.each { box ->
if (box.inBounds(mouseX, mouseY) && !somethingIsSelected) {
box.randomizeColors()
randomColorCooldown = true
new Thread({
Thread.sleep(200)
randomColorCooldown = false
} as Runnable).run()
}
}
}
def selectedBoxUpdate(box) {
switch (mouseButton) {
case LEFT:
if (box.inBounds(mouseX, mouseY) && !somethingIsSelected) {
somethingIsSelected = true
box.selected = true
}
break
case RIGHT:
somethingIsSelected = false
box.selected = false
break
}
}
def static void main(args) {
def display = new DisplayTest()
new SwingBuilder().frame(
title: 'Episode 4',
defaultCloseOperation: JFrame.EXIT_ON_CLOSE,
size: [640, 480], show: true) {
widget(display)
}
display.init()
}
}
class Box {
def x, y
def selected = false
def colorRed, colorBlue, colorGreen
def display
def Box(display, x, y) {
this.display = display
this.x = x
this.y = y
def randomGenerator = new Random()
colorRed = randomGenerator.nextInt(256)
colorBlue = randomGenerator.nextInt(256)
colorGreen = randomGenerator.nextInt(256)
}
def inBounds(mouseX, mouseY) {
mouseX > x && mouseX < x + 50 && mouseY > y && mouseY < y + 50
}
def update(dx, dy) {
x += dx
y += dy
}
def randomizeColors() {
def randomGenerator = new Random()
colorRed = randomGenerator.nextInt(256)
colorBlue = randomGenerator.nextInt(256)
colorGreen = randomGenerator.nextInt(256)
}
def draw() {
display.with {
noStroke()
fill(colorRed, colorGreen, colorBlue)
rect(this.x, this.y, 50, 50)
}
}
}
Reference
이 문제에 관하여([Groovy 및 Processing] 키보드와 마우스에서 입력), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/hina0118/items/c4258535f36c939c6a6c텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)