JavaFX

JavaFX 예제

psys 2020. 6. 19. 14:32
728x90

powerOnOff

>>  내가

더보기
package Power;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javafx.fxml.Initializable;
import javafx.scene.image.Image;
import javafx.fxml.FXML;
import javafx.scene.image.ImageView;

public class powerController implements Initializable{
	private int cnt;
	private List<Image> imageList;
	@FXML ImageView image;
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		imageList = new ArrayList<Image>();
		
		imageList.add(new Image(getClass().getResource("power_on.png").toExternalForm()));
		imageList.add(new Image(getClass().getResource("power_off.png").toExternalForm()));
		
	}

	@FXML public void imageClick() {
		image.setImage(imageList.get(cnt++%2));	
	}

}

>> 강사님

더보기
package Power;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javafx.fxml.Initializable;
import javafx.scene.image.Image;
import javafx.fxml.FXML;
import javafx.scene.image.ImageView;

public class powerController implements Initializable{
	@FXML ImageView image;
	boolean powerFlag=false;
	Image powerOff = new Image(getClass().getResource("power_on.png").toExternalForm());
	Image powerOn = new Image(getClass().getResource("power_off.png").toExternalForm());
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		
	}

	@FXML public void imageClick() {
		//image.setImage(imageList.get(cnt++%2)); // 조금 위험~
		if(powerFlag) {
			image.setImage(powerOff);
			powerFlag=false;
		}else {
			image.setImage(powerOn);
			powerFlag=true;
		}
	}

}

 

>> 내가

 

더보기
package RPS;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.control.Label;
import javafx.scene.control.ToggleGroup;

public class RPSController implements Initializable {

	@FXML
	ImageView player;
	@FXML
	ImageView ai;
	@FXML
	Label text;
	private List<Image> imageListPlayer;
	private List<Image> imageListAI;
	private Random r;
	private int num;
	private int a;
	@FXML ToggleGroup abc;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		imageListPlayer = new ArrayList<Image>();

		imageListPlayer.add(new Image(getClass().getResource("가위_왼쪽.png").toExternalForm()));
		imageListPlayer.add(new Image(getClass().getResource("바위_왼쪽.png").toExternalForm()));
		imageListPlayer.add(new Image(getClass().getResource("보_왼쪽.png").toExternalForm()));

		imageListAI = new ArrayList<Image>();

		imageListAI.add(new Image(getClass().getResource("가위_오른쪽.png").toExternalForm()));
		imageListAI.add(new Image(getClass().getResource("바위_오른쪽.png").toExternalForm()));
		imageListAI.add(new Image(getClass().getResource("보_오른쪽.png").toExternalForm()));

	}

	@FXML
	public void sAction() {
		player.setImage(imageListPlayer.get(0));
		a = 0;
	}

	@FXML
	public void rAction() {
		player.setImage(imageListPlayer.get(1));
		a = 1;
	}

	@FXML
	public void pAction() {
		player.setImage(imageListPlayer.get(2));
		a = 2;
	}

	@FXML
	public void vsAction() {
		r = new Random();
		num = r.nextInt(3);
		ai.setImage(imageListAI.get(num));
		
		if (a==0&& num == 0 || a == 1 && num == 1 || a == 2 && num == 2) {
			text.setText("비김!");
		} else if (a == 0 && num == 2 || a == 1 && num == 0 || a == 2 && num == 1) {
			text.setText("사람 승!");
		} else {
			text.setText("AI 승!");
		}
	}

}

'JavaFX' 카테고리의 다른 글

..  (0) 2020.06.30