ネタ心者歓迎! 今更ながらにJava相談室

このエントリーをはてなブックマークに追加
285名無しの学生さん(♀)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Kadai5b extends JPanel {
CustomGraphics area;
ButtonPanel bp;
ComboPanel cp;
Kadai5b(){
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
area = new CustomGraphics(this);
bp = new ButtonPanel();
cp = new ComboPanel();
add(area);
add(bp);
add(cp);
}

public static void main(String args[]){
JFrame f = new JFrame("Draw Editor");
Kadai5b example = new Kadai5b();
f.getContentPane().add(example, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
286名無しの学生さん(♀):01/11/05 01:33

class CustomGraphics extends JPanel{
int x1, y1, x2, y2;
Kadai5b top;
CustomGraphics(Kadai5b t){
top = t;
setBackground (Color.white);
setMinimumSize (new Dimension(500, 500));
setPreferredSize(new Dimension(500, 500));
addMouseListener(new MyMouse());
addMouseMotionListener(new MyMouse1());
}

public void paintComponent(Graphics g){
super.paintComponent(g);
int xx, yy, xsize, ysize;
if (x1 < x2 ) {
xx = x1;
xsize = x2 - x1;
} else {
xx = x2;
xsize = x1 - x2;
}
if (y1 < y2) {
yy = y1;
ysize = y2 - y1;
} else {
yy = y2;
ysize = y1 - y2;
}
if (top.bp.type == 'r'){
g.setColor(top.cp.fillColor);
g.fillRect(xx, yy, xsize, ysize);
g.setColor(top.cp.drawColor);
g.drawRect(xx, yy, xsize, ysize);
} else if (top.bp.type == 'o'){
g.setColor(top.cp.fillColor);
g.fillOval(xx, yy, xsize, ysize);
g.setColor(top.cp.drawColor);
g.drawOval(xx, yy, xsize, ysize);
} else if (top.bp.type == 'l'){
g.setColor(top.cp.drawColor);
g.drawLine(x1, y1, x2, y2);
}
}

class MyMouse extends MouseAdapter{
public void mousePressed(MouseEvent e){
x1 = e.getX();
y1 = e.getY();
repaint();
}
public void mouseDragged(MouseEvent e){
x2 = e.getX();
y2 = e.getY();
repaint();
}
}
287名無しの学生さん(♀):01/11/05 01:34

class MyMouse1 extends MouseMotionAdapter{
public void mouseMoved (MouseEvent e){ }
public void mouseReleased(MouseEvent e){ }
public void mouseClicked (MouseEvent e){ }
public void mouseEntered (MouseEvent e){ }
public void mouseExited (MouseEvent e){ }
}
}
class ButtonPanel extends JPanel{
JButton lineB, rectB, ovalB;
int type = 'r';
InListener listener;
ButtonPanel(){
rectB = new JButton("四角");
ovalB = new JButton("楕円");
lineB = new JButton("直線");
listener = new InListener();
rectB.addActionListener(listener);
ovalB.addActionListener(listener);
lineB.addActionListener(listener);
add(rectB);
add(ovalB);
add(lineB);
}

class InListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (e.getSource() == rectB) { type = 'r'; }
else if (e.getSource() == ovalB) { type = 'o'; }
else if (e.getSource() == lineB) { type = 'l'; }
}
}
}
288名無しの学生さん(♀):01/11/05 01:34

class ComboPanel extends JPanel{
int drawInit = 1, fillInit = 0;
JLabel drawL, fillL;
JComboBox drawC, fillC;
JPanel drawP, fillP;
InListener1 listener1;
Color[] name = {Color.white, Color.black, Color.red, Color.yellow, Color.green, Color.blue};
Color drawColor = name[drawInit], fillColor = name[fillInit];
ComboPanel(){
String[] list = {"なし","黒","赤","黄","緑","青"};
drawL = new JLabel("線の色    ");
drawC = new JComboBox(list);
drawC.setSelectedIndex(drawInit);

drawC.addActionListener(listener1);
drawP = new JPanel();
drawP.setLayout(new GridLayout(2,1));
drawP.add(drawL);
drawP.add(drawC);
add(drawP);
fillL = new JLabel("塗りつぶしの色");
fillC = new JComboBox(list);
fillC.setSelectedIndex(fillInit);
fillC.addActionListener(listener1);
fillP = new JPanel();
fillP.setLayout(new GridLayout(2,1));
fillP.add(fillL);
fillP.add(fillC);
add(fillP);
}

class InListener1 implements ActionListener{
public void actionPerformed(ActionEvent e) {
drawColor = name[drawC.getSelectedIndex()];
fillColor = name[fillC.getSelectedIndex()];
}
}
}
}