how to code something このページをアンテナに追加 RSSフィード

2016-08-15

JavaでMax Heap Sizeを指定する方法

.bashrcなどに

export _JAVA_OPTIONS="-Xmx1g" # or -Xmx512m, -Xmx256m and so on

を記述しておけば、Java起動時に自動で設定が読み込まれる

$ java -jar jconsole.jar &
Picked up _JAVA_OPTIONS: -Xmx1g

2012-07-06

JARを解凍→実行可能JARの作成

Jarファイルの拡張子を.zipに変更
ダブルクリックして解凍
→いろいろと変更

マニフェストファイルを指定してjarファイル生成

@hoge $ jar cvfm Foo.jar META-INF/MANIFEST.MF *

jarの実行

java -jar Foo.jar

2012-01-11

Eclipseのエラー(NoClassDefFoundError)

久しぶりにEclipseを使ったらHello worldできなかったので。
android has no sourceとかいうエラーもでてる。

f:id:seinzumtode:20120111135236p:image

http://stackoverflow.com/questions/5753021/how-to-attach-source-to-android-jar
プラグインをアップデートすれば治るとのこと。

2011-11-13

SwingからProcessingを呼び出す

以前のエントリでも扱った気がするが、前回とは違う。
やりたいのは、Swingアプリのボタンをクリック→Processingアプリがアプレット上で発動(JFrameとAppletが共存?)
アプレットのみを動かしたいわけではなく、あくまでもSwingアプリケーション上でProcessingを動かしたい。
ビジュアライジングデータに載ってる普通の方法だと、PAppletを継承させるので結局アプレットで動いちゃう。
同じ質問してる人を発見。
http://oshiete.goo.ne.jp/qa/5889872.html
どうやらできるらしい。
http://wiki.processing.org/w/Swing_JSliders
まだ詳しくコード読んでないけど、これはかなり期待できそう。
#このmain関数の書き方が気になるんだよなあ

  public static void main(String[] args) {
    // threadsafe way to create a Swing GUI
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createGUI();
      }
    }
    );


KiteController.java

package test;

/*
swing sliders taken from http://wiki.processing.org/index.php?title=Swing_JSliders_with_Processing
@author Ira Greenberg
 
Put ''Kite.java'' and KiteController.java in their own 
files and follow instructions outlined above.
*/
 
/* ******************************
      KiteController class
****************************** */

//import com.iragreenberg.Kite;
import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;
 
public class KiteController extends JPanel {
 
  private int w = 600, h = 422;
  //private int w = 1000, h = 800; 変えてみようとしたけど駄目だった
	private Kite kite;
 
