2017-03-22 17 views
0

私はJavaFXにはかなり新しく、透明な背景と2つの赤い円でアプリケーションを作成しようとしています。サークルはタスクバーのアプリアイコンにカーソルを合わせると表示されます(小さなプレビューウィンドウに表示されます)が、画面には表示されません。画面にキャンバスを表示するのに問題があるJavaFX

編集:awtのものではなくScreenクラスを使用して修正しました。

私のコードは次のとおりです。

package com.razorrider7.touchgame; 

import java.awt.GraphicsDevice; 
import java.awt.GraphicsEnvironment; 
import java.awt.Insets; 
import java.awt.Rectangle; 
import java.awt.Toolkit; 
import java.io.File; 

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.canvas.Canvas; 
import javafx.scene.canvas.GraphicsContext; 
import javafx.scene.layout.Pane; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 

import com.razorrider7.touchgame.manager.SettingsManager; 

public class TouchGame extends Application { 

public static String dataFolder = System.getenv("APPDATA") + File.separator + "TouchGame"; 

@Override 
public void start(Stage stage) throws Exception { 
SettingsManager.setup(); 
initUI(stage); 
} 

private void initUI(Stage stage) { 

stage.setResizable(false); 

// Make the window transparent 
stage.initStyle(StageStyle.TRANSPARENT); 

Pane pane = new Pane(); 

double width = getBounds().getWidth(), height = getBounds().getHeight(); 

stage.setWidth(width); 
stage.setHeight(height); 
stage.setX(0); 
stage.setY(0); 


// Create new canvases for the left and right joysticks 
Canvas joy1 = new Canvas(), joy2 = new Canvas(); 

joy1.setWidth(Math.rint(width/6)); 
joy1.setHeight(Math.rint(width/6)); 
joy2.setWidth(Math.rint(width/6)); 
joy2.setHeight(Math.rint(width/6)); 

joy1.setTranslateX(0); 
joy1.setTranslateY(height - width/6); 
joy2.setTranslateX(width - width/6); 
joy2.setTranslateY(height - width/6); 

// Draw the joysticks 
drawJoy(joy1); 
drawJoy(joy2); 

pane.getChildren().add(joy1); 
pane.getChildren().add(joy2); 

Scene scene = new Scene(pane); 
stage.setScene(scene); 
stage.setTitle("TouchGame"); 
stage.setAlwaysOnTop(true); 
stage.show(); 
} 

private void drawJoy(Canvas canvas) { 
GraphicsContext gc = canvas.getGraphicsContext2D(); 
gc.setStroke(Color.RED); 
gc.setFill(Color.RED); 
gc.fillOval(1, 1, canvas.getWidth() - 1, canvas.getHeight() - 1); 
} 

public Rectangle getBounds() { 
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); 
Rectangle bounds = gd.getDefaultConfiguration().getBounds(); 
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gd.getDefaultConfiguration()); 

Rectangle safebounds = new Rectangle(bounds); 
safebounds.x += insets.left; 
safebounds.y += insets.top; 
safebounds.width -= (insets.left + insets.right); 
safebounds.height -= (insets.top + insets.bottom); 
return safebounds; 
} 
+0

私のために働きます。円は問題ありません(OS X、Java 8u72)。 UIツールキットを混ぜたり、プログラムからすべてのjava.awtインポートを削除しないことをお勧めします。また、キャンバスではなくシーングラフをプログラミングする方が簡単です。 – jewelsea

+0

Javaコードの[Screen](https://docs.oracle.com/javase/8/javafx/api/javafx/stage/Screen.html)クラスは、awtコードの置き換えに匹敵する可能性があります。 – jewelsea

+0

そうだった。 Screenクラスを使用してそれを修正しました。ありがとう! – razorrider7

答えて

0

はJavaFXのScreenクラスを使用してAWTコードを交換してください。

関連する問題