Skip to content

Commit 1873a19

Browse files
committed
Initial commit
0 parents  commit 1873a19

18 files changed

+1207
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Packages/
2+
.build/
3+
*.bin
4+
*.a

Package.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import PackageDescription
2+
3+
let package = Package(
4+
name: "examples",
5+
dependencies: [
6+
.Package(url: "https://github.com/SwiftJava/javax_swing.git", versions: Version(1,0,0)..<Version(2,0,0)),
7+
]
8+
)

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
### SwiftJava examples
3+
4+
Git submodule containing AWT and Swing examples intended to be cloned
5+
separately to build on Linux or macOS using the following commands:
6+
7+
export JVM_LIBRARY_PATH=$JAVA_HOME/jre/lib/server # macOS
8+
export JVM_LIBRARY_PATH=$JAVA_HOME/jre/lib/amd64/server # Linux
9+
10+
swift build -Xlinker -L$JVM_LIBRARY_PATH -Xlinker -rpath -Xlinker $JVM_LIBRARY_PATH -Xlinker -ljvm

Sources/AWTGraphicsDemo.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// AWTGraphicsDemo.swift
3+
// SwiftJava
4+
//
5+
// Created by John Holdsworth on 29/07/2016.
6+
// Copyright © 2016 John Holdsworth. All rights reserved.
7+
//
8+
9+
// Original Java Version: http://www.tutorialspoint.com/awt/awt_line2d_class.htm
10+
11+
import java_lang
12+
import java_awt
13+
14+
public class AWTGraphicsDemo: FrameBase {
15+
16+
public static func main(){
17+
let awtGraphicsDemo = AWTGraphicsDemo()
18+
awtGraphicsDemo.setVisible(true)
19+
}
20+
21+
public init(){
22+
super.init( javaObject: FrameBase().javaObject )
23+
prepareGUI()
24+
}
25+
26+
required public init(javaObject: jobject!) {
27+
fatalError("init(javaObject:) has not been implemented")
28+
}
29+
30+
private func prepareGUI(){
31+
setSize(400,400)
32+
class MyWindowAdapter: WindowAdapterBase {
33+
override func windowClosing( e: WindowEvent? ) {
34+
System.exit( 0 )
35+
}
36+
}
37+
addWindowListener(MyWindowAdapter())
38+
}
39+
40+
override public func paint(arg0: Graphics?) {
41+
let g = arg0!
42+
let shape = Line2D_Double()
43+
shape.setLine(250,250,150,150)
44+
let g2 = Graphics2D(casting: g)!
45+
g2.draw (shape)
46+
let font = Font("Serif", Font.PLAIN, 24)
47+
g2.setFont(font)
48+
g.drawString("Welcome to TutorialsPoint", 50, 70)
49+
g2.drawString("Line2D.Line", 100, 120)
50+
}
51+
}

Sources/FocusTest.swift

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// FocusTest.swift
3+
// SwiftJava
4+
//
5+
// Created by John Holdsworth on 28/07/2016.
6+
// Copyright © 2016 John Holdsworth. All rights reserved.
7+
//
8+
9+
import java_lang
10+
import java_awt
11+
import javax_swing
12+
13+
14+
public class FocusTest: JFrame {
15+
16+
var jtf1: JTextField!, jtf2: JTextField!, jtf3: JTextField!
17+
var p: JPanel!
18+
19+
public init()
20+
{
21+
let frame = JFrame()
22+
super.init( javaObject: frame.javaObject )
23+
24+
jtf1 = JTextField(10)
25+
jtf2 = JTextField(10)
26+
jtf3 = JTextField(10)
27+
28+
jtf1.addKeyListener(MyKeyListener( ft: self ))
29+
30+
p = JPanel()
31+
_ = p.add(jtf1)
32+
_ = p.add(jtf2)
33+
_ = p.add(jtf3)
34+
35+
_ = getContentPane().add(p)
36+
}
37+
38+
required public init(javaObject: jobject?) {
39+
fatalError("init(javaObject:) has not been implemented")
40+
}
41+
42+
class MyKeyListener: KeyListenerBase {
43+
44+
var ft: FocusTest
45+
46+
init( ft: FocusTest) {
47+
self.ft = ft
48+
super.init( javaObject: nil )
49+
}
50+
51+
required init(javaObject: jobject!) {
52+
fatalError("init(javaObject:) has not been implemented")
53+
}
54+
55+
internal override func keyPressed(e: KeyEvent?)
56+
{
57+
if(e?.getKeyCode() == KeyEvent.VK_ENTER)
58+
{
59+
Swift.print("ll")
60+
ft.jtf3.requestFocus()
61+
}
62+
}
63+
}
64+
}

Sources/ImageCanvas.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// ImageCanvas.swift
3+
// SwiftJava
4+
//
5+
// Created by John Holdsworth on 28/07/2016.
6+
// Copyright © 2016 John Holdsworth. All rights reserved.
7+
//
8+
9+
// Original Java Version: http://www.rgagnon.com/javadetails/java-0229.html
10+
11+
import java_lang
12+
import java_awt
13+
14+
class ImageCanvas: CanvasBase {
15+
var image: Image!
16+
17+
static func main( _ imagePath: String ) {
18+
let frame = try! Frame("Image")
19+
frame.setLayout(BorderLayout())
20+
_ = frame.add("Center", ImageCanvas(imagePath))
21+
frame.resize(400,400)
22+
frame.show()
23+
}
24+
25+
init(_ imagePath: String) {
26+
super.init( javaObject: CanvasBase().javaObject )
27+
let media = MediaTracker(comp: self)
28+
image = Toolkit.getDefaultToolkit().getImage(imagePath)
29+
media.addImage(image, 0)
30+
do {
31+
try media.waitForID(0)
32+
}
33+
catch {}
34+
}
35+
36+
init(imageProducer:ImageProducer) {
37+
super.init( javaObject: CanvasBase().javaObject )
38+
image = createImage(imageProducer)
39+
}
40+
41+
required init(javaObject: jobject?) {
42+
fatalError("init(javaObject:) has not been implemented")
43+
}
44+
45+
override func paint( g: Graphics? ) {
46+
_ = g?.drawImage(image, 0,0, self)
47+
}
48+
49+
}
50+

