プロ2 > 資料 > 第4章

第4章:自前のクラスを作る

目標

4.1 図形のクラス定義

従来の手続き的な図形の描画(非推奨)

import java.awt.*; import javax.swing.*; public class Sample41a extends JPanel { Color col1 = Color.MAGENTA, col2 = Color.BLUE; int xpos1 = 100, xpos2 = 190; int ypos1 = 50, ypos2 = 90; int rad1 = 30, rad2 = 40; public void paintComponent(Graphics g) { g.setColor(col1); g.fillOval(xpos1 - rad1, ypos1 - rad1, rad1 * 2, rad1 * 2); g.setColor(col2); g.fillOval(xpos2 - rad2, ypos2 - rad2, rad2 * 2, rad2 * 2); } public static void main(String[] args) { JFrame app = new JFrame(); app.add(new Sample41a()); app.setSize(400, 300); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setVisible(true); } }

オブジェクト指向的な図形の描画

import java.awt.*; import javax.swing.*; public class Sample41 extends JPanel { Circle c1 = new Circle(Color.MAGENTA, 100, 50, 30); // Circle c1の生成 Circle c2 = new Circle(Color.BLUE, 190, 90, 40); // Circle c2の生成 public void paintComponent(Graphics g) { c1.draw(g); //c1の描画 c2.draw(g); //c2の描画 } public static void main(String[] args) { JFrame app = new JFrame(); app.add(new Sample41()); app.setSize(400, 300); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setVisible(true); } static class Circle { //Circleのクラス定義 Color col; //Circleの色 int xpos, ypos, rad; //CircleのX座標,Y座標,半径 public Circle(Color c, int x, int y, int r) { //コンストラクタ col = c; xpos = x; ypos = y; rad = r; } public void draw(Graphics g) { //描画 g.setColor(col); g.fillOval(xpos - rad, ypos - rad, rad * 2, rad * 2); } } }

複数のクラスの定義

import java.awt.*; import javax.swing.*; public class Ex41cd extends JPanel { Circle c1 = new Circle(Color.MAGENTA, 100, 50, 30); Circle c2 = new Circle(Color.BLUE, 180, 120, 40); Rect r1 = new Rect(Color.YELLOW, 200, 80, 80, 50); //長方形の生成 Triangle t1 = new Triangle(Color.BLACK, 50, 120, 200, 80, 180, 20); //三角形の生成 public void paintComponent(Graphics g) { c1.draw(g); c2.draw(g); r1.draw(g); //長方形の描画 t1.draw(g); //三角形の描画 } public static void main(String[] args) { JFrame app = new JFrame(); app.add(new Ex41cd()); app.setSize(400, 300); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setVisible(true); } static class Circle { Color col; int xpos, ypos, rad; public Circle(Color c, int x, int y, int r) { col = c; xpos = x; ypos = y; rad = r; } public void draw(Graphics g) { g.setColor(col); g.fillOval(xpos - rad, ypos - rad, rad * 2, rad * 2); } } /* 長方形クラスの定義 */ static class Rect { Color col; int xpos, ypos, width, height; public Rect(Color c, int x, int y, int w, int h) { col = c; xpos = x; ypos = y; width = w; height = h; } public void draw(Graphics g) { g.setColor(col); g.fillRect(xpos - width / 2, ypos - height / 2, width, height); } } /* 三角形クラスの定義 */ static class Triangle { Color col; int[] xs, ys; public Triangle(Color c, int x0, int y0, int x1, int y1, int x2, int y2) { col = c; xs = new int[] { x0, x1, x2 }; ys = new int[] { y0, y1, y2 }; } public void draw(Graphics g) { g.setColor(col); g.fillPolygon(xs, ys, 3); } } }

4.2 複合図形を組み立てる

import java.awt.*; import javax.swing.*; public class Sample42 extends JPanel { Flag f1 = new Flag(Color.GREEN, Color.WHITE, 30, 100, 80); //フラグ1の生成 Flag f2 = new Flag(Color.GRAY, Color.RED, 20, 240, 120); //フラグ2の生成 public void paintComponent(Graphics g) { f1.draw(g); //フラグ1の描画 f2.draw(g); //フラグ2の描画 } public static void main(String[] args) { JFrame app = new JFrame(); app.add(new Sample42()); app.setSize(400, 300); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setVisible(true); } /* フラグクラスの定義 */ static class Flag { Rect r1; Circle c1; public Flag(Color cl1, Color cl2, int r, int x, int y) { r1 = new Rect(cl1, x, y, r * 6, r * 4); c1 = new Circle(cl2, x, y, r); } public void draw(Graphics g) { r1.draw(g); c1.draw(g); } } static class Circle { Color col; int xpos, ypos, rad; public Circle(Color c, int x, int y, int r) { col = c; xpos = x; ypos = y; rad = r; } public void draw(Graphics g) { g.setColor(col); g.fillOval(xpos - rad, ypos - rad, rad * 2, rad * 2); } } static class Rect { Color col; int xpos, ypos, width, height; public Rect(Color c, int x, int y, int w, int h) { col = c; xpos = x; ypos = y; width = w; height = h; } public void draw(Graphics g) { g.setColor(col); g.fillRect(xpos - width / 2, ypos - height / 2, width, height); } } }

4.3 マウスで図形を移動させる

(無名クラスを用いた)アダプタの登録

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Sample43 extends JPanel { Circle c1 = new Circle(Color.RED, 100, 50, 30); Circle c2 = new Circle(Color.BLUE, 150, 90, 40); public Sample43() { setOpaque(false); //画面を上描きしない(不透明)モードに設定する addMouseMotionListener(new MouseMotionAdapter() { //マウスに反応できるようにする public void mouseDragged(MouseEvent evt) { //マウスをドラグした時の動作定義 c1.moveTo(evt.getX(), evt.getY()); //マウスの位置までc1を移動させる repaint(); //再描画する.paintComponentを呼び出す. } }); //メソッドの引数の中にクラス定義が入っていることに注意. } public void paintComponent(Graphics g) { c1.draw(g); c2.draw(g); } public static void main(String[] args) { JFrame app = new JFrame(); app.add(new Sample43()); app.setSize(400, 300); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setVisible(true); } static class Circle { Color col; int xpos, ypos, rad; public Circle(Color c, int x, int y, int r) { col = c; xpos = x; ypos = y; rad = r; } public void moveTo(int x, int y) { xpos = x; ypos = y; } public void draw(Graphics g) { g.setColor(col); g.fillOval(xpos - rad, ypos - rad, rad * 2, rad * 2); } } }

個別にクラス定義したアダプタの登録

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Sample43a extends JPanel { Circle c1 = new Circle(Color.RED, 100, 50, 30); Circle c2 = new Circle(Color.BLUE, 150, 90, 40); public Sample43a() { setOpaque(false); // 画面を上描きしない(不透明)モードに設定する addMouseMotionListener(new MyAdapter1()); // マウスリスナーとしてアダプタを登録する } class MyAdapter1 extends MouseMotionAdapter { //マウス用アダプタの定義 public void mouseDragged(MouseEvent evt) { // マウスをドラグした時の動作定義 c1.moveTo(evt.getX(), evt.getY()); // マウスの位置までc1を移動させる repaint(); // 再描画する.paintComponentを呼び出す. } } /* 以下は同じ */ public void paintComponent(Graphics g) { c1.draw(g); c2.draw(g); } public static void main(String[] args) { JFrame app = new JFrame(); app.add(new Sample43a()); app.setSize(400, 300); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setVisible(true); } static class Circle { Color col; int xpos, ypos, rad; public Circle(Color c, int x, int y, int r) { col = c; xpos = x; ypos = y; rad = r; } public void moveTo(int x, int y) { xpos = x; ypos = y; } public void draw(Graphics g) { g.setColor(col); g.fillOval(xpos - rad, ypos - rad, rad * 2, rad * 2); } } }


ykitamura@kwansei.ac.jp