  // constructor
  public KiteController() {
    setBackground(new Color(180, 180, 220));
 
    /* create 2 pane across layout
     left pane holds processing PApplet
     right pane holds user widgets */
    setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
 
    // Instantiate Kite object
    kite = new Kite(400, 400);
 
    // right tools pane
    JPanel rtPanel = new JPanel();
    rtPanel.setOpaque(false);
    rtPanel.setLayout(new BoxLayout(rtPanel, BoxLayout.PAGE_AXIS));
 
    //無理矢理サイズを変えてやる(Added by me)
    rtPanel.setPreferredSize(new Dimension(400, 800));
    
    JLabel jl = new JLabel("   Drag the Kite String");
 
    // kite radius slider
    JSlider js0 = new JSlider(JSlider.HORIZONTAL, 10, 100, 40);
    js0.setBorder(BorderFactory.createTitledBorder("Kite Radius"));
    js0.setMajorTickSpacing(30);
    js0.setMinorTickSpacing(4);
    js0.setPaintTicks(true);
    js0.setPaintLabels(true);
 
    // handle js0 change events
    js0.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent evt) {
        JSlider slider = (JSlider)evt.getSource();
 
        if (!slider.getValueIsAdjusting()) {
          kite.setRadius(slider.getValue());
        }
      }
    }
    );
 
    // kite sides slider
    JSlider js1 = new JSlider(JSlider.HORIZONTAL, 3, 63, 4);
    js1.setBorder(BorderFactory.createTitledBorder("Kite Sides"));
    js1.setMajorTickSpacing(12);
    js1.setMinorTickSpacing(1);
    js1.setPaintTicks(true);
    js1.setPaintLabels(true);
 
    // handle js1 change events
    js1.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent evt) {
        JSlider slider = (JSlider)evt.getSource();
 
        if (!slider.getValueIsAdjusting()) {
          kite.setFeetCount(slider.getValue());
        }
      }
    }
    );
 
    // spring speed slider
    JSlider js2 = new JSlider(JSlider.HORIZONTAL, 1, 10, 5);
    js2.setBorder(BorderFactory.createTitledBorder("Spring Speed"));
    js2.setMajorTickSpacing(3);
    js2.setMinorTickSpacing(1);
    js2.setPaintTicks(true);
    js2.setPaintLabels(true);
 
    // handle js2 change events
    js2.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent evt) {
        JSlider slider = (JSlider)evt.getSource();
 
        if (!slider.getValueIsAdjusting()) {
          kite.setSpringSpeed(slider.getValue()/20.0f);
        }
      }
    }
    );
 
    // spring damping slider
    JSlider js3 = new JSlider(JSlider.HORIZONTAL, 1, 10, 4);
    js3.setBorder(BorderFactory.createTitledBorder("Spring Damping"));
    js3.setMajorTickSpacing(1);
    js3.setMinorTickSpacing(1);
    js3.setPaintTicks(true);
    js3.setPaintLabels(true);
 
    // handle js3 change events
    js3.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent evt) {
        JSlider slider = (JSlider)evt.getSource();
 
        if (!slider.getValueIsAdjusting()) {
          kite.setSpringDamping(1.0f - slider.getValue()/30.0f);
        }
      }
    }
    );
 
    // assemble tools pane
    rtPanel.add(jl);
    rtPanel.add(Box.createRigidArea(new Dimension(0,10)));
    rtPanel.add(js0);
    rtPanel.add(Box.createRigidArea(new Dimension(0,8)));
    rtPanel.add(js1);
    rtPanel.add(Box.createRigidArea(new Dimension(0,8)));
    rtPanel.add(js2);
    rtPanel.add(Box.createRigidArea(new Dimension(0,8)));
    rtPanel.add(js3);
 
 
    // Next comment taken directly from PApplet class: 
    /* "...ensures that the animation thread is started 
     and that other internal variables are properly set."*/
    kite.init(); 
    //無理矢理大きさを変更(Added by me)
    kite.setPreferredSize(new Dimension(400,800));
 
    // add panes to larger JPanel
    add(kite);
    // create some space between panes
    add(Box.createRigidArea(new Dimension(4,0)));
    add(rtPanel);
    // add a little margin on right of panel
    add(Box.createRigidArea(new Dimension(4,0)));
  }
 
  // create external JFrame
  private static void createGUI() {
    // create new JFrame
    JFrame jf = new JFrame("Kite Swing Application");
 
    // this allows program to exit
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
    // You add things to the contentPane in a JFrame
    jf.getContentPane().add(new KiteController());
 
    // keep window from being resized
    jf.setResizable(false);
 
    // size frame
    jf.pack();
 
    // make frame visible
    jf.setVisible(true);
  }
 
  public static void main(String[] args) {
    // threadsafe way to create a Swing GUI
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createGUI();
      }
    }
    );
  }
}


Kite.java

package test;

	/*******************************
    Kite class
*******************************/
/* By extending PApplet you can use your sweet old P5
calls in a regular Java App. Pretty nice indeed! */


import processing.core.*;
import java.awt.*;

