Skip to content

Conversation

jamesmudd
Copy link
Member

No description provided.

@jamesmudd jamesmudd requested review from Stewori, jeff5 and jimbaker March 24, 2019 20:47
@jamesmudd
Copy link
Member Author

You can see the page after this change at https://jamesmudd.github.io/jython.github.io/ Would be good to get peoples thoughts. I think we should have some nice examples showing the different ways Jython can be used. Not sure this is best was struggling to think of a cool example.

@Stewori
Copy link
Member

Stewori commented Mar 25, 2019

Looks good to me. Using timestamp from Java is also my favorite way to demonstrate an API call.
I'll give you some more advanced examples, yet no material for the front page since it's too complex. I suppose it would be good material for a separate examples page or as links for further reading.

  • In my Paper https://arxiv.org/abs/1607.00825, page 45 (paper is only 9 pages, but pages are numbered in context of Euro SciPy proceedings) there is a Tkinter example that makes use of coercion into Java inerfaces. Since it's a JyNI paper, that example would require JyNI, but the principle can also be adopted to pure Jython examples.

  • Josh Juneau has a JavaFX example in Jython:
    https://github.com/juneau001/JavaFXExamples/blob/master/src/jython/javafxdraw/JavaFXDrawJython.py
    However, JavaFX is rather finicky with Jython, e.g. some imports require certain order to work. (See the cumbersome from javafx.scene.control import Button, ChoiceBox somewhere within the main code in the example below.)

I once edited Josh's example to be even more compact and "pythonic". It counts less than 80 lines now.
Josh' didn't specify a license, so maybe we should ask him for permission, OTOH it is really tiny code example and does not make the impression as if he'd bother much. And we'd give credit accordingly.
Here is my compactified example:

# Based on code by Josh Juneau: https://github.com/juneau001/JavaFXExamples
# Edited by Stefan Richthofer.

from javafx.application import Application
from javafx.scene.paint import Color
from javafx.collections import FXCollections
from javafx.scene.canvas import Canvas
from javafx.scene import Scene
from javafx.scene.input import MouseEvent
from javafx.scene.layout import BorderPane, HBox, StackPane
import javafx.stage

class JythonFXDraw(Application):
	colors = ("Aqua", "LIGHTgreen", "Black", "Blue", "Red", "Green", "Brown", "Orange")

	def start(self, primaryStage):
		primaryStage.setTitle("JythonFX Draw")
		root = StackPane()
		screen = javafx.stage.Screen.getPrimary()
		rect = screen.visualBounds
		canvas = Canvas(rect.width/2 + 50, rect.height/2 + 300)
		graphics_context = canvas.graphicsContext2D

		def resetAction(event):
			graphics_context.clearRect(2, 2, canvas.width-3, canvas.height-3)

		def colorAction(newval):
			graphics_context.setStroke(getattr(Color, JythonFXDraw.colors[newval.value].upper()))

		def sizeAction(newval):
			graphics_context.lineWidth = newval.value+1

		from javafx.scene.control import Button, ChoiceBox
		resetButton = Button("Reset", onAction=resetAction)
		resetButton.translateX = 10
		colorChooser = ChoiceBox(FXCollections.observableArrayList(*JythonFXDraw.colors))

		cssm = colorChooser.selectionModel
		cssm.selectedIndexProperty().addListener(colorAction)

		sizeChooser = ChoiceBox(FXCollections.observableArrayList(range(1, 6)))

		scsm = sizeChooser.selectionModel
		scsm.selectedIndexProperty().addListener(sizeAction)

		def mouse_pressed(event):
			graphics_context.beginPath()
			graphics_context.moveTo(event.x, event.y)
			graphics_context.stroke()

		def mouse_dragged(event):
			graphics_context.lineTo(event.x, event.y)
			graphics_context.stroke()

		canvas.addEventHandler(MouseEvent.MOUSE_PRESSED, mouse_pressed)
		canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED, mouse_dragged)

		buttonBox = HBox()
		buttonBox.children.addAll(colorChooser, sizeChooser, resetButton)

		self.init_draw(graphics_context, canvas.layoutX, canvas.layoutY)

		container = BorderPane()
		container.top = buttonBox
		container.center = canvas
		root.children.add(container)

		primaryStage.title = "JythonFX Draw"
		primaryStage.scene = Scene(root, rect.height, rect.width)
		primaryStage.show()

	def init_draw(self, gc, x, y):
		gc.fill()
		gc.strokeRect(x, y, gc.canvas.width, gc.canvas.height)

if __name__ == "__main__":
	Application.launch(JythonFXDraw().class, [])

@jeff5
Copy link
Member

jeff5 commented Mar 29, 2019

I like it. The examples are short, and at least the first is compelling, since if you know Python, you know where to go next.

The second ... meh. But the JavaFX one is long . (Another page maybe?) The thing that first got me interested in Jython was:

>>> from javax.sound.midi import *
>>> myMsg = ShortMessage()
>>> myMsg.setMessage(ShortMessage.NOTE_ON, 4, 60, 93)
>>> synth = MidiSystem.getSynthesizer()
>>> synth.open()
>>> synthRcvr = synth.getReceiver()
>>> synthRcvr.send(myMsg, -1)

This is an example from from https://docs.oracle.com/javase/tutorial/sound/MIDI-synth.html . It's not very nice Python, and I didn't get much further, because I got interested in Jython.

@jamesmudd jamesmudd merged commit 7dba8a0 into jython:master Mar 30, 2019
@jamesmudd jamesmudd deleted the improve-homepage branch March 30, 2019 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants