import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
public class JSplitPaneTest2 extends JFrame{
JSplitPane sp1, sp2;
public JSplitPaneTest2() {
sp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new CalculatorT(), new SwingCalendar());
sp2.setDividerLocation(385);
sp1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new Grimpan(), sp2);
sp1.setDividerLocation(300);
add(sp1, "Center");
setSize(800, 600);
setVisible(true);
}
public static void main(String[] args) {
new JSplitPaneTest2();
}
}
=====================================================================
그림판
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Grimpan extends JPanel implements ActionListener {
JPanel p = new JPanel(new GridLayout(1, 0));
JButton penTool = new JButton("펜");
JButton lineTool = new JButton("선");
JButton ovalTool = new JButton("원");
JButton rectTool = new JButton("사각형");
MyCanvas mc = new MyCanvas();
JPanel p2 = new JPanel(new GridLayout(1, 0));
JButton red = new JButton("Red");
JButton green = new JButton("Green");
JButton blue = new JButton("Blue");
JButton yellow = new JButton("Yellow");
String tool = "선", color = "Blue";
public Grimpan() {
setLayout(new BorderLayout());
p.add(penTool); p.add(lineTool); p.add(ovalTool); p.add(rectTool);
add(p, "North");
add(mc, "Center");
red.setBackground(Color.red); green.setBackground(Color.GREEN); blue.setBackground(Color.BLUE); blue.setForeground(Color.WHITE); yellow.setBackground(Color.YELLOW);
p2.add(red); p2.add(green); p2.add(blue); p2.add(yellow);
add(p2, "South");
setSize(500, 500);
setVisible(true);
penTool.addActionListener(this);
lineTool.addActionListener(this);
ovalTool.addActionListener(this);
rectTool.addActionListener(this);
red.addActionListener(this);
green.addActionListener(this);
blue.addActionListener(this);
yellow.addActionListener(this);
}
class MyCanvas extends Canvas implements MouseListener, MouseMotionListener {
int firstX, firstY, lastX, lastY;
int x, y;
MyCanvas() {
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g) {
// color
if (color.equals("Red"))
g.setColor(Color.RED);
else if (color.equalsIgnoreCase("Green"))
g.setColor(Color.GREEN);
else if (color.equals("Blue"))
g.setColor(Color.BLUE);
else if (color.equals("Yellow"))
g.setColor(Color.YELLOW);
// type of drawing tool
if (tool.equals("선"))
g.drawLine(firstX, firstY, lastX, lastY);
else if (tool.equalsIgnoreCase("원"))
g.drawOval(firstX, firstY, (lastX - firstX), (lastY - firstY));
else if (tool.equals("사각형"))
g.drawRect(firstX, firstY, lastX - firstX, lastY - firstY);
else if (tool.equals("펜")) {
g.drawLine(x, y, lastX, lastY);
x = lastX;
y = lastY;
}
}
public void update(Graphics g) {
paint(g);
}
@Override
public void mouseClicked(MouseEvent me) {};
@Override
public void mouseEntered(MouseEvent me) {};
@Override
public void mouseExited(MouseEvent me) {};
@Override
public void mousePressed(MouseEvent me) {
// System.out.println("pressed");
firstX = me.getX();
firstY = me.getY();
x = firstX;
y = firstY;
}
@Override
public void mouseReleased(MouseEvent me) {
// System.out.println("Released");
lastX = me.getX();
lastY = me.getY();
repaint();
}
// MouseMotionListener
@Override
public void mouseDragged(MouseEvent me) {
if (tool.equals("펜")) {
lastX = me.getX();
lastY = me.getY();
repaint();
}
}
@Override
public void mouseMoved(MouseEvent me) {};
}
@Override
public void actionPerformed(ActionEvent ae) {
String eventStr = ae.getActionCommand();
if (eventStr.equals("펜") || eventStr.equals("선") || eventStr.equals("원") || eventStr.equals("사각형"))
tool = eventStr;
else
color = eventStr;
// System.out.println("tool = " + tool + ", " + "color = " + color);
}
public static void main(String[] args) {
new Grimpan();
}
}
'응용 SoftWare > JAVA' 카테고리의 다른 글
Thread Test 1 (0) | 2016.12.23 |
---|---|
Class Test (0) | 2016.12.23 |
JSplitPane 예제 (0) | 2016.12.21 |
JTree 예제 (0) | 2016.12.21 |
Thread를 이용한 디지털 시계 (0) | 2016.12.21 |