Javaに投稿されたコード一覧

Java hello world

public static void main(String[] args){
	System.out.println("Hello World");
}

Java 回転鮨

import javax.swing.*;
import java.awt.*;
import java.math.*;
import java.awt.event.*;

class rotSushi {
    public static void main ( String args[] ) {
        MyFrame mf = new MyFrame("rotSushi");
        mf.setVisible(true);
    }
}

class MyFrame extends JFrame implements ActionListener {
    Container contentPane = getContentPane();
    Sushi sushi = new Sushi();
    JPanel p = new JPanel();
    MyFrame ( String title ) {
        setTitle(title);
        setSize(512,512);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p.add(sushi);
        contentPane.add(p);
        Timer tm = new Timer(30,this);
        tm.start();
    }
    public void actionPerformed ( ActionEvent e ) {
        this.repaint();
        sushi.angle = (sushi.angle+1) % 360;
    }
}

class Sushi extends JLabel {
    public static int angle=0;
    Sushi () {
        setText("鮨");
        setForeground(Color.RED);
        setPreferredSize(new Dimension(512,512));
        setFont(new Font("MS 明朝",Font.BOLD,140));
        setHorizontalAlignment(JLabel.CENTER);
    }
    @Override
    public void paintComponent ( Graphics g ) {
        Graphics2D g2 = (Graphics2D) g;
        g2.rotate(Math.toRadians(angle),256,256);
        super.paintComponent(g);
    }
}

Java おなじ変数名を2回使える罠

Class Test1{

	int a;
	String b;

	public Test1(){
		a = 5;
		b = "びー";
	}
//中略

	public void setAB(int a, String b){
		this.a = a;
		this.b = b;
	}

}

Java アホなコマンド受け

//コマンド解釈

public TwitterResponse s(int argment){
	int checking = 0;

	while(argment.length >= checking)
		setTweetAccount("reset");
		setTweetAccount(
			"add",
			TwitterAccount.getUserId( argment.charAt(checking) )
		)
	if(getTweetAccount.length <= 0){
		return TwitterResponse.READY;
	}else{
		throw NonSelectTweetAccountsException;
		return TwitterResponse.WARN;
	}
}

Java kcs/sound へのアクセス

public static void main(String[] args) throws IOException {
	String fileName = "http://(サーバIP)/kcs/sound/kc(shipID)/(fileNum).mp3";
	URL url = new URL(fileName);
	HttpURLConnection conn = (HttpURLConnection) url.openConnection();
	conn.setRequestMethod("GET");
	conn.setInstanceFollowRedirects(false);
	conn.setRequestProperty("Referer","http://(サーバIP)/kcs/mainD2.swf");
	conn.connect();
	int status = conn.getResponseCode();
	// ファイルが存在している場合200が入る
	conn.disconnect();
}

Java R

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class PicSave {

	/*public static void main(String[] args){
	  FreeCapture fc1 = new FreeCapture();
	  fc1.doCapture();
	 }*/

	 public void doCapture(){

		 //参照する変数の宣言
		 Search sc = new Search();
		 //sc.SetVal(1000);

		 System.out.println("引継ぎ完了!"+sc.GetVal());

		  try{

			  //画像のキャプチャ
			   Rectangle bounds = new Rectangle(0, 0, 200, 200);
			   Robot r2d2 = new Robot();
			   BufferedImage img = r2d2.createScreenCapture(new Rectangle(bounds));
			   //キャプチャの保存
			   ImageIO.write(img, "jpg", new File("Main_cap.jpg"));
		  } catch(Exception e){}
	}
}

