この質問は、私が Java Swing や Graphics の基本的な理解を欠いているという単純な問題かもしれません。そうであれば、お詫び申し上げます。
Bluetooth経由でピッチ、ヨー、ロールの値をアプリケーションに送信する外部デバイスによって制御できるJava Swingを使用してGUIアプリケーションを開発しようとしています。私の考えは、外部デバイスが動き回るときに動き回るカーソル (おそらく空の円) を作成することです。デバイスからのデータの受信に問題はありません。すべてのコンポーネントに実際に何かをペイントする必要がある部分だけです。
GlassPane は、アプリケーション全体にカーソルを表示し、外部デバイスの移動に合わせてカーソルを移動させる最も簡単な方法であると考えました。Thread を使用してデータをキャプチャし、後で repaint() を呼び出そうとしていますが、トリガーされていないようです。
関連するコードは次のとおりです。
JFrame:
public class Frame extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
//Thread myoHandlerThread = new Thread(myoHandler);
//myoHandlerThread.start();
Frame frame = new Frame();
GlassPane glassPane = new GlassPane();
glassPane.setVisible(true);
frame.setGlassPane(glassPane);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Frame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(50, 50, 1000, 650);
/* Code to add and place components */
}
}
そして私のGlassPane:
public class GlassPane extends JComponent {
private static double pitch;
private static double yaw;
private static double roll;
Point point;
public void setPoint(Point p) {
this.point = p;
}
public void paintComponent(Graphics g) {
if (point != null) {
System.out.println("Test print statement");
g.setColor(Color.red);
g.fillOval(point.x - 10, point.y - 10, 20, 20);
}
}
public GlassPane() {
Thread handler = new Thread(deviceHandler);
handler.start();
}
private Runnable deviceHandler = new Runnable() {
@Override
public void run() {
Hub hub = new Hub("com.garbage");
System.out.println("Attempting to find device...");
Device externalDevice = hub.waitForDevice(10000);
if (externalDevice == null) {
throw new RuntimeException("Unable to find device!");
}
System.out.println("Connected");
DataCollector dataCollector = new DataCollector();
hub.addListener(dataCollector);
while (true) {
hub.run(1000/20); //gathers data and stores in dataCollector
roll = dataCollector.getRoll();
pitch = dataCollector.getPitch();
yaw = dataCollector.getYaw();
Point p = new Point();
p.setLocation(Math.abs(pitch) * 10, Math.abs(yaw) * 10);
setPoint(p);
repaint();
}
}
};
}
外部デバイスの向きに応じて、GUI のどこかに赤い円が描画されるようにしたいと考えています。この時点で、私の「test print statement」は一度も発火しません。
私の推測では、Java の GlassPane や、ペイント、ペイント コンポーネント、再ペイントがどのように機能するかについての基本的な理解が欠けていると思います。誰かが私が間違っていることを指摘できますか?