새턴
Purpose is what gives life a meaning
2013년 9월 15일 일요일
2013년 9월 14일 토요일
2013년 9월 10일 화요일
2010년 3월 9일 화요일
Browser 객체
웹서핑(Web Surfing)을 하다 보면 흔히 만나게 되는 것이 광고나 Event, 여러 가지 공지사항 등을 작은 창에 띄우는 것입니다. 자바스크립트를 이용하면 이와같은 Window들을 다양한 방법으로 제어할 수 있습니다.
I. Window Open
[형식]
winObject = window.open("url", "name", "options");
* winObject는 open된 window를 가리키는 reference변수이다.
* url (반드시 있어야 한다)
새로 만들어진 window에 나타날 web문서의 URL을 적는다. 적당한 url이 없을 경우 ""로 표시한다.
* name (반드시 있어야 한다)
새로 만들어진 window의 이름을 지정한다. 적당한 name을 붙이지 않으려면 ""로 표시하면 된다. name위치에 입력한 이름은 <FORM>이나 <A>태그에서 사용하는 Target속성으로 연결할 수 있다.
* options (생략가능하며 생략하는 경우 default값이 적용된다)
1. menubar : window의 menubar표시 여부 (yes/no or 1/0)
2. toolbar : window의 toolbar표시 여부 (yes/no or 1/0)
3. location : window의 location box표시여부 (yes/no or 1/0)
4. directories : window의 directory button들의 출력여부 (yes/no or 1/0)
5. status : window의 상태표시줄 표시 여부 (yes/no or 1/0)
6. scrollbars : window의 가로, 세로 scrollbar표시 여부 (yes/no or 1/0)
7. resizable : window의 크기가 조절 될 수 있는지 결정 (yes/no or 1/0)
8. width : Display될 window의 너비 결정 (단위 pixel)
9. height : Display될 window의 높이 결정 (단위 pixel)
10. left : Display될 window의 좌측 상단의 x좌표 (단위 pixel)
11. top : Display될 window의 좌측 상단의 x좌표 (단위 pixel)
II. Window Close
[형식]
window.close();
* window를 종료할 때는 반드시 Window가 open이 되었는지 check한 다음 close하도록 한다.
2009년 7월 22일 수요일
AWT를 이용한 간단한 - 계산기 -
import java.awt.Frame;
import java.awt.TextField;
import java.awt.Button;
import java.awt.Panel;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.Color;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Calculator extends Frame implements WindowListener, ActionListener{
TextField result;
Button[] button;
boolean num_state = true;
boolean operator_state=true;
String op_temp;
String num_temp;
public void init() {
result = new TextField("0", 30);
result.setEditable(false);
String[] buttonTitle = {"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"C", "0", "=", "+"};
button = new Button[16];
Panel p = new Panel();
p.setLayout(new GridLayout(4,4,3,3));
for (int i=0; i<button.length; i++ ) {
button[i] = new Button(buttonTitle[i]);
button[i].setBackground(new Color(58, 226, 206));
p.add(button[i]);
button[i].addActionListener(this);
} //for
this.add("North", result);
this.add("Center", p);
this.setTitle("Calculator");
this.setBounds(600,300,200,200);
this.setVisible(true);
this.addWindowListener(this);
}//init()
public void windowActivated(WindowEvent e) { }
public void windowClosed(WindowEvent e) { }
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowDeactivated(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowOpened(WindowEvent e) { }
public void actionPerformed(ActionEvent e) {
String in = e.getActionCommand();
if(in.equals("C")){
result.setText("0");
num_state = true;
}else if('0' <= in.charAt(0) && in.charAt(0) <= '9') {
if (num_state)
result.setText(in);
else
result.setText(result.getText() +in);
num_state = false;
}
if(in.equals("/") || in.equals("*") || in.equals("-") || in.equals("+")) {
num_temp = result.getText();
op_temp = in;
num_state=true;
}else if(in.equals("=")) {
if(operator_state) {
double num1 = Double.parseDouble(num_temp);
double num2 = Double.parseDouble(result.getText());
if(op_temp.equals("+")) {
result.setText("" + (num1+num2));
}else if(op_temp.equals("-")) {
result.setText("" + (num1-num2));
}else if(op_temp.equals("*")) {
result.setText("" + (num1*num2));
}else if(op_temp.equals("/")) {
result.setText("" + (num1/num2));
}
}
}
}
public static void main(String[] args) {
Calculator ct = new Calculator();
ct.init();
}
}
Swing을 이용한 간단한 - 그림판 -
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import java.awt.FlowLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.*;
class GraphicTest extends JFrame implements ActionListener {
JLabel x1L, y1L, x2L, y2L, z1L, z2L;
JTextField x1T, y1T, x2T, y2T, z1T, z2T;
JCheckBox fill;
JRadioButton line, circle, rect, roundR, arc;
JButton draw;
DrCanvas can;
JComboBox combo;
int x1, y1, x2, y2, z1, z2;
public GraphicTest() {
super("간단 그림판");//setTitle("간단 그림판"); //super는 맨위쪽
x1L = new JLabel("x1L");
y1L = new JLabel("y1L");
x2L = new JLabel("x2L");
y2L = new JLabel("y2L");
z1L = new JLabel("z1L");
z2L = new JLabel("z2L");
x1T = new JTextField("100",4);
y1T = new JTextField("150",4);
x2T = new JTextField("200",4);
y2T = new JTextField("250",4);
z1T = new JTextField("80",4);
z2T = new JTextField("80",4);
fill = new JCheckBox("채우기");
line = new JRadioButton("선");
circle = new JRadioButton("원");
rect = new JRadioButton("사각형", true);
roundR = new JRadioButton("둥근사각형");
arc = new JRadioButton("호");
ButtonGroup bg = new ButtonGroup();
bg.add(line);
bg.add(circle);
bg.add(rect);
bg.add(roundR);
bg.add(arc);
draw = new JButton("그리기");
can = new DrCanvas();
String[] color = {"빨강", "초록", "파랑", "검정", "보라"};
combo = new JComboBox(color);
JPanel p = new JPanel(); //FlowLayout(순서배치)
p.add(x1L); p.add(x1T);
p.add(y1L); p.add(y1T);
p.add(x2L); p.add(x2T);
p.add(y2L); p.add(y2T);
p.add(z1L); p.add(z1T);
p.add(z2L); p.add(z2T);
p.add(fill);
JPanel p2 = new JPanel();
p2.add(line);
p2.add(circle);
p2.add(rect);
p2.add(roundR);
p2.add(arc);
p2.add(combo);
p2.add(draw);
getContentPane().add("North", p);
getContentPane().add("South", p2);
getContentPane().add("Center", can);
setBounds(500,100,600,500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//이벤트 처리
draw.addActionListener(this);
line.addActionListener(this);
circle.addActionListener(this);
rect.addActionListener(this);
roundR.addActionListener(this);
arc.addActionListener(this);
can.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
x1T.setText(String.valueOf(e.getX()));
y1T.setText(String.valueOf(e.getY()));
can.repaint();
}
});
can.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
x2T.setText(String.valueOf(e.getX()));
y2T.setText(String.valueOf(e.getY()));
can.repaint();
}
});
}//GraphicTest( )
//ActionListner override
public void actionPerformed(ActionEvent e){
can.repaint(); //update () -> paint() 순서로 호출
}
class DrCanvas extends Canvas {
public DrCanvas() {
this.setBackground(new Color(255,255,100)); //바탕색 -연한노랑
setForeground(new Color(255,0,0));//글자색
}//DrCanvas( )
public void paint(Graphics g) {
//좌표 얻어오기
x1 = Integer.parseInt(x1T.getText().trim()); //trim - 앞뒤공백제거
y1 = Integer.parseInt(y1T.getText().trim());
x2 = Integer.parseInt(x2T.getText().trim());
y2 = Integer.parseInt(y2T.getText().trim());
z1 = Integer.parseInt(z1T.getText().trim());
z2 = Integer.parseInt(z2T.getText().trim());
//색
switch(combo.getSelectedIndex()) {
case 0: this.setForeground(new Color (255,0,0));
break;
case 1: this.setForeground(new Color (0,255,0));
break;
case 2: this.setForeground(new Color (0,0,255));
break;
case 3: this.setForeground(new Color (0,0,0));
break;
case 4: this.setForeground(new Color (255,0,255));
break;
}//switch
int startX = x1 < x2 ? x1 : x2;
int startY = y1 < y2 ? y1 : y2;
int width = x1 < x2 ? x2-x1 : x1-x2;
int height = y1 < y2 ? y2-y1 : y1-y2;
//도형 그리기
if(fill.isSelected()) { //채우기 선택
if(line.isSelected())
g.drawLine(x1, y1, x2, y2);
else if(circle.isSelected())
g.fillOval(startX, startY, width, height);
else if(rect.isSelected())
g.fillRect(startX, startY, width, height);
else if(roundR.isSelected())
g.fillRoundRect(startX, startY, width, height, z1, z2);
else if(arc.isSelected())
g.fillArc(startX, startY, width, height, z1, z2);
}else { //채우기 해제
if(line.isSelected())
g.drawLine(x1, y1, x2, y2);
else if(circle.isSelected())
g.drawOval(startX, startY, width, height);
else if(rect.isSelected())
g.drawRect(startX, startY, width, height);
else if(roundR.isSelected())
g.drawRoundRect(startX, startY, width, height, z1, z2);
else if(arc.isSelected())
g.drawArc(startX, startY, width, height, z1, z2);
}//else
}//paint( )
} //DrCanvas
public static void main(String[] args) {
new GraphicTest();
}//main( )
}






