public class Kite extends PApplet {
// instance properties
private Point2D dummy;
private Vect2D dummyVel;
private int feetCount = 4;
private Point2D[] feet = new Point2D[feetCount];
private Vect2D[] feetDelta = new Vect2D[feetCount];
private Vect2D[] feetVel = new Vect2D[feetCount];
private Vect2D[] feetAccel = new Vect2D[feetCount];
private float botRadius = 45.0f;
private float[] springSpeed = new float[feetCount];
private float springSpdMin = .05f, springSpdMax = .1f;
private float damping = .9f;
private Point2D lead;
private Vect2D leadVel;
private int w, h;
private int stringBaseX;
private int stringBaseY;
private float theta = 0;

// constructor
public Kite(int w, int h){
this.w = w;
this.h = h;

}

// Yes, this is the P5 setup()
public void setup(){
/* account for height of frame title bar,
when sizing applet. getBounds() returns a Java
Rectangle object that has 4 public properties:
(x, y, width and height) */
int titleBarHt = getBounds().y;
size(w, h-titleBarHt);
smooth();
frameRate(30);

dummy = new Point2D(width/2, height/4);
dummyVel = new Vect2D(3.65f, 2.0f);
lead = new Point2D(width/2, height/2);
leadVel = new Vect2D(.04f, .05f);

stringBaseX = width/2;
stringBaseY = height;

// set legs with some trig
for (int i=0; i<feet.length; i++){
 feet[i] = new Point2D(dummy.x + cos(theta)*botRadius, 
        dummy.y + sin(theta)*botRadius);
 feetDelta[i] = new Vect2D(feet[i].x-dummy.x, feet[i].y-dummy.y);
 feetVel[i] = new Vect2D(0, 0);
 feetAccel[i] = new Vect2D(0, 0);
 theta += TWO_PI/feet.length;
 springSpeed[i] = random(springSpdMin, springSpdMax);
}
}

// A huh, this is the P5 draw()
public void draw(){
background(255);
drawBot();
moveBot();
}

public void drawBot(){
rectMode(CENTER);
fill(0);
for (int i=0; i<feet.length; i++){
 line(dummy.x, dummy.y, feet[i].x, feet[i].y);
 if (i<feet.length-1){
   line(feet[i].x, feet[i].y, feet[i+1].x, feet[i+1].y);
 }
 else {
   line(feet[i].x, feet[i].y, feet[0].x, feet[0].y);
 }
}
line(stringBaseX, stringBaseY, dummy.x, dummy.y);

if (mousePressed){
 stringBaseX = mouseX;
 stringBaseY = mouseY;
}
fill(255);
}

public void moveBot(){
Vect2D[] feetMotionDelta = new Vect2D[feetCount];

for (int i=0; i<feet.length; i++){
 feetMotionDelta[i] = new Vect2D((dummy.x-feet[i].x+ 
        feetDelta[i].vx)*springSpeed[i],
 (dummy.y-feet[i].y+feetDelta[i].vy)*springSpeed[i]);

 feetAccel[i].vx+= feetMotionDelta[i].vx;
 feetAccel[i].vy+= feetMotionDelta[i].vy;

 feetVel[i].vx += feetAccel[i].vx;
 feetVel[i].vy += feetAccel[i].vy;

 feet[i].x = feetVel[i].vx ;
 feet[i].y = feetVel[i].vy ;

 feetAccel[i].vx *= damping;
 feetAccel[i].vy *= damping;

 float rotAng = atan2(mouseY-(height+100), mouseX-width/2);
 if (mousePressed){
   dummy.x = mouseX + cos(rotAng)*200;
   dummy.y = mouseY + sin(rotAng)*200;
 }
}
}

// dynamically change kite parameters
public void setFeetCount(int val){
// reinitialize arrays
feetCount = val;
feet = new Point2D[feetCount];
feetDelta = new Vect2D[feetCount];
feetVel = new Vect2D[feetCount];
feetAccel = new Vect2D[feetCount];
springSpeed = new float[feetCount];

// reset values
float theta = 0;
for (int i=0; i<feet.length; i++){
 feet[i] = new Point2D(dummy.x + cos(theta)*botRadius, 
        dummy.y + sin(theta)*botRadius);
 feetDelta[i] = new Vect2D(feet[i].x-dummy.x, feet[i].y-dummy.y);
 feetVel[i] = new Vect2D(0, 0);
 feetAccel[i] = new Vect2D(0, 0);
 theta += TWO_PI/feet.length;
 springSpeed[i] = random(springSpdMin, springSpdMax);
}
}

public void setRadius(int val){
botRadius = val;
for (int i=0; i<feet.length; i++){
 feet[i].x = dummy.x + cos(theta)*botRadius;
 feet[i].y = dummy.y + sin(theta)*botRadius;
 feetDelta[i].vx = feet[i].x-dummy.x;
 feetDelta[i].vy = feet[i].y-dummy.y;
 theta += TWO_PI/feet.length;
}
}

public void setSpringSpeed(float val){
for (int i=0; i<feet.length; i++){
 springSpeed[i] = random(springSpdMin, val);
}
}

public void setSpringDamping(float val){
damping = val;
}

/* two simple inner classes to make
things a little easier */
class Point2D{
float x, y;

Point2D(float x, float y){
 this.x = x;
 this.y = y;
}
}

class Vect2D{
float vx, vy;

Vect2D(float vx, float vy){
 this.vx = vx;
 this.vy = vy;
}
}
}


動作テスト


#Scalaに興味がでてきた。。。

2011-11-12

SwingでJDBCを扱う

やっとこさ動いた。

(実行環境)
データベース:MySQL 5.1
データベースコネクタ:JDBC Connector/J

まず、データベースを用意する。

cd /usr/local/mysql/bin 
./mysqladmin -u root -p passwd create hellodb

