## コンピュータグラフィックス 演習

[注意] 本年度の授業ではこの課題を提出する必要はありません。

### 課題1a: ベジエ曲線を描画する

 Bezier.javaの実装例 ```import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import java.util.*; public class Bezier extends ToyGraphics { Vector vp; Vector vr; public Bezier() { this(640,480); } public Bezier(int w,int h) { super(w,h); vp = new Vector(); vr = new Vector(); } Point2D.Double get(Point2D.Double p0,Point2D.Double p1,Point2D.Double p2,Point2D.Double p3,double t) { /* 課題: [0,1]なるtに対して、ベジエ曲線上の点の座標を返すこと */ return new Point2D.Double(x,y); } public static void main(String[] args) { Bezier bz = new Bezier(); }; public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); int button = e.getButton(); System.out.println("mousePressed:("+x+","+y+")"+button); vp.add(new Point2D.Double(x,y)); g2d.setColor(Color.red); g2d.fillRect(x-5,y-5,10,10); repaint(0,0,width,height); } public void mouseReleased(MouseEvent e) { int x = e.getX(); int y = e.getY(); int button = e.getButton(); System.out.println("mouseReleased:("+x+","+y+")"+button); vr.add(new Point2D.Double(x,y)); g2d.setColor(Color.blue); g2d.draw(new Rectangle2D.Double(x-5,y-5,10,10)); if (vr.size() == 1) { repaint(0,0,width,height); return;} Point2D.Double p3 = vp.lastElement(); Point2D.Double p0 = vp.get(vp.size()-2); Point2D.Double p1 = vr.get(vr.size()-2); double cx = p3.getX() * 2 - x; double cy = p3.getY() * 2 - y; Point2D.Double p2 = new Point2D.Double(cx,cy); g2d.setColor(Color.green); g2d.draw(new Rectangle2D.Double(p2.x-5,p2.y-5,10,10)); g2d.draw(new Line2D.Double(x,y,p2.getX(),p2.getY())); g2d.setColor(Color.white); for (double t = 0; t<=1.0; t+=0.005) { Point2D.Double p = get(p0,p1,p2,p3,t); g2d.draw(new Line2D.Double(p.getX(),p.getY(),p.getX(),p.getY())); } repaint(0,0,width,height); } } ```
 ToyGraphics.java ```import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import java.awt.image.*; import java.util.*; import javax.swing.*; class ToyGraphics extends JComponent implements MouseListener { int width, height; BufferedImage image; protected Graphics2D g2d; public boolean isMousePressed = false; public void initImage() { initImage(1024,768); } public void initImage(int w,int h) { width = w; height = h; image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); g2d = image.createGraphics(); setFont(); clear(); } public void initFrame() { JFrame frame = new JFrame("Graphic Window"); Container c = frame.getContentPane(); Dimension size = getPreferredSize(); int w = size.width; int h = size.height; c.setSize(w,h); c.add(this,BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } public Dimension getPreferredSize() { return new Dimension(image.getWidth(),image.getHeight()); } public ToyGraphics() { initImage(); initFrame(); } public ToyGraphics(int w,int h) { initImage(w,h); initFrame(); addMouseListener(this); } public void setFont() { setFont("Serif",24); } public void setFont(String name,int size) { g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font font = new Font(name,Font.PLAIN,size); g2d.setFont(font); } public void setColor(Color col) { g2d.setColor(col); } public void drawText(String txt,int x,int y) { g2d.drawString(txt,x,y); } public void paint(Graphics g) { g.drawImage(image,0,0,this); } public static int rgb2pixel(int r,int g,int b) throws IllegalArgumentException { return rgb2pixel(255,r,g,b); } public static int rgb2pixel(int a,int r,int g,int b) throws IllegalArgumentException { if (a<0 || a>255 || r<0 || r>255 || g<0 || g>255 || b<0 || b>255) throw new IllegalArgumentException("bad color:("+a+","+r+","+g+","+b+")"); return (a<<24) | (r << 16) | (g << 8) | (b); } public void drawRGB(int x,int y,int r,int g,int b) { setRGB(x,y,r,g,b); repaint(x,y,1,1); } public void drawRGB(int x,int y,int pxl) { setRGB(x,y,pxl); repaint(x,y,1,1); } public void setRGB(int x,int y,int r,int g,int b) { setRGB(x,y,rgb2pixel(r,g,b)); } public void setRGB(int x,int y,int pxl) { if (x < 0 || x>width || y<0 || y>height) return; image.setRGB(x,y,pxl); } public void clear() { for (int y=0; y