プロ2 > 資料 > 第3章

第3章:繰り返しと枝分かれ

目標

3.1 while文による繰り返し

import java.awt.*; import javax.swing.*; public class Sample31 extends JPanel { public void paintComponent(Graphics g) { g.setColor(new Color(220, 70, 250)); int x = 100; while (x < getWidth()) { //パネルの幅に至るまで繰り返す g.fillOval(x, 80, 40, 40); x = x + 60; } } public static void main(String[] args) { JFrame app = new JFrame(); app.add(new Sample31()); app.setSize(400, 300); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setVisible(true); } }
import java.awt.*; import javax.swing.*; public class Sample31a extends JPanel { public void paintComponent(Graphics g) { g.setColor(new Color(220, 70, 250)); for (int x = 100; x < getWidth(); x += 60) g.fillOval(x, 80, 40, 40); } public static void main(String[] args) { JFrame app = new JFrame(); app.add(new Sample31a()); app.setSize(400, 300); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setVisible(true); } }

3.2 計数ループと配列

import java.awt.*; import javax.swing.*; public class Sample32 extends JPanel { public void paintComponent(Graphics g) { g.setColor(new Color(240, 50, 240)); ((Graphics2D) g).setStroke(new BasicStroke(4)); //多角形の線を太くする int[] xs = new int[8], ys = new int[8]; //配列の定義 xs[0] = 100; for (int i = 1; i < 8; ++i) { xs[i] = xs[i - 1] + 20; } ys[0] = ys[2] = ys[4] = ys[6] = 50; ys[1] = ys[3] = ys[5] = ys[7] = 150; g.drawPolyline(xs, ys, 8); //折れ線の描画 } public static void main(String[] args) { JFrame app = new JFrame(); app.add(new Sample32()); app.setSize(400, 300); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setVisible(true); } }

3.3 枝分かれ

import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class Sample33a extends JPanel { public void paintComponent(Graphics g) { g.setColor(new Color(220, 70, 240)); int x = 50, dx = 50; int y = 80, dy = 30; for (int i = 0; i < 20; ++i) { g.fillOval(x, y, 40, 40); if (y + 80 > getHeight()) { //フレームの下辺にぶつかると上昇する dy = -dy; } x = x + dx; y = y + dy; } } public static void main(String[] args) { JFrame app = new JFrame(); app.add(new Sample33a()); app.setSize(400, 300); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setVisible(true); } }

3.4 ファイルの中の絵を表示する

import java.awt.*; import javax.swing.*; import java.awt.image.*; //画像の作成・修正するためのパッケージ import javax.imageio.*; //Java Image I/Oの基本パッケージ import java.io.*; //ファイル読み書きのためのパッケージ public class Sample34b extends JPanel { BufferedImage[] img = new BufferedImage[2]; // 画像のための配列 public Sample34b() { // コンストラクタ try { // 例外(エラー)処理 img[0] = ImageIO.read(new File("cat.png")); // ファイルから画像を読み込み img[1] = ImageIO.read(new File("dog.png")); // ファイルから画像を読み込み } catch (Exception ex) { ex.printStackTrace(); //エラー表示 System.exit(-1); //強制終了 } } public void paintComponent(Graphics g) { for (int i = 0; i < 4; i++) { int w = img[i%2].getWidth(), h = img[i%2].getHeight(); // 画像の幅と高さを得る g.drawImage(img[i%2], i*50, i*50, w, h, null); // 画像を描画する } } public static void main(String[] args) { JFrame app = new JFrame(); app.add(new Sample34b()); app.setSize(400, 400); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setVisible(true); } }

3.5 カレンダーを扱うクラス

import java.util.*; //グレゴリオ暦を使うためのパッケージ class WhatDateGregorian { public static void main(String[] args) { String[] day = { "日", "月", "火", "水", "木", "金", "土" };// 文字配列の宣言と初期化 Calendar today = new GregorianCalendar(); // 今日のグレゴリオ暦を取得する int year = today.get(Calendar.YEAR); // 今日の年を得る int month = today.get(Calendar.MONTH) + 1; // 今日の月(0~11)を得る int date = today.get(Calendar.DATE); // 今日の日を得る int m_len = today.getActualMaximum(Calendar.DATE); // 今月の日数を得る System.out.println("今日は" + year + "年" + month + "月" + date + "日です"); System.out.println("今月は" + m_len + "日あります"); Calendar cal1 = new GregorianCalendar(year - 1, month - 1, date); // 1年前のグレゴリオ暦を得る int w = cal1.get(Calendar.DAY_OF_WEEK) - 1; // 1年前の曜日(1~7)を得る System.out.println("1年前は" + day[w] + "曜日です"); } }

3.6 カレンダーの作り方

カレンダー

  1. その月の1日が何曜日であるかを求める. これをwとする.w=0なら日曜,w=1なら月曜,...
  2. その月のn日のカレンダー上の座標[px, py]を求める.
    px=(w+(n-1))%7
    py=(w+(n-1))/7
  3. nを描画するフレーム上の座標は[C*px+A, D*py+B]となる.

Last updated: 2021/05/13
Author: ykitamura@kwansei.ac.jp