で、hellodbというデータベースを作っておく。(なぜか.bash_profileで指定したPATHを読まないのでmysqladminコマンドを直に実行。これもMAMPの設定が関係してる?)
次に、employeeテーブルを作るが、操作に慣れる意味もあり、今回はeclipseからSQLを実行した。
変数の型は適当。VARCHAR(64)の指定は、ただ文字列にしたかっただけ。

create table employee (
	FName  VARCHAR(64),
	LName VARCHAR(64),
	Address VARCHAR(64),
	Salary VARCHAR(64)
	);

次に、
http://shivasoft.in/blog/java/jdbc-example-with-microsoft-access-in-swing/
を参考にして、以下を書き換えた。

 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 Connection con = DriverManager.getConnection("jdbc:odbc:ShivaEvening");

を、

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection( "jdbc:mysql://localhost/hellodb", "root", "********");

伏せ字はルートのパスワード

ソースコード JDBCSwing.java

package test;

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class JDBCSwing implements ActionListener {

	JLabel lblFName,lblLname,lblAddress,lblSalary,lblF,lblL,lblA,lblS,
	lblFVal,lblLVal,lblAVal,lblSVal;
	JTextField txtFName,txtLName,txtAddress,txtSalary;
	JButton btnAdd,btnUpdate,btnDelete,btnPrev,btnNext;
	ResultSet rs;
	public static void main(String[] args) {
		JDBCSwing obj = new JDBCSwing();
		obj.createUI();
	}
	private void createUI()
	{
		JFrame frame = new JFrame("JDBC All in One");

		JPanel pnlInput = new JPanel(new GridLayout(4,2));

		lblFName = new JLabel("  First Name : ");
		txtFName = new JTextField(15);

		lblLname = new JLabel("  Last Name : ");
		txtLName = new JTextField();

		lblAddress = new JLabel("  Address : ");
		txtAddress = new JTextField();

		lblSalary = new JLabel("  Salary : ");
		txtSalary = new JTextField();

		pnlInput.add(lblFName);
		pnlInput.add(txtFName);

		pnlInput.add(lblLname);
		pnlInput.add(txtLName);

		pnlInput.add(lblAddress);
		pnlInput.add(txtAddress);

		pnlInput.add(lblSalary);
		pnlInput.add(txtSalary);

		JPanel pnlButton = new JPanel(new GridLayout(1,3));

		btnAdd = new JButton("Add");
		btnAdd.addActionListener(this);

		btnUpdate = new JButton("Update");
		btnUpdate.addActionListener(this);

		btnDelete = new JButton("Delete");
		btnDelete.addActionListener(this);

		pnlButton.add(btnAdd);
		pnlButton.add(btnUpdate);
		pnlButton.add(btnDelete);

		JPanel pnlNavigate = new JPanel(new GridLayout(1,2));
		btnPrev = new JButton(" << ");
		btnPrev.setActionCommand("Prev");
		btnPrev.addActionListener(this);

		btnNext = new JButton(" >> ");
		btnNext.setActionCommand("Next");
		btnNext.addActionListener(this);

		pnlNavigate.add(btnPrev);
		pnlNavigate.add(btnNext);

		JPanel pnlNavAns = new JPanel(new GridLayout(4,2));

		lblF = new JLabel("  First Name : ");
		lblFVal = new JLabel("Val");

		lblL = new JLabel("  Last Name : ");
		lblLVal = new JLabel("Val");

		lblA = new JLabel("  Address : ");
		lblAVal = new JLabel("Val");

		lblS = new JLabel("  Salary : ");
		lblSVal = new JLabel("Val");

		pnlNavAns.add(lblF);
		pnlNavAns.add(lblFVal);

		pnlNavAns.add(lblL);
		pnlNavAns.add(lblLVal);

		pnlNavAns.add(lblA);
		pnlNavAns.add(lblAVal);

		pnlNavAns.add(lblS);
		pnlNavAns.add(lblSVal);

		Container cn = frame.getContentPane();
		cn.setLayout(new BoxLayout(cn,BoxLayout.Y_AXIS));

		frame.add(pnlInput);
		frame.add(pnlButton);
		frame.add(pnlNavAns);
		frame.add(pnlNavigate);

		//If this will not be written, the only frame will be closed
		// but the application will be active.
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent evt) {

		String action = evt.getActionCommand();
		if(action.equals("Add"))
		{
			addOperation();
		}else if(action.equals("Update"))
		{
			updateOperation();
		}else if(action.equals("Delete"))
		{
			deleteOperation();
		}else if(action.equals("Prev"))
		{
			preNavigation();
		}else if(action.equals("Next"))
		{
			nextNavigation();
		}
	}
	private void addOperation()
	{
		try
		{
			//Load Jdbc Odbc Driver
			//Class.forName("com.mysql.jdbc.Driver");
			//Connection con = DriverManager.getConnection( "jdbc:mysql://localhost/hellodb", "root", "********");

			//Load Jdbc Driver
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection(
						    "jdbc:mysql://localhost/hellodb", "root", "********");
			
			String sql = "INSERT INTO Employee (FName,LName,Address,Salary) " +
					"Values ('"+txtFName.getText()+"'," +
							"'"+txtLName.getText()+"'," +
							"'"+txtAddress.getText()+"'," +
							"'"+txtSalary.getText()+"')";

			Statement st = con.createStatement();
			st.execute(sql);

			JOptionPane.showMessageDialog(null, "Record Added Succesfully.","Record Added",
                        JOptionPane.INFORMATION_MESSAGE);
			clearControls();
		}catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
                        JOptionPane.ERROR_MESSAGE);
		}
	}
	private void updateOperation()
	{
		try
		{
			//Load Jdbc Odbc Driver
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection( "jdbc:mysql://localhost/hellodb", "root", "********");

			String sql = "Update Employee " +
					        "SET LName = '"+txtLName.getText()+"'," +
							"Address = '"+txtAddress.getText()+"'," +
							"Salary = '"+txtSalary.getText()+"'" +
							"Where FName = '"+txtFName.getText()+"'";

			JOptionPane.showMessageDialog(null, sql,"Record Updated",
                        JOptionPane.INFORMATION_MESSAGE);
			Statement st = con.createStatement();
			st.execute(sql);

			JOptionPane.showMessageDialog(null, "Record Update Succesfully.",
                        "Record Updated",JOptionPane.INFORMATION_MESSAGE);
			clearControls();
		}catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
                        JOptionPane.ERROR_MESSAGE);
		}
	}
	private void deleteOperation()
	{
		int ans = JOptionPane.showConfirmDialog(null,
				"Are you sure to delete the Record ?", "Delete Record",
                           JOptionPane.YES_NO_OPTION);
		if(ans == JOptionPane.YES_OPTION)
		{
			try{
			//Load Jdbc Odbc Driver
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection( "jdbc:mysql://localhost/hellodb", "root", "********");

			String sql = "Delete FROM Employee where FName = '"+txtFName.getText()+"'";

			Statement st = con.createStatement();
			st.execute(sql);
			}catch(Exception e)
			{
				JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
                                JOptionPane.ERROR_MESSAGE);
			}
			JOptionPane.showMessageDialog(null, "Record Deleted","Success",
                        JOptionPane.INFORMATION_MESSAGE);
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Operation Canceled","Cancel",
                        JOptionPane.INFORMATION_MESSAGE);
		}
	}
	private void preNavigation()
	{
		try{
			if(rs == null)
			{
			//Load Jdbc Odbc Driver
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection( "jdbc:mysql://localhost/hellodb", "root", "********");

			String sql = "SELECT * FROM Employee";

			Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                        ResultSet.CONCUR_UPDATABLE);
			rs = st.executeQuery(sql);
			}
			if(rs.previous())
			{
				populateValue();
			}
			}catch(Exception e)
			{
				JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
                                JOptionPane.ERROR_MESSAGE);
			}
	}
	private void nextNavigation()
	{
		try{
			if(rs == null)
			{
			//Load Jdbc Odbc Driver
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection( "jdbc:mysql://localhost/hellodb", "root", "********");

			String sql = "SELECT * FROM Employee";

			Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                        ResultSet.CONCUR_UPDATABLE);
			rs = st.executeQuery(sql);
			}
			if(rs.next())
			{
				populateValue();
			}
			}catch(Exception e)
			{
				JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
                                JOptionPane.ERROR_MESSAGE);
			}
	}
	private void populateValue() throws Exception
	{
		String fName = rs.getString("FName");
		String lName = rs.getString("LName");
		String add = rs.getString("Address");
		String sal = rs.getString("Salary");

		lblFVal.setText(fName);
		lblLVal.setText(lName);
		lblAVal.setText(add);
		lblSVal.setText(sal);

		txtFName.setText(fName);
		txtLName.setText(lName);
		txtAddress.setText(add);
		txtSalary.setText(sal);
	}
	private void clearControls()
	{
		txtFName.setText("");
		txtLName.setText("");
		txtAddress.setText("");
		txtSalary.setText("");
	}
}


動作テスト