Ik wil graag een variabele uitlezen en veranderen van een andere klasse.
Ik wil de kleur veranderen onder invloed van een variabele uit een andere klasse. dus in pricipe zou "red" nu door een variabele vervangen moeten worden, en die variabele wordt gedefineerd in een andere klasse.
Enig idee hoe ik dit moet aanpakken?
De code van mijn andere klasse:
code:
1
| app.setContentPane(new Paneel() ); |
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| class Paneel extends JPanel { public void paint(Graphics g) { super.paintComponent(g); g.setColor (Color.red); g.fillOval (50,50,70,70); g.fillOval (125,50,70,70); g.fillOval (200,50,70,70); g.fillOval (50,125,70,70); g.fillOval (125,125,70,70); g.fillOval (200,125,70,70); g.fillOval (50,200,70,70); g.fillOval (125,200,70,70); g.fillOval (200,200,70,70); } } |
Ik wil de kleur veranderen onder invloed van een variabele uit een andere klasse. dus in pricipe zou "red" nu door een variabele vervangen moeten worden, en die variabele wordt gedefineerd in een andere klasse.
Enig idee hoe ik dit moet aanpakken?
De code van mijn andere klasse:
code:
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
| import java.io.*; import java.util.*; import javax.comm.*; import java.awt.*; import javax.swing.*; import java.util.Scanner; import javax.swing.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier portId; static Enumeration portList; InputStream inputStream; SerialPort serialPort; Thread readThread; public String test; public static void main(String[] args) { portList = CommPortIdentifier.getPortIdentifiers(); JFrame app=new JFrame(); app.setBounds(213,133,600,600); app.setTitle("Sensor Check"); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setContentPane(new Paneel() ); app.setVisible(true); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals("COM16")) { // if (portId.getName().equals("/dev/term/a")) { SimpleRead reader = new SimpleRead(); } } } } public SimpleRead() { test = "blaat"; try { serialPort = (SerialPort) portId.open("SimpleReadApp", 2000); } catch (PortInUseException e) {System.out.println(e);} try { inputStream = serialPort.getInputStream(); } catch (IOException e) {System.out.println(e);} try { serialPort.addEventListener(this); } catch (TooManyListenersException e) {System.out.println(e);} serialPort.notifyOnDataAvailable(true); try { serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) {System.out.println(e);} readThread = new Thread(this); readThread.start(); } public void run() { try { Thread.sleep(20000); } catch (InterruptedException e) {System.out.println(e);} } public String getColor() { return "red"; } public void serialEvent(SerialPortEvent event) { switch(event.getEventType()) { case SerialPortEvent.BI: case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: byte[] readBuffer = new byte[10]; try { while (inputStream.available() > 0) { int numBytes = inputStream.read(readBuffer); } System.out.print("waarde: " + new String(readBuffer)); } catch (IOException e) {System.out.println(e);} break; } } } |