java 構文

条件分岐

int score = 35;
    if (score > 80){
      System.out.println("great");
    } else if (score> 60){
      System.out.println("good");
    } else {
    System.out.println("soso");
  }
int score = 65;
  String msg = score > 80 ? "great" : "soso...";

switch

String signal = "red";
    switch (signal){
      case "red":
        System.out.println("stop!");
        break;
      case "blue":
        System.out.println("go");
        break;
      case "yellow":
        System.out.println("caution");
        break;
      default:
        System.out.println("wrong signal");
        break;

while, do while

int i = 0;
    while (i < 10){
      System.out.println(i);
      i++;
    } 
int i = 0;
    do {
      System.out.println(i);
      i++;
    } while (i < 10);

for

for(int i = 0; i < 10; i++){
        if (i == 5){
          break;
        }
        System.out.println(i);
      }

array

      int[] sales;
      sales = new int[3];
      sales[0] = 100;
      sales[1] = 200;
      sales[2] = 300;
      sales[1] = 1000;
      System.out.println(sales[1]);
      // int[] sales:
      // sales = new int[](100, 200, 300);

      int[] sales = {100, 200, 300}

要素の操作

int[] sales = {700, 300, 500};

      for (int i = 0; i < sales.length; i++){
        System.out.println(sales[i]);
      }

int[] sales = {700, 300, 500};

      for (int sale : sales){
        System.out.println(sale);
      }

参照型にはメモリ領域のバッチが入ります。文字列の場合は、変更不可となります。

      int[] a = {3, 5, 7};
      int[] b = a;
      b[1] = 8;
      System.out.println(a[1]);
      System.out.println(b[1]);
      String s = "hello";
      String t = s;
      t = "world";

メソッド

public static void sayHi(String name){
    System.out.println("hi!" + name);
  }

  public static void main(String[] args){
    sayHi("jack");
  }

public static String sayHi(String name){
    return "hi!" + name;
  }

  public static void main(String[] args){
    String msg = sayHi("jack");
    System.out.println(msg);
  }

オーバーロード

public static void sayHi(){
    System.out.printl("hi! nobody");
  }

  public static void main(String[] args){
    sayHi("jack"); //引数
    sayHi();
  }

open-java JDK8

開発環境にJDKをインストールします。javaは.classにコンパイルすれば、Linux以外のOS(mac, windows等)のJRE(Java Runtime Environment)/JVM(Java Virtual Machine)で実行することが可能です。Run anywhereの思想です。

http://openjdk.java.net/install/

[vagrant@localhost java]$ sudo yum -y install java-1.8.0-openjdk-devel

ターミナルからのコンパイル方法です。

public class MyApp{

  public static void main(String[] args){

    String msg;
    msg = "Hello Wrold again";
    System.out.println(msg);
  }
}
[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Hello Wrold again

データ型

    char a = 'a';
    int x = 10;
    long y = 5555555555L;
    double d = 23423.344;
    float f = 32.33F;
    boolean flag = true; // false
    // \n \t

演算

// 演算
    int i;
    i = 10 / 3;
    i = 10 % 3;
    int x = 5;
    x++;
    x--;
    int x = 5;
    x = x + 12;
    x += 12;
    String s;
    s = "hello " + "world";

型変換(cast)

    double d = 452323.231;
    int i = (int)d;
    int i = 10;
    double d = i / 4

Processing


Processing is a flexible software sketchbook and a language for learning how to code within the context of the visual arts.

プロセッシングはjavaで、主にアーティスト・デザイナー向けです。
processing

size(200, 200);
smooth();
background(255);
rect(50, 50, 80, 30);

%e7%84%a1%e9%a1%8c

角丸

rect(50, 50, 80, 30, 10);

rectMode()

rectMode(CORNER);
rect(50, 50, 30, 30);
rectMode(RADIUS);
rect(50, 50, 30, 40);
fill(255, 0, 0, 127);
rect(50, 50, 30, 40);

枠線

stroke(#ff0000);
strokeWeight(3);
fill(127);
rect(50, 50, 100, 100);

円、線

stroke(#ff0000);
fill(#999999);

line(50, 50, 100, 100);
ellipse(50, 50, 80, 80);
arc(50, 50, 80, 80, 0, radians(180));

矢印

stroke(#ff0000);
fill(#999999);

beginShape();
vertex(100, 20);
vertex(120, 100);
vertex(100, 80);
vertex(80, 100);
endShape(CLOSE);

画像の表示

PImage img;
img = loadImage("a.jpg");
image(img,10, 10);

テキスト描画

PFont f;
f = createFont("Helvetica", 16, true);

textFont(f, 12);
fill(#ff0000);

text("hello world", 100, 100);

座標空間の変更

fill(#ff0000, 127);
noStroke();
rect(10, 10, 50, 50);

fill(#0000ff, 127);
noStroke();
pushMatrix();
translate(10, 10);
rect(10, 10, 50, 50);
popMatrix();

立体

size(200, 200, P3D);
smooth();
background(255);
lights();

pushMatrix();
translate(50, 50, 0);
rotateX(radians(30));
rotateY(radians(40));
rotateZ(radians(10));
box(40);
popMatrix();

setup()

void setup(){
  size(200, 200, P3D);
smooth();
background(255);

 noStroke();
 fill(#ff0000);
 
 frameRate(30);
}

int x = 0;
int y = 0;
void draw(){
  rect(x, y, 100, 100);
  x++;
  y++;
  println(frameCount);
}

マウス情報

int r = 100;
void draw(){
  ellipse(mouseX, mouseY, r, r);
}

ライブラリ
https://processing.org/reference/

ボール遊び

import java.awt.*;
import java.awt.event.*;

class MyFrame extends Frame
	implements Runnable{// t
	int w=600, h=600;
	int sleepTime=10;// t
	Thread thread=null;// t
	Ball[] ball=new Ball[200];
	
	MyFrame(String s){
		super(s);
		setSize(w, h);
		setBackground(Color.GRAY);
		setForeground(Color.WHITE);
		for(int i=0; i < ball.length; i=i+1){
			int x=(int)(Math.random()*(double)w);
			int y=(int)(Math.random()*(double)h);
			int r=(int)(Math.random()*255.0);
			int g=(int)(Math.random()*255.0);
			int b=(int)(Math.random()*255.0);
			int vx=(int)(0.02*(Math.random()-0.5)*(double)w);// t
			int vy=(int)(0.02*(Math.random()-0.5)*(double)h);// t
			ball[i]=new Ball(x, y, 5, new Color(r, g, b));
			ball[i].setVelocity(vx, vy);
			ball[i].setMyFrame(this);
		}
	}
	public void paint(Graphics g){
		for(int i=0; i < ball.length; i=i+1){
			ball[i].paint(g);
		}
	}
	public void start(){// t
		if(thread==null){
			thread=new Thread(this);
			thread.start();
		}
	}
	public void stop(){// t
		thread=null;
	}
	public void run(){// t
		while(Thread.currentThread()==thread){
			for(int i=0; i < ball.length; i=i+1){
				ball[i].move();
			}
			repaint();
			try{
				Thread.sleep(sleepTime);
			}catch(InterruptedException e){
			}
		}
	}
	public boolean checkLimit(int x, int y){// c
		return((x < 0)||(w < x)||(y < 0)||(h < y));
	}
}//end of class MyFrame
class Ball{
	MyFrame myFrame;// c
	int x, y, r;
	int vx, vy;// t
	Color c;
	
	public Ball(int x, int y, int r, Color c){
		this.x=x;
		this.y=y;
		this.r=r;
		this.c=c;
	}
	public void paint(Graphics g){
		Color gc=g.getColor();
		g.setColor(c);
		g.fillOval(x-r, y-r, 2*r, 2*r);
		g.setColor(gc);
	}
	public void setVelocity(int vx, int vy){//t
		this.vx=vx;
		this.vy=vy;
	}
	public void setMyFrame(MyFrame myFrame){// c
		this.myFrame=myFrame;
	}
	public void move(){
		x=x+vx;
		if(myFrame.checkLimit(x,y)){// c
			vx=-vx;
			x=x+vx;
		}
		y=y+vy;
		if(myFrame.checkLimit(x, y)){// c
			vy=-vy;
			y=y+vy;
		}
	}
}
public class ballplay{
	public static void main(String[] args){
		MyFrame myFrame=new MyFrame("BallTest");
		myFrame.addWindowListener(
				new WindowAdapter(){
					public void windowClosing(WindowEvent e){
						System.exit(0);
					}
				});
		myFrame.setVisible(true);
		myFrame.start();// t
	}
}// end of class BallTest

デジタル時計

1000ミリ秒ごとにThreadで再表示させて、まるで、数字が動いているかのようにみせます。run()、start()、stop()の三つのメソッドを記述しています。sleep()メソッドで、clockThreadスレッドを1000ミリ秒間、休止させています。

import java.awt.*;
import java.awt.event.*;
import java.util.Date;

class AnimationClockTestFrame extends Frame
 implements Runnable{
	private Thread clockThread=null;
	
	public AnimationClockTestFrame(){
		setSize(480, 80);
		setForeground(Color.BLUE);
		setFont(new Font("Monospaced", Font.BOLD, 24));
	}
	public void paint(Graphics g){
		Date date=new Date();
		g.drawString(date.toString(), 50, 60);
	}
	public void start(){
		if(clockThread==null){
			clockThread=new Thread(this, "Clock");
			clockThread.start();
		}
	}
	public void stop(){
		clockThread=null;
	}
	public void run(){
		while(Thread.currentThread()==clockThread){
			repaint();
			try{
				Thread.sleep(1000);
			}catch(InterruptedException e){
			}
		}
	}
}

public class watch{
	public static void main(String[] args){
		AnimationClockTestFrame frame=new AnimationClockTestFrame();
		frame.addWindowListener(
				new WindowAdapter(){
					public void windowClosing(WindowEvent e){
						System.exit(0);
					}
				});
		frame.setVisible(true);
		frame.start();
	}
}

AWT

AWTのクラスをメモ

Java.lang.Object
L java.awt.Component
L Button, Checkbox, Label, Choise, List, Scrollbar, Canvas, TextComponent(L TextField, TextArea), Container(L Window(Frame, Dialog), Panel(Applet))

import java.awt.*;

public class Calc1{
	public static void main(String[] args){
		Frame frame=new Frame("frame");
		frame.setLocation(200, 100);
		frame.setSize(300, 200);
		frame.setBackground(Color.BLUE);
		frame.setVisible(true);
	}
}