I made a method for showing an image at mouse hover. The message from the system.out.println("success!!!"); is shown when I hover, but the image doesn't appear. Can anyone help me?
public void titleView() {
Image img = new Image("Img\\titleSirius.png");
ImageView title = new ImageView(img);
title.setImage(img);
title.setLayoutX(569);
title.setLayoutY(146);
title.fitHeightProperty().add(100);
title.fitWidthProperty().add(100);
title.setVisible(true);
System.out.println("success!!!");
}
The folder 'Img' is in the src folder of my project.
You need to add the ImageView object to your scene, e.g:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class ShowImage extends Application
{
public void titleView(Pane pane)
{
Image img = new Image("Img\\titleSirius.png");
ImageView title = new ImageView(img);
title.setImage(img);
title.setLayoutX(569);
title.setLayoutY(146);
title.fitHeightProperty().add(100);
title.fitWidthProperty().add(100);
title.setVisible(true);
pane.getChildren().add(title);
System.out.println("success!!!");
}
#Override
public void start(Stage primaryStage) throws Exception
{
Pane root = new Pane();
titleView(root);
Scene scene = new Scene(root, 640, 480);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
Related
I am currently trying to make "Welcome!" string and a button appear on screen by using the GUI Label and GUILayout Button, but it does not appear and there are not any errors that I know of. Unity emulator is ok, but the "Welcome!" string is not displayed in android.
Here is my code, can someone please tell me my problem and if I might have forgotten to do something.
public class MainMenu : MonoBehaviour
{
public GUISkin Skin;
public Texture btnTexture;
public void Awake() { }
public void OnGUI()
{
if (this.Skin != null)
{
GUI.skin = this.Skin;
}
GUI.skin.button.fontSize = 30;
Rect content = new Rect(30, 100, Screen.width-60, Screen.height-200);
GUILayout.BeginArea(content);
GUILayout.BeginHorizontal();
GUIStyle TextStyle = new GUIStyle(GUI.skin.label);
TextStyle.fontSize = 50;
// Load and set Font
Font myFont = (Font)Resources.Load("font/stingray", typeof(Font));
GUI.skin.font = myFont;
// Set color for selected and unselected
TextStyle.normal.textColor = Color.yellow;
TextStyle.hover.textColor = Color.yellow;
// Display and set the style in string
GUI.Label(new Rect(50,50,300,70), "Welcome!", TextStyle);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (GUILayout.Button("Join", GUILayout.Width(300), GUILayout.Height(80)))
{
SceneManager.LoadSceneAsync("SingleMenu");
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
void Start () { }
void Update () { }
}
There are two imageviews on AnchorPane in scenebuilder .one imageview1 has image
and now trying to drag and drop on imageview2.
public class FXMLDocumentController implements Initializable {
#FXML
private ImageView imageview1;
#FXML
private ImageView imageview2;
#Override
public void initialize(URL url, ResourceBundle rb) {
imageview1.setImage(new Image("File:///C:imagedragndropex6/IMG_4093.JPG"));
}
#FXML
private void handleDragDetected(MouseEvent event) {
System.out.println("DragDetectedEntered");
// ImageView im=(ImageView)event.getSource();
Image img=imageview1.getImage();
Dragboard db= imageview1.startDragAndDrop(TransferMode.COPY);
// db.setDragView(imageview1.getImage());
ClipboardContent content=new ClipboardContent();
content.putImage(img);
System.out.println(content.hasImage());
db.setContent(content);
System.out.println(db.hasImage());
System.out.println("DragDetectedExited");
}
#FXML
private void handleDragOver(DragEvent event) {
System.out.println("DragOverEntered");
Dragboard db=event.getDragboard();
System.out.println(db.hasImage());
if(db.hasImage()){
event.acceptTransferModes(TransferMode.COPY);
System.out.println(event.getAcceptedTransferMode());
}
event.consume();
System.out.println("DragOverExited");
}
#FXML
private void handleDragDropped(DragEvent event) {
System.out.println("DragDroppedrEntered");
Dragboard dragboard=event.getDragboard();
if(dragboard.hasImage()){
Image img=dragboard.getImage();
imageview2.setFitHeight(200);
imageview2.setFitWidth(200);
imageview2.setPreserveRatio(true);
imageview2.setImage(img);
event.setDropCompleted(true);
}
else
event.setDropCompleted(false);
System.out.println("DragDroppedrEntered");
}
}
output:
DragDetectedEntered
true
true
DragDetectedExited
DragOverEntered
false
DragOverExited
Plz let me know what is wrong with this trying for hours...
public class FXMLDocumentController implements Initializable {
#FXML
private ImageView imageview1;
#FXML
private AnchorPane anchorpane;
#Override
public void initialize(URL url, ResourceBundle rb) {
imageview1.setImage(new Image("File:///D:/Downloads/Earthpic.jpg"));
// TODO
}
#FXML
private void handleDragDetected(MouseEvent event) {
System.out.println("DragDetectedEntered");
Dragboard db= imageview1.startDragAndDrop(TransferMode.COPY_OR_MOVE);
Image img=imageview1.getImage();
ClipboardContent content=new ClipboardContent();
content.putImage(img);
System.out.println(content.hasImage());
db.setContent(content);
System.out.println(db.hasImage());
System.out.println("DragDetectedExited");
event.consume();
}
#FXML
private void handleDragOver(DragEvent event) {
System.out.println("DragOverEntered");
System.out.println(event.getGestureSource());
System.out.println(event.getGestureTarget());
Dragboard db=event.getDragboard();
System.out.println(db.hasImage());
if(db.hasImage()){
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
System.out.println(event.getAcceptedTransferMode());
}
event.consume();
System.out.println("DragOverExited");
}
#FXML
private void handleDragDropped(DragEvent event) {
System.out.println("DragDroppedrEntered");
Dragboard dragboard=event.getDragboard();
if(dragboard.hasImage()){
Image img=dragboard.getImage();
ImageView imageview2=new ImageView();
imageview2.setFitHeight(200);
imageview2.setFitWidth(200);
imageview2.setLayoutX(400);
imageview2.setLayoutY(150);
imageview2.setPreserveRatio(true);
imageview2.setImage(img);
anchorpane.getChildren().add(imageview2);
event.setDropCompleted(true);
}
else
event.setDropCompleted(false);
System.out.println("DragDroppedrEntered");
}
}
Finally,this works fine...handleDragOver and handleDragDropped has to be set to anchorpane.
I am trying to learn the principles of starting a new activity in android. I am using "Android studio". I have created a MainActivity class which contains a text which tells the user to push the button, and the button. When the button is pushed the new activity is supposed to start(aktivitet2Klasse). This activity contains two textviews and a button. When the first button is pressed, the tetview should display "works fine!". When the second button is pushed it is supposed to display "Finished!". The point with the app is just to start one activity from another. However after pushing the button on the first screen (first activity) the app crashes. I have studies a lot of the same issues on these sides, but I cannot figure what is wrong.
Here is the code:
The main activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView instruction;
private Button startButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
instruction = (TextView) findViewById(R.id.InstruksjonsView);
startButton = (Button) findViewById(R.id.btn1);
startButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent intent = new Intent(MainActivity.this,aktivitet2Klasse.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The next activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class aktivitet2Klasse extends AppCompatActivity {
private Button button2;
private TextView text;
private Button button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
button2 = (Button) findViewById(R.id.btn2);
button3 = (Button) findViewById(R.id.btn3);
text = (TextView) findViewById(R.id.textView1);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Works fine!");
}
});//End button2Listener
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Finished!");
}
});//End button3Listener
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
you forgot to set the content view in the second activity
public class aktivitet2Klasse extends AppCompatActivity {
private Button button2;
private TextView text;
private Button button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//YOU NEED TO SET THE CONTENTVIEW
// like setContentView(R.layout.activity_secondary);
Intent intent = getIntent();
button2 = (Button) findViewById(R.id.btn2);
button3 = (Button) findViewById(R.id.btn3);
text = (TextView) findViewById(R.id.textView1);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Works fine!");
}
});//End button2Listener
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Finished!");
}
});//End button3Listener
}
I have some problem with binding ngui events in the StrangeIoC framework.
This is an unityGUI sample:
public class TestView : View
{
private readonly Rect buttonRect = new Rect(0, 0, 200, 50);
public Signal buttonClicked = new Signal();
public void OnGUI()
{
if (GUI.Button(buttonRect, "Test"))
{
buttonClicked.Dispatch();
}
}
}
This is the NGUI version:
public class NGUIView : View
{
public UIButton Button;
public Signal buttonClicked = new Signal();
private void Start()
{
if (Button != null)
{
EventDelegate.Add(Button.onClick, buttonClicked.Dispatch);
}
}
}
In the NGUI version, the the buttonClicked is never dispatched. I noticed in the scene the Notify property on that button has an empty value.
This one works, but the buttonClicked is triggered several times :(
public class NGUIView : View
{
public UIButton Button;
public Signal buttonClicked = new Signal();
void Update()
{
if (Button.state == UIButtonColor.State.Pressed)
{
buttonClicked.Dispatch();
}
}
}
Could you kindly tell me how do you handle this NGUI-StrangeIoC situation?
Thanks!
I finally figured out. Add the delegate in Awake instead of Start.
Now here's the perfect solution(not sure enough):
public class TestView : View
{
private readonly Rect buttonRect = new Rect(0, 0, 200, 50);
public UIButton Button;
public Signal buttonClicked = new Signal();
private void OnGUI()
{
if (GUI.Button(buttonRect, "Test"))
{
buttonClicked.Dispatch();
}
}
private void Awake()
{
EventDelegate.Add(Button.onClick, buttonClicked.Dispatch);
}
}
The docs say the button should fire for clicks, keypress, and touch. It doesn't seem to work.
Some code to play with.
public class FireButtonTest extends Application {
#Override
public void start(Stage primaryStage) {
TextArea ta = new TextArea();
Button btn1 = new Button();
btn1.setText("btn1");
btn1.setOnAction((ActionEvent event) -> {
ta.setText(ta.getText()+"btn1 fired \n");
});
Button btn2 = new Button();
btn2.setText("btn2");
btn2.setOnAction((ActionEvent event) -> {
ta.setText(ta.getText()+"btn2 fired \n");
});
btn2.setOnKeyReleased((KeyEvent ke) -> {
if (ke.getCode() == KeyCode.ENTER) {
ta.setText(ta.getText()+"btn2 key event -> ");
btn2.fire();
}
});
//?? can't test this
btn2.setOnTouchReleased((TouchEvent te) -> {
if (te.getTouchCount() == 1){
ta.setText(ta.getText()+"btn2 touch event -> ");
btn2.fire();
}
});
VBox root = new VBox(5);
root.getChildren().addAll(btn1, btn2, ta);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Button Test");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Why can't I just press enter when the button is focused?
Doh! I guess you have to use the space bar. Here's the source.
<!-- language: java -->
protected static final List<KeyBinding> BUTTON_BINDINGS = new ArrayList<KeyBinding>();
static {
BUTTON_BINDINGS.add(new KeyBinding(SPACE, KEY_PRESSED, PRESS_ACTION));
BUTTON_BINDINGS.add(new KeyBinding(SPACE, KEY_RELEASED, RELEASE_ACTION));
}