Java Q

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Search extends PicSave {

	private int x1=0;
	private int y1=0;
	private int w1=0;
	private int h1=0;

	Search(){
		x1 = 0;
	}
	public void SetVal(int p1){
		x1 = p1;
	}

	JFrame frame;
	JPanel container;
	CanvasPane canvas;

	Point pt_from, pt_to;
	static final Point nullpoint = new Point(-1, -1);

	Search (String title) {

		/*
		 * ウィンドウ周辺のGUI初期設定
		 */
		frame = new JFrame(title);
		frame.setSize(300, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		container = (JPanel) frame.getContentPane();

		/*
		 * 描画の条件判定に使うオブジェクト
		 */
		pt_from = new Point(nullpoint);
		pt_to = new Point(nullpoint);

		canvas = new CanvasPane();
		MouseAdapter madp = new SelectiongMouseListener();
		canvas.addMouseListener(madp);
		canvas.addMouseMotionListener(madp);
		container.add(canvas);

		frame.setVisible(true);

		/*
		 * 50ミリ秒間隔でアニメーションを描かせるスレッド
		 */
		Timer timer = new Timer();
		timer.scheduleAtFixedRate(new FrameDrawer(), 0, 50);
	}

	class CanvasPane extends JPanel {

		BufferedImage image;
		TexturePaint tp;

		Color pale = new Color(0, 0, 255, 110);
		Color transparent = new Color(0, 0, 0, 0);

		CanvasPane () {
			super();
			this.setBackground(Color.white);
			this.setOpaque(true);

			/*
			 * 描画キャンバスとなるパネルの生成段階(コンストラクタ)で、
			 *  以後繰返し利用するテクスチャ・オブジェクトを用意する
			 */
			int len = 50;

			image = new BufferedImage(len, len, BufferedImage.TYPE_INT_ARGB);
			Graphics2D gbi = image.createGraphics();
			gbi.setColor(transparent);
			gbi.fillRect(0, 0, len, len );

			gbi.setColor(pale);
			int unit = len / 2;
			for (int i = 0; i < 2; i++) {
				gbi.fillRect(unit * i, unit * i, unit, unit);
			}

			Rectangle rect = new Rectangle(0, 0, len, len );
			tp = new TexturePaint(image, rect);

		}

		public void paintComponent (Graphics g) {
			super.paintComponent(g);	//バックグラウンド色による初期化なども含まれる
			Graphics2D g2 = (Graphics2D)g;
			//画像の取得
			 BufferedImage readImage = null;
			    try {
			      readImage = ImageIO.read(new File("10.jpg"));
			    } catch (Exception e) {
			      e.printStackTrace();
			      readImage = null;
			    }

			    if (readImage != null){
			      g2.drawImage(readImage, 0, 0, this);
			    }

			if (!pt_from.equals(nullpoint) && !pt_to.equals(nullpoint) ) {
				/*
				 * 始点・終点双方の条件をチェックするのは、mousePressedだけされて
				 *  mouseMovedが生じていない場合に描画を見送るため
				 */

				int x = Math.min(pt_from.x, pt_to.x);
				int y = Math.min(pt_from.y, pt_to.y);
				int w = Math.abs(pt_to.x - pt_from.x);
				int h = Math.abs(pt_to.y - pt_from.y);

				//g2.setPaint(tp);
				//g2.fillRect(x, y, w, h);	//テクスチャを先に描画

				g2.setColor(Color.red);
				for (int i = 0; i < 2; i++) {	//青色で線幅3ピクセルの枠を描く
					g2.drawRect(x + i, y + i, w - 2 * i, h - 2 * i);
					//xの始点とyの始点、画像の大きさの取得
					x1=x+i;
					y1=y+i;
					w1=w-2*i;
					h1=h-2*i;
					System.out.println("1は"+x1);
					return x1;
				}
			}
		}
	}
	public int GetVal(){
		System.out.println("GetValは"+x1);
		return x1;
	}

	class SelectiongMouseListener extends MouseAdapter {

		@Override
		public void mousePressed (MouseEvent ev) {
			pt_from.setLocation(ev.getPoint() );
		}

		@Override
		public void mouseReleased (MouseEvent ev) {
			 System.out.println("xの始点は="+x1+"yの始点は="+y1+"画像の横は="+ w1+"画像の縦は="+h1);
			//pt_from.setLocation(nullpoint);
			//pt_to.setLocation(nullpoint);
			PicSave save;
			save = new PicSave();
            save.doCapture();
		}

		@Override
		public void mouseDragged (MouseEvent ev) {
			pt_to.setLocation(ev.getPoint() );
		}

	}

	class FrameDrawer extends TimerTask {	//50ミリ秒おきに描画

		@Override
		public void run() {
			canvas.repaint();
		}

	}

	public static void main(String[] args) {
		new Search("Search");
	}

}

Java Test3

class Hello {
	public static void main(String[] args) {
		Test t = new Test();
		t.SetVal(1000);
		System.out.println(t.GetVal());
	}
}

class Test {
	private int val;
	Test(){
		val = 0;
	}
	public void SetVal(int p1){
		val = p1;
	}
	public int GetVal(){
		return val;
	}
}

Java Test2

class Hello {
	public static void main(String[] args) {
		Test t = new Test();
		t.val = 1000;
		System.out.println(t.val);
	}
}

class Test {
	public int val;
}

Java Test

class Hello {
	public static void main(String[] args) {
		Test t = new Test();
		t.SetVal(1000);
		System.out.println(t.GetVal());
	}
}

class Test {
	private int val;
	public void SetVal(int p1){
		val = p1;
	}
	public int GetVal(){
		return val;
	}
}
Total Pages: 1 / 111234510...最後 »

よく投稿されているコード

タグ

最近投稿されたコード