split이용한 배열의 마지막 값을 가져오는 방법






String filepath = "C:\\Users\\402-08\\Desktop\\jp\\Penguins.jpg";


// \\을 기준으로 데이터를 나누어 ar에 데이터를 넣어준다 . 

String[] ar = filepath.split("\\\\");


//데이터의 길이를 length로 확인하여 -1을 해주면 마지막 데이터를 가져올 수 있다.


String file = ar[ar.length - 1];

[패널과 합친 레이블 ]


import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;


public class Animation extends JFrame {

private static final long serialVersionUID = 1L;


public Animation() {

setLayout(null);

JPanel jp = new JPanel();

Icon ic = new ImageIcon("image\\캡처1.png");

JLabel lbl = new JLabel(ic);

jp.setBounds(20, 150, 430, 420);


jp.add(lbl);


// 패널추가

add(jp);

// 기본설정

setTitle("움직이는 스머프");

setBounds(100, 100, 1000, 1000);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);


String[] imgs = { "캡처1.png", "캡처2.png", "캡처3.png", "캡처4.png", "캡처5.png", "캡처6.png", "캡처7.png", "캡처8.png",

"캡처9.png", "캡처10.png", "캡처11.png", "캡처12.png" };


int idx = 0;

int pos = 5;

while (true) {

idx = idx + 1;

try {


Thread.sleep(100);

Icon icon1 = new ImageIcon("image\\" + imgs[idx % imgs.length]);

lbl.setIcon(icon1);

int x = jp.getX();

int y = jp.getY();

System.out.println(y);

if(x<10) {

pos=-5;

}else if (x>450) {

pos = 5;

}

jp.setLocation(x+pos , y);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}


}


}


// 위의 소스코드는 이미지를 움직이기 위한 소스코드 입니다. 

// 만약 패널위에 이미지를 묶어주었다면 위와 같이 이미지가 들어간 레이블의 값을 이동시키는 것이 아니라 

// 이미지를 묶고 있는 패널을 이동시켜주어야만합니다. 


만약, 레이블만 사용할 것이라면, 레이블만을 움직여주어야만합니다. 


[레이블로만 구성된 이미지 이동 ] 

import java.awt.Button;


import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;


public class Animation2 extends JFrame {


public Animation2() {


setLayout(null);


Icon ic = new ImageIcon("image\\캡처1.png");

JLabel lbl = new JLabel(ic);

lbl.setBounds(150, 30, 150,150);

add(lbl);

Button btn = new Button("dddddd");

add(btn);


setTitle("움직이는 스머프");

setBounds(100, 100, 1000, 1000);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

String[] imgs = { "캡처1.png", "캡처2.png", "캡처3.png", "캡처4.png", "캡처5.png", "캡처6.png", "캡처7.png", "캡처8.png",

"캡처9.png", "캡처10.png", "캡처11.png", "캡처12.png" };


int idx = 0;

int pos = 5;

while (true) {

idx = idx + 1;

try {


Thread.sleep(50);

Icon icon1 = new ImageIcon("image\\" + imgs[idx % imgs.length]);

lbl.setIcon(icon1);

int x = lbl.getX();

int y = lbl.getY();

System.out.println(x);

if (x < 10) {

pos = 5;

} else if (x > 750) {

pos = -5;

}

lbl.setLocation(x + pos, y);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}


}


}



'Java' 카테고리의 다른 글

String이 아닌 데이터를 String 으로 변환하기  (0) 2018.01.16
JOption Pane  (0) 2018.01.16
AWT를 활용한 ActionListener 사용법  (0) 2018.01.15
이벤트 라우팅  (0) 2018.01.15
BorderLayout  (0) 2018.01.15

int[] A = new int[input.length()];

for(임시변수 : 컬렉션){ 컬렉션의 모든 데이터를 순서대로 임시변수에 대입}




FilterOutputStream 에 정의 된 Close()는 flush()를 호출한 다음에 기반스트림의 close()를 호출하는 것을 알 수 있다. 

보조스트림을 사용한 경우 BufferedOutputStream() 사용한 경우 flush() 사용하지 않고 close()사용하면된다. 

'Java' 카테고리의 다른 글

JFram과 배열을 이용한 스머프 이미지 움직이는 방법.  (0) 2018.01.15
AWT를 활용한 ActionListener 사용법  (0) 2018.01.15
이벤트 라우팅  (0) 2018.01.15
BorderLayout  (0) 2018.01.15
이벤트 처리  (0) 2018.01.15

+ Recent posts