Sources/JComboBox.swift

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
//
2+
// JComboBox.swift
3+
// SwiftJava
4+
//
5+
// Created by John Holdsworth on 28/07/2016.
6+
// Copyright © 2016 John Holdsworth. All rights reserved.
7+
//
8+
9+
// Original Java Version: http://java.happycodings.com/swing/code5.html
10+
11+
import java_lang
12+
import java_util
13+
import java_awt
14+
import javax_swing
15+
16+
private func +(left: String!, right: UInt16) -> String {
17+
var left = left ?? ""
18+
left.append( String( UnicodeScalar(right)! ) )
19+
return left
20+
}
21+
22+
var cBox: AutoComplete!
23+
24+
class AutoComplete: JComboBox
25+
{
26+
27+
static func main() {
28+
let f = try! JFrame("AutoCompleteComboBox")
29+
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
30+
f.setSize(200,300)
31+
let cp = f.getContentPane()!
32+
cp.setLayout(nil)
33+
// let names = ["Beate", "Claudia", "Fjodor", "Fred", "Friedrich", "Fritz", "Frodo", "Hermann", "Willi"]
34+
// cBox = AutoComplete(items: names.map { JavaString( $0 ) })
35+
let locales = JavaLocale.getAvailableLocales()!//
36+
cBox = AutoComplete(items: locales)
37+
cBox.setBounds(50,50,100,21)
38+
cBox.setEditable(true)
39+
_ = cp.add(cBox)
40+
f.setVisible(true)
41+
}
42+
43+
private var searchFor: String!
44+
private var lap: jlong!
45+
46+
class CBDocument: PlainDocumentBase {
47+
override func insertString(offs:Int, str: String? , a: AttributeSet?) throws {
48+
if (str==nil) { return }
49+
try super.insertString(offs:offs, str: str, a: a)
50+
if(!cBox.isPopupVisible() && JavaString(str).length() != 0) { cBox.fireActionEvent() }
51+
}
52+
}
53+
54+
class MyJComboBox_KeySelectionManager: JComboBox_KeySelectionManagerBase {
55+
56+
override func selectionForKey( aKey: UInt16, aModel: ComboBoxModel? ) -> Int {
57+
let now = java_util.Date().getTime()
58+
if ( cBox.searchFor != nil && aKey == UInt16(KeyEvent.VK_BACK_SPACE) && JavaString(cBox.searchFor).length()>0 )
59+
{
60+
cBox.searchFor = JavaString(cBox.searchFor).substring(0, JavaString(cBox.searchFor).length() - 1)
61+
}
62+
else
63+
{
64+
// System.out.println(lap)
65+
// Kam nie hier vorbei.
66+
if(cBox.lap + 1000 < now) {
67+
cBox.searchFor = "" + aKey
68+
}
69+
else {
70+
cBox.searchFor = cBox.searchFor + aKey
71+
}
72+
}
73+
cBox.lap = now
74+
var current: String!
75+
for i in 0..<aModel!.getSize()
76+
{
77+
current = JavaString(aModel!.getElementAt(i).toString()).toLowerCase()
78+
if (JavaString(JavaString(current).toLowerCase()).startsWith(JavaString(cBox.searchFor).toLowerCase())) { return i }
79+
}
80+
return -1
81+
}
82+
83+
}
84+
85+
init(items: [JavaObject])
86+
{
87+
super.init(javaObject:JComboBox(items).javaObject)
88+
lap = java_util.Date().getTime()
89+
setKeySelectionManager(MyJComboBox_KeySelectionManager())
90+
if(getEditor() != nil)
91+
{
92+
if let tf = JTextField(casting: getEditor().getEditorComponent())
93+
{
94+
tf.setDocument( CBDocument())
95+
96+
class MyActionListener: ActionListenerBase {
97+
98+
override func actionPerformed(e: ActionEvent?)
99+
{
100+
let tf = JTextField(casting: cBox.getEditor().getEditorComponent())!
101+
let text = tf.getText()!
102+
let aModel = cBox.getModel()!
103+
var current: String
104+
for i in 0..<aModel.getSize()
105+
{
106+
current = aModel.getElementAt(i).toString()
107+
if(JavaString(JavaString(current).toLowerCase()).startsWith(JavaString(text).toLowerCase()))
108+
{
109+
tf.setText(current)
110+
tf.setSelectionStart(JavaString(text).length())
111+
tf.setSelectionEnd(JavaString(current).length())
112+
break
113+
}
114+
}
115+
}
116+
}
117+
118+
addActionListener(MyActionListener())
119+
}
120+
121+
}
122+
}
123+
124+
required init(javaObject: jobject?) {
125+
fatalError("init(javaObject:) has not been implemented")
126+
}
127+
128+
override func fireActionEvent()
129+
{
130+
super.fireActionEvent()
131+
}
132+
}

0 commit comments

Comments
 (0)