Skip to content

Instantly share code, notes, and snippets.

@yakimelon
Last active July 31, 2017 08:03
Show Gist options
  • Save yakimelon/32e898fe463065430636ca2f4769b960 to your computer and use it in GitHub Desktop.
Save yakimelon/32e898fe463065430636ca2f4769b960 to your computer and use it in GitHub Desktop.
package main;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("unchecked")
class Paint4 extends Frame implements MouseListener,MouseMotionListener,ActionListener{
int x,y;
Vector<Figure> objList;
CheckboxGroup cbg,clr,dof;
Checkbox c1,c2,c3,c4,c5,red,green,blue,draw,full;
Button end;
int mode =0;
Figure obj;
public static void main(String[] args){
Paint4 f =new Paint4();
f.setSize(640,640);
f.setTitle("Paint Sample");
f.addWindowListener(new WindowAdapter(){
@Override public void windowClosing(WindowEvent e){
System.exit(0);
}});
f.setVisible(true);
if(args.length ==1) f.load(args[0]);
//f.load("paint.dat");
}
Paint4(){
objList = new Vector<Figure>();
// UIの設定
cbg = new CheckboxGroup();
clr = new CheckboxGroup();
dof = new CheckboxGroup();
c1 = new Checkbox("maru",cbg,true);
c2 = new Checkbox("en",cbg,false);
c3 = new Checkbox("shikaku",cbg,false);
c4 = new Checkbox("sen",cbg,false);
c5 = new Checkbox("origin",cbg,false);
end = new Button("end");
red = new Checkbox("red",clr,true);
green = new Checkbox("green",clr,false);
blue = new Checkbox("blue",clr,false);
draw = new Checkbox("draw",dof,true);
full = new Checkbox("full",dof,false);
c1.setBounds(560,30,80,30);
c2.setBounds(560,60,80,30);
c3.setBounds(560,90,90,30);
c4.setBounds(560,120,80,30);
c5.setBounds(560,150,80,30);
end.setBounds(560,300,80,30);
red.setBounds(560,360,80,30);
green.setBounds(560,390,80,30);
blue.setBounds(560,420,80,30);
draw.setBounds(560,480,80,30);
full.setBounds(560,510,80,30);
setLayout(null);
// UIの追加
add(c1);
add(c2);
add(c3);
add(c4);
add(c5);
add(end);
add(red);
add(green);
add(blue);
add(draw);
add(full);
addMouseListener(this);
addMouseMotionListener(this);
end.addActionListener(this);
}
public void save(String fname){
try{
FileOutputStream fos = new FileOutputStream(fname);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(objList);
oos.close();
fos.close();
} catch(IOException e){
}
}
public void load(String fname){
try{
FileInputStream fis = new FileInputStream(fname);
ObjectInputStream ois = new ObjectInputStream(fis);
objList = (Vector<Figure>)ois.readObject();
ois.close();
fis.close();
} catch(IOException e){
} catch(ClassNotFoundException e){
}
repaint();
}
@Override public void paint(Graphics g){
Figure f;
for(int i=0;i< objList.size();i++){
f=(Figure)objList.elementAt(i);
f.paint(g);
}
if(mode>=1) obj.paint(g);
}
@Override public void actionPerformed(ActionEvent e){
save("paint.dat");
System.exit(0);
}
public void mousePressed(MouseEvent e){
Checkbox c;
Checkbox i;
Checkbox d;
x = e.getX();
y = e.getY();
c =cbg.getSelectedCheckbox();
i =clr.getSelectedCheckbox();
d =dof.getSelectedCheckbox();
obj=null;
if(i==red){
Figure.color=1;
}else if(i==green){
Figure.color=2;
}else if(i==blue){
Figure.color=3;
}
if(c==c1){
mode=1;
if(d==draw) obj=new Ring();
else if(d==full) obj=new Ring2();
}else if(c==c2){
mode=2;
if(d==draw) obj=new Circle();
else if(d==full) obj=new Circle2();
}else if(c==c3){
mode=2;
if(d==draw) obj=new Box();
else if(d==full) obj=new Box2();
}else if(c==c4){
mode=2;
obj=new Line();
}else if(c==c5){
mode=2;
obj=new Origin();
}
if(obj != null){
obj.moveto(x,y);
repaint();
}
}
@Override public void mouseReleased(MouseEvent e){
x=e.getX();
y=e.getY();
if(mode ==1) obj.moveto(x,y);
else if(mode==2) obj.setWH(x - obj.x,y - obj.y);
if(mode >=1){
objList.add(0,obj);
obj = null;
}
mode = 0;
repaint();
}
@Override public void mouseClicked(MouseEvent e){}
@Override public void mouseEntered(MouseEvent e){}
@Override public void mouseExited(MouseEvent e){}
@Override public void mouseDragged(MouseEvent e){
x=e.getX();
y=e.getY();
if(mode==1){
obj.moveto(x,y);
}else if(mode==2){
obj.setWH(x - obj.x ,y - obj.y);
}
repaint();
}
@Override public void mouseMoved(MouseEvent e){}
}
class Coord implements Serializable{
int x,y;
Coord(){
x=y=0;
}
public void move(int x,int y){
this.x += x;
this.y += y;
}
public void moveto(int x,int y){
this.x = x;
this.y = y;
}
}
class Figure extends Coord implements Serializable{
static int color;
int w,h;
Color clr;
Figure(){
if(color==1) clr=new Color(255,0,0);
else if(color==2) clr=new Color(0,255,0);
else if(color==3) clr=new Color(0,0,255);
w=h=0;
}
public void paint(Graphics g){}
public void setWH(int w,int h){
this.w=w;
this.h=h;
}
}
class Ring extends Figure implements Serializable{
int size;
Ring(){
size=10;
}
@Override public void paint(Graphics g){
g.setColor(clr);
g.drawOval(x - size/2, y - size/2, size, size);
}
}
class Ring2 extends Figure implements Serializable{
int size;
Ring2(){
size=10;
}
@Override public void paint(Graphics g){
g.setColor(clr);
g.fillOval(x - size/2, y - size/2, size, size);
}
}
class Circle extends Figure implements Serializable{
Circle(){}
@Override public void paint(Graphics g){
int r=(int)Math.sqrt((double)(w*w+h*h));
g.setColor(clr);
g.drawOval(x-r,y-r,r*2,r*2);
}
}
class Circle2 extends Figure implements Serializable{
Circle2(){}
@Override public void paint(Graphics g){
int r=(int)Math.sqrt((double)(w*w+h*h));
g.setColor(clr);
g.fillOval(x-r,y-r,r*2,r*2);
}
}
class Box extends Figure implements Serializable{
Box(){}
@Override public void paint(Graphics g){
g.setColor(clr);
if(w>0 && h>0){
g.drawRect(x,y,w,h);
}
else if(w<0 && h>0){
g.drawRect(x+w,y,-w,h);
}
else if(w>0 && h<0){
g.drawRect(x,y+h,w,-h);
}
else if(w<0 && h<0){
g.drawRect(x+w,y+h,-w,-h);
}
}
}
class Box2 extends Figure implements Serializable{
Box2(){}
@Override public void paint(Graphics g){
g.setColor(clr);
if(w>0 && h>0){
g.fillRect(x,y,w,h);
}
else if(w<0 && h>0){
g.fillRect(x+w,y,-w,h);
}
else if(w>0 && h<0){
g.fillRect(x,y+h,w,-h);
}
else if(w<0 && h<0){
g.fillRect(x+w,y+h,-w,-h);
}
}
}
class Line extends Figure implements Serializable{
Line(){}
@Override public void paint(Graphics g){
g.setColor(clr);
g.drawLine(x,y,x+w,x+h);
}
}
class Origin extends Figure implements Serializable{
@Override public void paint(Graphics g){
int r=(int)Math.sqrt((double)(w*w+h*h));
g.setColor(clr);
g.drawOval(x-r,y-r,r*2,r*2);
int r2=(int)Math.sqrt((double)((w-50)*(w-50)+(h-50)*(h-50)));
g.setColor(clr);
g.drawOval(x-r2,y-r2,r2*2,r2*2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment