Java getContentPane()
2023. 2. 2. 17:24ㆍJava
Container javax.swing.JFrame.getContentPane()
getContentPane() 메서드는 이 프레임에 쓸 contentPane 객체를 반환한다.
Pane 은 창유리. window glass, windowpane.
컴퓨터에서 쓸 때는 Pane 이 GUI 의 하나씩 쌓아올리는 구조 내부에서의 레이어(층) 이라고 본다.
프로젝트 내 src 내 javabasic 패키지 내 Ex7ImageMunje.java
package javabasic;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Ex7ImageMunje extends JFrame implements ActionListener {
String imageName = "C:\\java0901\\image\\귀여운 아이콘\\c1.png";
JButton btnLeft, btnRight, btnUp, btnDown;
// 이미지 초기 위치
int xPos = 140;
int yPos = 140;
// 캔바스 내부클래스
ImageMove im;
String imageName1 = "C:\\java0901\\image\\귀여운 아이콘\\c1.png";
// String imageName2="C:\\java0901\\image\\귀여운 아이콘\\c2.png";
// String imageName3="C:\\java0901\\image\\귀여운 아이콘\\c3.png";
// String imageName4="C:\\java0901\\image\\귀여운 아이콘\\c4.png";
JButton[] jb = new JButton[4];
String jbTitle[] = { "상", "하", "좌", "우" };
int x = 280;
int y = 280;
DrawImage dimage = new DrawImage();
Image image;
public Ex7ImageMunje(String title) {
// TODO Auto-generated constructor stub
super(title);
this.setBounds(700, 100, 1220, 900);// 시작위치x,y,크기 w,h
// super로 해도 되고 this로 해도 됨 super는 조상
// this로 해도 상속을 받아서 괜찮음
// this.getContentPane().setBackground(Color.orange);//프레임위에 있는 패널의 색상 변경
this.getContentPane().setBackground(new Color(211, 225, 208));// 프레임위에 있는 패널의 색상 변경
this.setDesign();// 디자인 코드
this.setVisible(true);// 보이게 하기
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 프로그램을 종료해주는 메서드
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object ob = e.getSource();// 이벤트가 발생한 오브젝트를
if (ob == btnLeft)
x = x - 20;
else if (ob == btnRight)
x = x + 20;
else if (ob == btnUp)
y = y - 20;
else if (ob == btnDown)
y = y + 20;
// im.repaint();// 캔바스 내부클래스가 가진 paint 다시 호출
dimage.repaint();
}
public void setDesign() {
this.setLayout(null);
btnLeft = new JButton("왼쪽");
btnLeft.setBounds(30, 20, 100, 30);
this.add(btnLeft);
btnRight = new JButton("오른쪽");
btnRight.setBounds(150, 20, 100, 30);
this.add(btnRight);
btnUp = new JButton("위");
btnUp.setBounds(270, 20, 100, 30);
this.add(btnUp);
btnDown = new JButton("아래");
btnDown.setBounds(390, 20, 100, 30);
this.add(btnDown);
btnLeft.addActionListener(this);
btnRight.addActionListener(this);
btnUp.addActionListener(this);
btnDown.addActionListener(this);
dimage.setBounds(20, 80, 760, 760);
dimage.setBackground(Color.BLACK);
this.add(dimage);
image = new ImageIcon(imageName1).getImage();
image = new ImageIcon(imageName).getImage();
im = new ImageMove();// 내부클래스 생성
im.setBounds(800, 80, 380, 380);
im.setBackground(Color.DARK_GRAY);
this.add(im);
int xPos = 815;
for (int i = 0; i < jb.length; i = i + 1) {
jb[i] = new JButton(jbTitle[i]);
jb[i].setBounds(xPos, 20, 80, 30);
jb[i].addActionListener(new jbClick());
this.add(jb[i]);
xPos = xPos + 90;
}
}
class jbClick implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object ob = e.getSource();
if (ob == jb[0]) {
yPos = yPos - 5;
im.repaint();
} else if (ob == jb[1]) {
yPos = yPos + 5;
im.repaint();
} else if (ob == jb[2]) {
xPos = xPos - 5;
im.repaint();
} else {
xPos = xPos + 5;
im.repaint();
}
}
}
class DrawImage extends Canvas {
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
System.out.println("DrawImage paint");
g.drawImage(image, x, y, 200, 200, this);
}
}
class ImageMove extends Canvas {
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
System.out.println("ImageMove paint");
g.drawImage(image, xPos, yPos, 100, 100, this);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Ex7ImageMunje("이미지이동");
}
}
'Java' 카테고리의 다른 글
Java Choice (0) | 2023.02.03 |
---|---|
Java JTextField (1) | 2023.02.02 |
Java FileDialog (0) | 2023.02.02 |
Java setDefaultCloseOperation(int operation) (1) | 2023.02.02 |
Java JRadioButton (0) | 2023.02.01 |