import javax.swing.*; //GUI用ライブラリを利用する
public class FrameSample {
public static void main(String[] args) {
JFrame app = new JFrame(); // 窓(フレーム)を表示するクラスJFrameのインスタンスを生成する
app.setSize(400, 300); // 窓の大きさを設定する
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 窓を閉じた(Xボタンを押した)時の動作(プログラムを終了する)を設定する.
app.setVisible(true); // 窓を可視化する
}
}
import java.awt.*; //描画用ライブラリ(クラスの入ったパッケージ)を利用する
import javax.swing.*; //GUI用ライブラリを利用する
public class Sample21 extends JPanel { //パネルのクラスJPanelを継承してクラスを作る
public void paintComponent(Graphics g) { //描画するメソッドをオーバーライドする
g.setColor(new Color(255, 180, 99)); //ペンの色を設定する
g.fillOval(100, 50, 100, 100); //楕円を描く
}
public static void main(String[] args) {
JFrame app = new JFrame(); //窓(フレーム)を表示するクラスJFrameのインスタンスを生成する
app.add(new Sample21()); //パネルを窓にはめる
app.setSize(400, 300); //窓の大きさを設定する
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //窓を閉じた(Xボタンを押した)時の動作(プログラムを終了する)を設定する.
app.setVisible(true); //窓を可視化する
}
}
import java.awt.*;
import javax.swing.*;
public class Ex21g1 extends JPanel {
public void paintComponent(Graphics g) {
g.setColor(new Color(250, 250, 0));
g.fillRect(150, 25, 100, 100);
g.setColor(new Color(150, 200, 120));
g.fillRect(100, 50, 100, 100);
g.setColor(new Color(20, 100, 220));
g.fillRect(125, 75, 100, 100);
g.setColor(new Color(250, 250, 0));
g.fillRect(150, 75, 75, 50);
g.setColor(Color.RED);
g.drawLine(100, 25, 250, 175);
g.drawLine(100, 175, 250, 25);
}
public static void main(String[] args) {
JFrame app = new JFrame();
app.add(new Ex21g1());
app.setSize(400, 300);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
public class Sample22 extends JPanel {
public void paintComponent(Graphics g) {
int x = 100; //変数の定義
g.setColor(new Color(220, 70, 250));
g.fillOval(x, 80, 40, 40); //変数の利用
x = x + 60; //変数の更新
g.fillOval(x, 80, 40, 40);
x = x + 60;
g.fillOval(x, 80, 40, 40);
x = x + 60;
g.fillOval(x, 80, 40, 40);
}
public static void main(String[] args) {
JFrame app = new JFrame();
app.add(new Sample22());
app.setSize(400, 300);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
}
public class MathTest {
public static void main(String[] args) {
System.out.println(Math.sin(Math.toRadians(90.0))); //sinの引数はラジアン
System.out.println(Math.sin(Math.PI / 2)); //πはMath.PIで表す
System.out.println(Math.sqrt(2.0)); //平方根はMath.sqrt
System.out.println((int) Math.sqrt(2.0)); //(int)をつけることで型変換(整数化)され小数点以下を切り捨てる.
}
}
Last updated: 2021/05/05
Author: ykitamura@kwansei.ac.jp