|
<Number>: [00000984]
<Date>: 2016/01/02 20:57:53
<Title>:
<Name>: amanojaku@管理人
|
|
|
《参考》
Java ジェネリクスのポイント - Qiita
http://qiita.com/pebblip/items/1206f866980f2ff91e77
Javaジェネリクス再入門 - プログラマーの脳みそ
http://d.hatena.ne.jp/Nagise/20101105/1288938415
ジェネリック : 総称 | Java プログラミング解説 - so-zou.jp
http://so-zou.jp/software/tech/programming/java/generic/
Javaのジェネリック(総称型)の使い方まとめ -- ぺけみさお
http://www.xmisao.com/2013/12/08/java-generics-summary.html
「Java SE5.0」から Generics なる機能が導入されていたようですが、(かなり以前に java でプログラミングしただけなので)最近になって Generics なるモノを知りましたが、良く分からんので、実際にコードを書いてみる事に(下記参照)。
なるほど、ワイルドカードは Element(要素) ではなく、Container(容器)に対して擬似的な親子関係をサポートすると言う事なのか。
ここでは「Box」がContainer(容器)、仮型パラメータ「T」で宣言された変数がElement(要素)に対応する。
class Box<T> {
T objT = null;
// public Box<? super T> getSuper(){ }
// ↑返り値には「super」を指定したワイルドカードは使うべきではないようです。
public T getT(){
return objT;
}
public Box<T> getBox(){
return this; // あくまでテスト用なので適当に処理。
}
public void setT(T o){
objT = o;
}
public void setBox(Box<?extends T> box){
objT = box.getT();
}
}
Box<Number> boxN = new Box<Number>( );
Box<Integer> boxI = new Box<Integer>( );
boxI.setT(new Integer(1)); // 当然、正常にコンパイルされる。
boxI.setT(boxI.getT()); // 当然、正常にコンパイルされる。
boxI.setBox(boxI); // 当然、正常にコンパイルされる。
boxI.setBox(boxI.getBox()); // 当然、正常にコンパイルされる。
boxN.setT((Number)new Integer(1)); // 当然、正常にコンパイルされる。
boxN.setT(new Integer(1)); // 当然、正常にコンパイルされる。
boxN.setT(boxN.getT()); // 当然、正常にコンパイルされる。
boxN.setT(boxI.getT()); // 当然、正常にコンパイルされる。
boxN.setBox(boxN); // 当然、正常にコンパイルされる。
boxN.setBox(boxI); // Container(容器)に対して擬似的な親子関係がサポートされ、正常にコンパイルされる。
boxN.setBox(boxN.getBox()); // 当然、正常にコンパイルされる。
boxN.setBox(boxI.getBox()); // Container(容器)に対して擬似的な親子関係がサポートされ、正常にコンパイルされる。
|
|
|
<Number>: [00000986]
<Date>: 2016/01/02 23:59:59
<Title>:
<Name>: amanojaku@管理人
|
|
|
Generics を使って基本的な Node クラスを考えてみると、下記のようになる。
class Node<T> {
Node oCoupler;
T oHook;
public void SetNext(Node oN){
oCoupler = oN;
}
public Node GetNext( ){
return oCoupler;
}
public void SetHook(T oH){
oHook = oH;
}
public T GetHook( ){
return oHook;
}
}
|
|
|
<Number>: [00000991]
<Date>: 2016/01/10 08:33:13
<Title>:
<Name>: amanojaku@管理人
|
|
|
Sin グラフを横スクロールさせる、ComboBox で「波長の数、カラー」を指定して、[Make]ボタンで新しい Sin グラフを追加できます。
「MainAppFrameObj」 からアプレット独自メソッドにアクセスする場合を想定すると、「MainAppFrameObj」はアプレット・クラス内に設置した方が良いだろう。
(アプリーケーションの初期実行用) main( ) メソッドで(MainAppFrameObjなどのような)クラス内クラスをインスタンス化したい場合(main( ) メソッド実行時には、まだアプレット自体のインスタンスが生成されておらず、通常のクラス内クラスではインスタンス化できないので) 、クラス内クラスには「static」修飾子を付与しなければならない(この場合、オブジェクトは1つだけしか作成できない)。
クリック時にマウス・カーソルが動いてしまった場合 mouseClicked イベントだとイベントが発生しない"仕様"になっているようなので、mouse イベントに関しては mouseReleased イベントを使用している。
『Appletcation.java』
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
//Event を使う場合は、その Event に対応する「〜Adapter、〜Event」を import しなければならない。
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Appletcation extends JApplet{
static Appletcation oMainApp;
static boolean lApplication = false; // true;
static MainAppFrameObj oMainAppFrame;
int iLayoutX, iLayoutY;
CanvasObj oCanvas;
int iCanvasWidth,iCanvasHeight;
JComboBox ccbSinGraph_WaveQnt;
int iCbSinGraph_WaveQnt_Width,iCbSinGraph_WaveQnt_Height;
JComboBox ccbSinGraph_Color;
int iCbSinGraph_Color_Width,iCbSinGraph_Color_Height;
Color[] d1oCbSinGraph_ColorSets;
JButton cbtSinGraph_Make;
int iBtSinGraph_Make_Width, iBtSinGraph_Make_Height;
public static void main(String[] args) {
oMainApp = new Appletcation( );
lApplication = true; // false;
oMainAppFrame =
new MainAppFrameObj( );
oMainAppFrame.oAppThread.start();
try{
oMainAppFrame.oAppThread.join();
}
catch(InterruptedException e){ }
System.exit(0);
}
public void init() {
System.out.println("init( );");
oMainApp = this;
getContentPane().setLayout(null); // Layout 座標指定モード
ccbSinGraph_WaveQnt = new JComboBox( );
ccbSinGraph_WaveQnt.setModel(new DefaultComboBoxModel(new Object[] { " 1 ", " 2 ", " 3 " }));
ccbSinGraph_WaveQnt.setDoubleBuffered(false);
ccbSinGraph_WaveQnt.setBorder(null);
System.out.println(
"ccbSinGraph_WaveQnt.getWidth()="+Integer.toString(ccbSinGraph_WaveQnt.getWidth())+"; "+
"ccbSinGraph_WaveQnt.getHeight()="+Integer.toString(ccbSinGraph_WaveQnt.getHeight())+"; "+
"ccbSinGraph_WaveQnt.getMinimumSize().width="+Integer.toString(ccbSinGraph_WaveQnt.getMinimumSize().width)+"; "+
"ccbSinGraph_WaveQnt.getMinimumSize().height="+Integer.toString(ccbSinGraph_WaveQnt.getMinimumSize().height)+"; "+
"");
iCbSinGraph_WaveQnt_Width = ccbSinGraph_WaveQnt.getMinimumSize().width;
iCbSinGraph_WaveQnt_Height = ccbSinGraph_WaveQnt.getMinimumSize().height;
d1oCbSinGraph_ColorSets = new Color[]{
Color.GREEN, Color.PINK,
Color.RED, Color.YELLOW,
};
ccbSinGraph_Color = new JComboBox( );
ccbSinGraph_Color.setModel(new DefaultComboBoxModel(new Object[]
{ "グリーン", "ピンク", "赤", "黄色", }));
ccbSinGraph_Color.setDoubleBuffered(false);
ccbSinGraph_Color.setBorder(null);
iCbSinGraph_Color_Width = ccbSinGraph_Color.getMinimumSize().width;
iCbSinGraph_Color_Height = ccbSinGraph_Color.getMinimumSize().height;
cbtSinGraph_Make = new JButton( );
cbtSinGraph_Make.setText("Make");
System.out.println(
"cbtSinGraph_Make.getWidth()="+Integer.toString(cbtSinGraph_Make.getWidth())+"; "+
"cbtSinGraph_Make.getHeight()="+Integer.toString(cbtSinGraph_Make.getHeight())+"; "+
"cbtSinGraph_Make.getMinimumSize().width="+Integer.toString(cbtSinGraph_Make.getMinimumSize().width)+"; "+
"cbtSinGraph_Make.getMinimumSize().height="+Integer.toString(cbtSinGraph_Make.getMinimumSize().height)+"; "+
"");
iBtSinGraph_Make_Width = cbtSinGraph_Make.getMinimumSize().width;
iBtSinGraph_Make_Height = cbtSinGraph_Make.getMinimumSize().height;
cbtSinGraph_Make.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
if(null!=oCanvas){
oCanvas.SinGraph_Make( );
}
}
});
iCanvasWidth = 500;
iCanvasHeight = 300;
iLayoutX = 0; iLayoutY = 0;
getContentPane().add(ccbSinGraph_WaveQnt);
ccbSinGraph_WaveQnt.setBounds(iLayoutX,iLayoutY, iCbSinGraph_WaveQnt_Width, iCbSinGraph_WaveQnt_Height);
iLayoutX = iLayoutX+iCbSinGraph_WaveQnt_Width; // iLayoutY = 0;
getContentPane().add(ccbSinGraph_Color);
ccbSinGraph_Color.setBounds(iLayoutX,iLayoutY, iCbSinGraph_Color_Width, iCbSinGraph_Color_Height);
iLayoutX = iLayoutX+iCbSinGraph_Color_Width; // iLayoutY = 0;
getContentPane().add(cbtSinGraph_Make);
cbtSinGraph_Make.setBounds(iLayoutX,iLayoutY, iBtSinGraph_Make_Width, iBtSinGraph_Make_Height);
iLayoutX = 0; iLayoutY = iLayoutY+iBtSinGraph_Make_Height;
oCanvas = new CanvasObj(iLayoutX, iLayoutY, iCanvasWidth, iCanvasHeight);
getContentPane().add(oCanvas);
// oCanvas.setVisible(true);
iLayoutX = iCanvasWidth; iLayoutY = iLayoutY+iCanvasHeight;
// this.setSize(iLayoutX, iLayoutY);
System.out.println(
"this.getWidth()="+Integer.toString(this.getWidth())+"; "+
"this.getHeight()="+Integer.toString(this.getHeight())+"; "+
"this.getMinimumSize().width="+Integer.toString(this.getMinimumSize().width)+"; "+
"this.getMinimumSize().height="+Integer.toString(this.getMinimumSize().height)+"; "+
"");
if( lApplication ){
System.out.println("if(lApplication)");
oMainAppFrame.setPreSize(new Dimension(iLayoutX, iLayoutY));
System.out.println("PanelWidth="+oCanvas.getSize( ).getWidth());
System.out.println("PanelHeight="+oCanvas.getSize( ).getHeight());
}
System.out.println("AppletWidth="+getSize( ).getWidth());
System.out.println("AppletHeight="+getSize( ).getHeight());
}
public void start(){
System.out.println("start( );");
repaint();
}
public void stop(){
System.out.println("stop( );");
}
public void destroy(){
System.out.println("destroy( );");
}
class SinGraph {
int iWidth;
int iHeight;
int iMoveStride = 3;
// volatile:最適化の抑制.
volatile int iMoveDistance = 0;
int iWaveQnt = 1;
double vdWaveHeightRate = iWaveQnt;
int iWaveStep = 0;
int iWaveWidth;
int iWaveHeight;
Color oColor = Color.BLUE;
double vdRadian;
double vdWaveWidth,vdWaveHeight;
double vdWaveX1,vdWaveX2;
double vdWaveY1,vdWaveY2;
double vdRate,vdRateInt;
SinGraph( int iW, int iH, int iWQ, Color oC ){
// コンストラクター自体には synchronized 修飾子を付与できないので、
// synchronized ブロックで囲う。
synchronized(this){
// iMoveDistance に対する競合(描画中の変更)を回避する。
// 対象は SinGraph("自分")内の変数です。
iWidth = iW;
iHeight = iH;
iWaveQnt = iWQ;
vdWaveHeightRate = iWaveQnt; // sin グラフの高さの調整。
oColor = oC;
}
}
synchronized void Draw(Graphics oGraphic){
// iMoveDistance に対する競合(描画中の変更)を回避する。
// 対象は SinGraph("自分")内の変数です。
Graphics2D oGraphic2D = (Graphics2D)oGraphic;
oGraphic2D.setColor(oColor);
vdWaveWidth = (double)iWidth/iWaveQnt;
vdWaveHeight = vdWaveWidth/(2*Math.PI)*vdWaveHeightRate;
iWaveStep = 0;
vdWaveX1 = -1; vdWaveX2 = -1;
while( iWaveStep<iWidth ){ // true / false
vdWaveX1 = vdWaveX2;
vdWaveY1 = vdWaveY2;
vdRate = (double)
(iWaveStep+iWidth-iMoveDistance)/iWidth*iWaveQnt;
vdRateInt = Math.floor(vdRate);
vdRate = vdRate-vdRateInt;
vdWaveX2 = iWaveStep;
vdRadian = 2*Math.PI*vdRate;
vdWaveY2 = iHeight/2-Math.sin(vdRadian)*vdWaveHeight;
iWaveStep++;
if(0<=vdWaveX1){
oGraphic2D.drawLine(
(int)Math.round(vdWaveX1), (int)Math.round(vdWaveY1),
(int)Math.round(vdWaveX2), (int)Math.round(vdWaveY2));
}
}
}
}
class CanvasObj extends JPanel {
// Java2 には JCanvas は存在しないので、
// グラフィックの描画には JPanel を継承して Canvas の代わりに使う。
Thread oThread;
// SinGraph oSinGraph;
int iWidth;
int iHeight;
ArrayList<SinGraph> dl1oSinGraph = new ArrayList<SinGraph>( );
CanvasObj(int x, int y, int w, int h ) {
super( );
System.out.println("oApplet:CanvasObj:");
setBounds(x, y, w, h);
iWidth = getWidth( );
iHeight = getHeight( );
SinGraph_Make( );
oThread = new Thread( ){
@Override
public void run( ){
while( true ){ // true / false
repaint();
try {
Thread.sleep(50); // ←50ミリ秒の sleep。
} catch (InterruptedException e) {
e.printStackTrace();
}
for( SinGraph oSinGraph : dl1oSinGraph.toArray(new SinGraph[0])){
// ↑この「new SinGraph[0]」の部分は要素がゼロ個の配列を指定しなければならないらしい。
// それにより指定された型と同一の型が返されるらしい。
synchronized(oSinGraph) {
// SinGraph内の iMoveDistance変数に対する競合(描画中の変更)を回避する。
// 対象は SinGraph内の変数なので、当然 ここは"他人"となります。
oSinGraph.iMoveDistance = oSinGraph.iMoveDistance+oSinGraph.iMoveStride;
if( iWidth<=oSinGraph.iMoveDistance ){ oSinGraph.iMoveDistance = oSinGraph.iMoveDistance-iWidth; }
}
}
}
}
};
oThread.start( );
}
void SinGraph_Make( ){
dl1oSinGraph.add(new SinGraph(iWidth, iHeight,
ccbSinGraph_WaveQnt.getSelectedIndex( )+1,
d1oCbSinGraph_ColorSets[ccbSinGraph_Color.getSelectedIndex( )]));
}
@Override
public void paint(Graphics oGraphic){
Graphics2D oGraphic2D = (Graphics2D)oGraphic;
// ↑(JPanel を継承しているので)ここの Graphics の実態は Graphics2D となるから、
// キャストしてやれば Graphics2D が使える.
oGraphic2D.setBackground(Color.BLACK);
oGraphic2D.clearRect(0, 0, iWidth, iHeight);
oGraphic2D.setColor(Color.WHITE);
oGraphic2D.drawLine(0, iHeight/2, iWidth, iHeight/2);
for( SinGraph oSinGraph : dl1oSinGraph.toArray(new SinGraph[0])){
// ↑この「new SinGraph[0]」の部分は要素がゼロ個の配列を指定しなければならないらしい。
// それにより指定された型と同一の型が返されるらしい。
oSinGraph.Draw(oGraphic2D);
}
}
@Override
public void update(Graphics oGraphic){
System.out.println("updat( );");
// Java のバージョンによって違うかもしれないが、
// 動的なグラフィック表示をする場合は
// update メソッドが存在しないと表示のチラツキの原因になる場合がある.
paint(oGraphic);
}
}
static class MainAppFrameObj extends JFrame implements Runnable {
// JApplet oApplet;
Thread oAppThread;
public MainAppFrameObj( ) {
super("Application frame.");
//setVisible(false);
System.out.println("Construct:Application frame window.");
// oApplet = oEntApplet; // new JAppletcation();
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent event){
// ユーザーがウインドウを閉じようとした時].
Object oSource = event.getSource();
if (oSource == MainAppFrameObj.this){
System.out.println("MainAppFrameObj:WindowEvent:windowClosing.");
Close();
}
}
}
);
addWindowListener(
new WindowAdapter(){
public void windowClosed(WindowEvent event){
// ウインドウが閉じた時.
Object oSource = event.getSource();
if (oSource == MainAppFrameObj.this){
System.out.println("MainAppFrameObj:WindowEvent:windowClosed.");
}
}
}
);
addWindowListener(
new WindowAdapter(){
public void windowDeiconified(WindowEvent event){
// ウィンドウが最小化された状態から通常の状態に変更された時.
Object oSource = event.getSource();
if (oSource == MainAppFrameObj.this){
System.out.println("MainAppFrameObj:WindowEvent:windowDeiconified.");
oMainApp.repaint();
}
}
}
);
addComponentListener(
new ComponentAdapter(){
public void componentResized(ComponentEvent event){
// コンポーネントのサイズが変更された時.
Object oSource = event.getSource();
if (oSource == MainAppFrameObj.this){
System.out.println("ComponentEvent:componentResized.");
System.out.println("MainAppFrameObj: "+
"width="+Integer.toString(MainAppFrameObj.this.getSize( ).width)+"; "+
"Height="+Integer.toString(MainAppFrameObj.this.getSize( ).height)+"; "+
"");
oMainApp.repaint();
}
}
}
);
getContentPane().add(oMainApp,"Center");
oAppThread = new Thread(this);
}
public synchronized void setPreSize(Dimension oSize){
// dispose();
getContentPane().setPreferredSize(oSize);
pack();
setVisible(true);
requestFocus();
}
public synchronized void run(){
System.out.println("Open:Application frame window.");
oMainApp.init();
oMainApp.start();
oMainApp.repaint();
try{
wait();
}
catch(InterruptedException e){
e.printStackTrace();
}
oMainApp.stop();
oMainApp.destroy();
oAppThread = null;
}
public synchronized void Close(){
System.out.println("Close:Application frame window.");
dispose();
notify();
}
}
}
|
|
|
<Number>: [00000998]
<Date>: 2016/01/10 08:55:27
<Title>:
<Name>: amanojaku@管理人
|
|
|
|
|
<Number>: [0000099D]
<Date>: 2016/01/10 13:45:17
<Title>:
<Name>: amanojaku@管理人
|
|
|
Appletcation を ほんのチョッピリ修正、「Java2 FTP Connect 12」と同様のマウスのダブル・クリック検出用プログラムを実装。
マウスのダブル・クリック用インターバル「ilMouseDoubleClickedIntervalMax = 180;」に設定。
《参考》
『Java2 MouseDoubleClicked 1』
http://artemis.rosx.net/sjis/smt.cgi?r+izanami/&bid+00000918&tsn+0000096B.+&
『Java2 FTP Connect 12』
http://artemis.rosx.net/sjis/smt.cgi?r+izanami/&bid+00000918&tsn+00000976-00000978&
(分かりやすければ他のファイル名でも良い)「MainPanelDesign」(MainPanelDesign.java)ファイルを Visual Swing for Eclipse でビジュアルに画面デザイン(GUI 部品の配置)を作成している(Java コードが自動生成される)。
「MainAppFrameObj、MainPanelImplementationObj」 からアプレット独自メソッドにアクセスする場合を想定すると、「MainAppFrameObj、MainPanelImplementationObj」はアプレット・クラス内に設置した方が良いだろう。
(アプリーケーションの初期実行用) main( ) メソッドで(MainAppFrameObjなどのような)クラス内クラスをインスタンス化したい場合(main( ) メソッド実行時には、まだアプレット自体のインスタンスが生成されておらず、通常のクラス内クラスではインスタンス化できないので) 、クラス内クラスには「static」修飾子を付与しなければならない(この場合、オブジェクトは1つだけしか作成できない)。
クリック時にマウス・カーソルが動いてしまった場合 mouseClicked イベントだとイベントが発生しない"仕様"になっているようなので、mouse イベントに関しては mouseReleased イベントを使用している。
今回はダブル・クリック検出用に mousePressed イベントも使用している。
変更した部分はイベント用メソッドの「Test_CbtNativeMouseMouseReleased、Test_CbtNativeMouseMousePressed」の private 修飾子を削除。
その他、「jLabel0」変数の private 修飾子を削除(Button など Component を直にイジる必要がなければ private 修飾子を削除しなくても良い)。
『MouseDoubleClicked.java』
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
//Event を使う場合は、その Event に対応する「〜Adapter、〜Event」を import しなければならない。
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import javax.swing.JApplet;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class MouseDoubleClicked extends JApplet{
static String vsApplicationTitle = "Application MouseDoubleClicked 2.";
static MouseDoubleClicked oMainApp;
static boolean lApplication = false; // true;
static MainAppFrameObj oMainAppFrame;
MainPanelImplementationObj oMainPanelImp;
public static void main(String[] args) {
oMainApp = new MouseDoubleClicked( );
lApplication = true; // false;
oMainAppFrame =
new MainAppFrameObj(vsApplicationTitle);
oMainAppFrame.oAppThread.start();
try{
oMainAppFrame.oAppThread.join();
}
catch(InterruptedException e){ }
System.exit(0);
}
public void init() {
System.out.println("init( );");
oMainApp = this;
oMainPanelImp = new MainPanelImplementationObj();
getContentPane().add(oMainPanelImp,"Center");
// oMainPanelImp.setVisible(true);
System.out.println(
"oMainApp.getWidth( )="+Integer.toString(oMainApp.getWidth())+"; "+
"oMainApp.getHeight( )="+Integer.toString(oMainApp.getHeight())+"; "+
"oMainApp.getSize( ).width="+Integer.toString(oMainApp.getSize( ).width)+"; "+
"oMainApp.getSize( ).height="+Integer.toString(oMainApp.getSize( ).height)+"; "+
"oMainApp.getMinimumSize( ).width="+Integer.toString(oMainApp.getMinimumSize().width)+"; "+
"oMainApp.getMinimumSize( ).height="+Integer.toString(oMainApp.getMinimumSize().height)+"; "+
"");
if( lApplication ){
System.out.println("if(lApplication)");
oMainAppFrame.SetPreSize(this);
System.out.println("MainPanelWidth="+oMainPanelImp.getSize( ).getWidth());
System.out.println("MainPanelHeight="+oMainPanelImp.getSize( ).getHeight());
}
System.out.println("AppletWidth="+getSize( ).getWidth());
System.out.println("AppletHeight="+getSize( ).getHeight());
System.out.println("Hello, World!");
}
public void start(){
System.out.println("start( );");
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
System.out.println("ScreenWidth="+gc.getBounds().getWidth());
System.out.println("ScreenHeight="+gc.getBounds().getHeight());
repaint();
}
public void stop(){
System.out.println("stop( );");
}
public void destroy(){
System.out.println("destroy( );");
}
enum MouseMouseDoubleClicked_Enum { feTest };
class MainPanelImplementationObj extends MainPanelDesign {
long ilMouseDoubleClickedIntervalMax = 180; // 200; 250; 300; 350;
private HashMap<MouseMouseDoubleClicked_Enum, Runner_MouseMouseDoubleClicked> da1oRunnable_MouseMouseDoubleClicked =
new HashMap<MouseMouseDoubleClicked_Enum, Runner_MouseMouseDoubleClicked>( );
// volatile:最適化の抑制.
volatile byte ibCnt_Test_CbtMouseMouseDoubleClicked = 0;
Thread oThread_Test_CbtMouseMouseDoubleClicked;
Runnable oRunner_Test_CbtMouseMouseDoubleClicked;
class Runner_MouseMouseDoubleClicked implements Runnable {
private Thread oThread;
// volatile:最適化の抑制.
volatile private MouseMouseDoubleClicked_Enum eMouseDoubleClicked;
volatile byte ibCnt_MouseEvent = 0;
MouseEvent oEvent_MouseClicked = null;
Runner_MouseMouseDoubleClicked( MouseMouseDoubleClicked_Enum eMMDC ){
eMouseDoubleClicked = eMMDC;
oThread = new Thread(this);
oThread.start();
}
void NativeMouseMousePressed(MouseEvent event) {
System.out.println("NativeMouseMousePressed( ).");
System.out.println(
"eMouseDoubleClicked.name( )="+eMouseDoubleClicked.name( )+"; "+
"ibCnt_MouseEvent="+ibCnt_MouseEvent+"; "+
"");
oEvent_MouseClicked = null;
if(oThread.getState()==Thread.State.TIMED_WAITING){
ibCnt_MouseEvent = 3;
oThread.interrupt();
}else{
ibCnt_MouseEvent = 1;
}
System.out.println(
"eMouseDoubleClicked.name( )="+eMouseDoubleClicked.name( )+"; "+
"ibCnt_MouseEvent="+ibCnt_MouseEvent+"; "+
"");
}
boolean NativeMouseMouseReleased(MouseEvent event) {
System.out.println("NativeMouseMouseReleased( ).");
System.out.println(
"eMouseDoubleClicked.name( )="+eMouseDoubleClicked.name( )+"; "+
"ibCnt_MouseEvent="+ibCnt_MouseEvent+"; "+
"");
oEvent_MouseClicked = event;
boolean lDoubleClicked = false; // true; //
if(ibCnt_MouseEvent==3){
ibCnt_MouseEvent = 0;
lDoubleClicked = true; // false; //
}else if(oThread.getState()==Thread.State.WAITING){
ibCnt_MouseEvent = 2;
synchronized(this) {
notify();
// ↑メソッドを synchronized 指定するか、
// synchronized(自分のインスタンス) ブロックで囲うかしないと
// 実行時に「IllegalMonitorStateException」が発生する。
}
}
System.out.println(
"eMouseDoubleClicked.name( )="+eMouseDoubleClicked.name( )+"; "+
"ibCnt_MouseEvent="+ibCnt_MouseEvent+"; "+
"lDoubleClicked="+lDoubleClicked+"; "+
"");
return lDoubleClicked;
}
@Override
public void run( ) {
System.out.println("run.");
boolean lInterrupted = false; // true; //
while(true){
try {
synchronized(this) {
wait( );
// ↑メソッドを synchronized 指定するか、
// synchronized(自分のインスタンス) ブロックで囲うかしないと
// 実行時に「IllegalMonitorStateException」が発生する。
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("wait:End.");
lInterrupted = false; // true; //
try {
Thread.sleep(ilMouseDoubleClickedIntervalMax);
} catch (InterruptedException e) {
// e.printStackTrace();
System.out.println("sleep:InterruptedException.");
lInterrupted = true; // false; //
}
System.out.println("sleep:End.");
if( ! lInterrupted &&
ibCnt_MouseEvent==2 ){
ibCnt_MouseEvent = 0;
switch(eMouseDoubleClicked){
case feTest :
Test_CbtMouseMouseClicked(oEvent_MouseClicked);
break;
default:
System.out.println(
"★Error:MouseDoubleClicked="+eMouseDoubleClicked.name( )+"; "+
"");
}
oEvent_MouseClicked = null;
}
}
}
}
MainPanelImplementationObj( ){
super( );
for( MouseMouseDoubleClicked_Enum eEnum : MouseMouseDoubleClicked_Enum.values( ) ){
da1oRunnable_MouseMouseDoubleClicked.put(eEnum,
new Runner_MouseMouseDoubleClicked(eEnum));
}
}
void Test_CbtMouseMouseClicked(MouseEvent event) {
System.out.println("MainPanelImplementationObj:MouseEvent:Test_CbtMouseMouseClicked.");
jLabel0.setText("SingleClicked");
}
void Test_CbtMouseMouseDoubleClicked(MouseEvent event) {
System.out.println("MainPanelImplementationObj:MouseEvent:Test_CbtMouseMouseDoubleClicked.");
jLabel0.setText("DoubleClicked");
}
@Override
void Test_CbtNativeMouseMousePressed(MouseEvent event) {
System.out.println("MainPanelImplementationObj:MouseEvent:Test_CbtNativeMouseMousePressed.");
System.out.println("currentTimeMillis="+System.currentTimeMillis() );
da1oRunnable_MouseMouseDoubleClicked.get(
MouseMouseDoubleClicked_Enum.feTest).NativeMouseMousePressed(event);
}
@Override
public void Test_CbtNativeMouseMouseReleased(MouseEvent event) {
System.out.println("MainPanelImplementationObj:MouseEvent:Test_CbtNativeMouseMouseReleased.");
System.out.println("currentTimeMillis="+System.currentTimeMillis() );
boolean lDoubleClicked = da1oRunnable_MouseMouseDoubleClicked.get(
MouseMouseDoubleClicked_Enum.feTest).NativeMouseMouseReleased(event);
if( lDoubleClicked ){
Test_CbtMouseMouseDoubleClicked(event);
}
}
}
static class MainAppFrameObj extends JFrame implements Runnable {
// JApplet oApplet;
Thread oAppThread;
public MainAppFrameObj(String vsAppTitle) {
super(vsAppTitle);
//setVisible(false);
System.out.println("MainAppFrameObj:Construct:Application frame window.");
// oApplet = oEntApplet; // new JAppletcation();
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent event){
// ユーザーがウインドウを閉じようとした時].
Object oSource = event.getSource();
if (oSource == MainAppFrameObj.this){
System.out.println("MainAppFrameObj:WindowEvent:windowClosing.");
Close();
}
}
}
);
addWindowListener(
new WindowAdapter(){
public void windowClosed(WindowEvent event){
// ウインドウが閉じた時.
Object oSource = event.getSource();
if (oSource == MainAppFrameObj.this){
System.out.println("MainAppFrameObj:WindowEvent:windowClosed.");
}
}
}
);
addWindowListener(
new WindowAdapter(){
public void windowDeiconified(WindowEvent event){
// ウィンドウが最小化された状態から通常の状態に変更された時.
Object oSource = event.getSource();
if (oSource == MainAppFrameObj.this){
System.out.println("MainAppFrameObj:WindowEvent:windowDeiconified.");
oMainApp.repaint();
}
}
}
);
addComponentListener(
new ComponentAdapter(){
public void componentResized(ComponentEvent event){
// コンポーネントのサイズが変更された時.
Object oSource = event.getSource();
if (oSource == MainAppFrameObj.this){
System.out.println("MainAppFrameObj:ComponentEvent:componentResized.");
System.out.println("MainAppFrameObj: "+
"width="+Integer.toString(MainAppFrameObj.this.getSize( ).width)+"; "+
"Height="+Integer.toString(MainAppFrameObj.this.getSize( ).height)+"; "+
"");
oMainApp.repaint();
}
}
}
);
getContentPane().add(oMainApp,"Center");
oAppThread = new Thread(this);
}
public synchronized void SetPreSize(Component oComponent){
System.out.println("MainAppFrameObj:SetPreSize.");
// dispose();
System.out.println(
"oComponent.getWidth( )="+Integer.toString(oComponent.getWidth())+"; "+
"oComponent.getHeight( )="+Integer.toString(oComponent.getHeight())+"; "+
"oComponent.getSize( ).width="+Integer.toString(oComponent.getSize( ).width)+"; "+
"oComponent.getSize( ).height="+Integer.toString(oComponent.getSize( ).height)+"; "+
"oComponent.getMinimumSize( ).width="+Integer.toString(oComponent.getMinimumSize().width)+"; "+
"oComponent.getMinimumSize( ).height="+Integer.toString(oComponent.getMinimumSize().height)+"; "+
"");
getContentPane( ).setPreferredSize(
new Dimension(Math.max(oComponent.getSize( ).width, oComponent.getMinimumSize( ).width),
Math.max(oComponent.getSize( ).height, oComponent.getMinimumSize( ).height)));
pack( );
setVisible(true);
requestFocus( );
}
public synchronized void run(){
System.out.println("MainAppFrameObj:run.");
oMainApp.init();
oMainApp.start();
oMainApp.repaint();
try{
wait();
}
catch(InterruptedException e){
e.printStackTrace();
}
oMainApp.stop();
oMainApp.destroy();
oAppThread = null;
}
public synchronized void Close(){
System.out.println("MainAppFrameObj:Close:Application frame window.");
dispose();
notify();
}
}
}
『MainPanelDesign.java』
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;
//VS4E -- DO NOT REMOVE THIS LINE!
public class MainPanelDesign extends JPanel {
private static final long serialVersionUID = 1L;
private JButton jButton0;
JLabel jLabel0;
public MainPanelDesign() {
initComponents();
}
private void initComponents() {
setLayout(new GroupLayout());
add(getJLabel0(), new Constraints(new Leading(12, 259, 12, 12), new Leading(36, 10, 10)));
add(getJButton0(), new Constraints(new Leading(12, 12, 12), new Leading(83, 10, 10)));
setSize(288, 141);
}
private JLabel getJLabel0() {
if (jLabel0 == null) {
jLabel0 = new JLabel();
jLabel0.setText("jLabel0");
}
return jLabel0;
}
private JButton getJButton0() {
if (jButton0 == null) {
jButton0 = new JButton();
jButton0.setText("jButton0");
jButton0.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
Test_CbtNativeMouseMousePressed(event);
}
public void mouseReleased(MouseEvent event) {
Test_CbtNativeMouseMouseReleased(event);
}
});
}
return jButton0;
}
void Test_CbtNativeMouseMouseReleased(MouseEvent event) {
}
void Test_CbtNativeMouseMousePressed(MouseEvent event) {
}
}
|
|
|
<Number>: [000009A2]
<Date>: 2016/01/10 21:03:35
<Title>:
<Name>: amanojaku@管理人
|
|
|
Sin グラフを横スクロールさせる、ComboBox で「波長の数、カラー」を指定して、[Make]ボタンで新しい Sin グラフを追加できます。
2つ以上の Sin グラフを表示すると、白色で合成波を表示します。
なお、SinGraph の Draw メソッドは使用していません。
「MainAppFrameObj」 からアプレット独自メソッドにアクセスする場合を想定すると、「MainAppFrameObj」はアプレット・クラス内に設置した方が良いだろう。
(アプリーケーションの初期実行用) main( ) メソッドで(MainAppFrameObjなどのような)クラス内クラスをインスタンス化したい場合(main( ) メソッド実行時には、まだアプレット自体のインスタンスが生成されておらず、通常のクラス内クラスではインスタンス化できないので) 、クラス内クラスには「static」修飾子を付与しなければならない(この場合、オブジェクトは1つだけしか作成できない)。
クリック時にマウス・カーソルが動いてしまった場合 mouseClicked イベントだとイベントが発生しない"仕様"になっているようなので、mouse イベントに関しては mouseReleased イベントを使用している。
『Appletcation.java』
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
//Event を使う場合は、その Event に対応する「〜Adapter、〜Event」を import しなければならない。
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Appletcation extends JApplet{
static String vsApplicationTitle = "Application SinGraph 6.";
static Appletcation oMainApp;
static boolean lApplication = false; // true;
static MainAppFrameObj oMainAppFrame;
int iLayoutX, iLayoutY;
CanvasObj oCanvas;
int iCanvasWidth,iCanvasHeight;
JComboBox ccbSinGraph_WaveQnt;
int iCbSinGraph_WaveQnt_Width,iCbSinGraph_WaveQnt_Height;
JComboBox ccbSinGraph_Color;
int iCbSinGraph_Color_Width,iCbSinGraph_Color_Height;
Color[] d1oCbSinGraph_ColorSets;
JButton cbtSinGraph_Make;
int iBtSinGraph_Make_Width, iBtSinGraph_Make_Height;
public static void main(String[] args) {
oMainApp = new Appletcation( );
lApplication = true; // false;
oMainAppFrame =
new MainAppFrameObj(vsApplicationTitle);
oMainAppFrame.oAppThread.start();
try{
oMainAppFrame.oAppThread.join();
}
catch(InterruptedException e){ }
System.exit(0);
}
public void init() {
System.out.println("init( );");
oMainApp = this;
getContentPane().setLayout(null); // Layout 座標指定モード
ccbSinGraph_WaveQnt = new JComboBox( );
ccbSinGraph_WaveQnt.setModel(new DefaultComboBoxModel(new Object[] { " 1 ", " 2 ", " 3 " }));
ccbSinGraph_WaveQnt.setDoubleBuffered(false);
ccbSinGraph_WaveQnt.setBorder(null);
System.out.println(
"ccbSinGraph_WaveQnt.getWidth()="+Integer.toString(ccbSinGraph_WaveQnt.getWidth())+"; "+
"ccbSinGraph_WaveQnt.getHeight()="+Integer.toString(ccbSinGraph_WaveQnt.getHeight())+"; "+
"ccbSinGraph_WaveQnt.getMinimumSize().width="+Integer.toString(ccbSinGraph_WaveQnt.getMinimumSize().width)+"; "+
"ccbSinGraph_WaveQnt.getMinimumSize().height="+Integer.toString(ccbSinGraph_WaveQnt.getMinimumSize().height)+"; "+
"");
iCbSinGraph_WaveQnt_Width = ccbSinGraph_WaveQnt.getMinimumSize().width;
iCbSinGraph_WaveQnt_Height = ccbSinGraph_WaveQnt.getMinimumSize().height;
d1oCbSinGraph_ColorSets = new Color[]{
Color.GREEN, Color.PINK,
Color.RED, Color.YELLOW,
};
ccbSinGraph_Color = new JComboBox( );
ccbSinGraph_Color.setModel(new DefaultComboBoxModel(new Object[]
{ "グリーン", "ピンク", "赤", "黄色", }));
ccbSinGraph_Color.setDoubleBuffered(false);
ccbSinGraph_Color.setBorder(null);
iCbSinGraph_Color_Width = ccbSinGraph_Color.getMinimumSize().width;
iCbSinGraph_Color_Height = ccbSinGraph_Color.getMinimumSize().height;
cbtSinGraph_Make = new JButton( );
cbtSinGraph_Make.setText("Make");
System.out.println(
"cbtSinGraph_Make.getWidth()="+Integer.toString(cbtSinGraph_Make.getWidth())+"; "+
"cbtSinGraph_Make.getHeight()="+Integer.toString(cbtSinGraph_Make.getHeight())+"; "+
"cbtSinGraph_Make.getMinimumSize().width="+Integer.toString(cbtSinGraph_Make.getMinimumSize().width)+"; "+
"cbtSinGraph_Make.getMinimumSize().height="+Integer.toString(cbtSinGraph_Make.getMinimumSize().height)+"; "+
"");
iBtSinGraph_Make_Width = cbtSinGraph_Make.getMinimumSize().width;
iBtSinGraph_Make_Height = cbtSinGraph_Make.getMinimumSize().height;
cbtSinGraph_Make.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
if(null!=oCanvas){
oCanvas.SinGraph_Make( );
}
}
});
iCanvasWidth = 500;
iCanvasHeight = 500;
iLayoutX = 0; iLayoutY = 0;
getContentPane().add(ccbSinGraph_WaveQnt);
ccbSinGraph_WaveQnt.setBounds(iLayoutX,iLayoutY, iCbSinGraph_WaveQnt_Width, iCbSinGraph_WaveQnt_Height);
iLayoutX = iLayoutX+iCbSinGraph_WaveQnt_Width; // iLayoutY = 0;
getContentPane().add(ccbSinGraph_Color);
ccbSinGraph_Color.setBounds(iLayoutX,iLayoutY, iCbSinGraph_Color_Width, iCbSinGraph_Color_Height);
iLayoutX = iLayoutX+iCbSinGraph_Color_Width; // iLayoutY = 0;
getContentPane().add(cbtSinGraph_Make);
cbtSinGraph_Make.setBounds(iLayoutX,iLayoutY, iBtSinGraph_Make_Width, iBtSinGraph_Make_Height);
iLayoutX = 0; iLayoutY = iLayoutY+iBtSinGraph_Make_Height;
oCanvas = new CanvasObj(iLayoutX, iLayoutY, iCanvasWidth, iCanvasHeight);
getContentPane().add(oCanvas);
// oCanvas.setVisible(true);
iLayoutX = iCanvasWidth; iLayoutY = iLayoutY+iCanvasHeight;
this.setSize(iLayoutX, iLayoutY);
System.out.println(
"this.getWidth()="+Integer.toString(this.getWidth())+"; "+
"this.getHeight()="+Integer.toString(this.getHeight())+"; "+
"this.getMinimumSize().width="+Integer.toString(this.getMinimumSize().width)+"; "+
"this.getMinimumSize().height="+Integer.toString(this.getMinimumSize().height)+"; "+
"");
if( lApplication ){
System.out.println("if(lApplication)");
oMainAppFrame.SetPreSize(this);
System.out.println("PanelWidth="+oCanvas.getSize( ).getWidth());
System.out.println("PanelHeight="+oCanvas.getSize( ).getHeight());
}
System.out.println("AppletWidth="+getSize( ).getWidth());
System.out.println("AppletHeight="+getSize( ).getHeight());
}
public void start(){
System.out.println("start( );");
repaint();
}
public void stop(){
System.out.println("stop( );");
}
public void destroy(){
System.out.println("destroy( );");
}
class SinGraph {
CanvasObj oCanvas;
// int iWidth, iHeight;
int iMoveStride = 3;
// volatile:最適化の抑制.
volatile int iMoveDistance = 0;
int iWaveQnt = 1;
double vdWaveHeightRate = iWaveQnt;
int iGraphXStep = 0;
int iWaveWidth;
int iWaveHeight;
Color oColor = Color.BLUE;
double vdRadian;
double vdWaveWidth, vdWaveHeight;
double vdGraphX1, vdGraphX2;
double vdGraphY1, vdGraphY2;
double vdWaveY1, vdWaveY2;
double vdRate, vdRateInt;
SinGraph( CanvasObj ccv, int iWQ, Color oC ){
// コンストラクター自体には synchronized 修飾子を付与できないので、
// synchronized ブロックで囲う。
synchronized(this){
// iMoveDistance に対する競合(描画中の変更)を回避する。
// 対象は SinGraph("自分")内の変数です。
oCanvas = ccv;
iWaveQnt = iWQ;
vdWaveHeightRate = iWaveQnt; // sin グラフの高さの調整。
oColor = oC;
}
}
synchronized void Draw(Graphics oGraphic){
// iMoveDistance に対する競合(描画中の変更)を回避する。
// 対象は SinGraph("自分")内の変数です。
Graphics2D oGraphic2D = (Graphics2D)oGraphic;
vdGraphX1 = -1; vdGraphX2 = -1;
oGraphic2D.setColor(oColor);
vdWaveWidth = (double)oCanvas.iWidth/iWaveQnt;
vdWaveHeight = vdWaveWidth/(2*Math.PI)*vdWaveHeightRate;
iGraphXStep = 0;
while( iGraphXStep<oCanvas.iWidth ){ // true / false
DrawBullet(oGraphic, iGraphXStep);
iGraphXStep++;
}
}
void DrawBullet(Graphics oGraphic, int iBulletGraphXStep){
Graphics2D oGraphic2D = (Graphics2D)oGraphic;
oGraphic2D.setColor(oColor);
if( 0==iBulletGraphXStep ){
vdGraphX1 = -1;
vdGraphX2 = -1;
}
vdWaveWidth = (double)oCanvas.iWidth/iWaveQnt;
vdWaveHeight = vdWaveWidth/(2*Math.PI)*vdWaveHeightRate;
vdGraphX1 = vdGraphX2;
vdGraphY1 = vdGraphY2;
vdWaveY1 = vdWaveY2;
vdRate = (double)
(iBulletGraphXStep+oCanvas.iWidth-iMoveDistance)/oCanvas.iWidth*iWaveQnt;
vdRateInt = Math.floor(vdRate);
vdRate = vdRate-vdRateInt;
vdGraphX2 = iBulletGraphXStep;
vdRadian = 2*Math.PI*vdRate;
vdWaveY2 = Math.sin(vdRadian)*vdWaveHeight;
vdGraphY2 = oCanvas.iHeight/2-vdWaveY2;
if(0<=vdGraphX1){
oGraphic2D.drawLine(
(int)Math.round(vdGraphX1), (int)Math.round(vdGraphY1),
(int)Math.round(vdGraphX2), (int)Math.round(vdGraphY2));
// oCanvas.vdGraphX1 = oCanvas.vdGraphX1+vdGraphX1;
// oCanvas.vdGraphX2 = oCanvas.vdGraphX2+vdGraphX2;
oCanvas.vdWaveY1 = oCanvas.vdWaveY1+vdWaveY1;
oCanvas.vdWaveY2 = oCanvas.vdWaveY2+vdWaveY2;
}
}
}
class CanvasObj extends JPanel {
// Java2 には JCanvas は存在しないので、
// グラフィックの描画には JPanel を継承して Canvas の代わりに使う。
Thread oThread;
// SinGraph oSinGraph;
int iWidth;
int iHeight;
ArrayList<SinGraph> dl1oSinGraph = new ArrayList<SinGraph>( );
int iGraphXStep;
double vdGraphX1,vdGraphX2;
double vdGraphY1,vdGraphY2;
double vdWaveY1,vdWaveY2;
double vdWaveYRate = 0.666; // 合成波のY軸の倍率。
CanvasObj(int x, int y, int w, int h ) {
super( );
System.out.println("oApplet:CanvasObj:");
setBounds(x, y, w, h);
iWidth = getWidth( );
iHeight = getHeight( );
SinGraph_Make( );
oThread = new Thread( ){
@Override
public void run( ){
while( true ){ // true / false
repaint();
try {
Thread.sleep(50); // ←50ミリ秒の sleep。
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(CanvasObj.this) {
// SinGraph内の iMoveDistance変数に対する競合(描画中の変更)を回避する。
// 対象は SinGraph内の変数なのだが、
// (CanvasObj の) paint内のループ全体の処理を1つと見なす必要があるので
// 実際の対象は CanvasObj とする必要がある。
for( SinGraph oSinGraph : dl1oSinGraph.toArray(new SinGraph[0])){
// ↑この「new SinGraph[0]」の部分は要素がゼロ個の配列を指定しなければならないらしい。
// それにより指定された型と同一の型が返されるらしい。
oSinGraph.iMoveDistance = oSinGraph.iMoveDistance+oSinGraph.iMoveStride;
if( iWidth<=oSinGraph.iMoveDistance ){ oSinGraph.iMoveDistance = oSinGraph.iMoveDistance-iWidth; }
}
}
}
}
};
oThread.start( );
}
void SinGraph_Make( ){
dl1oSinGraph.add(new SinGraph(this,
ccbSinGraph_WaveQnt.getSelectedIndex( )+1,
d1oCbSinGraph_ColorSets[ccbSinGraph_Color.getSelectedIndex( )]));
}
@Override
public synchronized void paint(Graphics oGraphic){
Graphics2D oGraphic2D = (Graphics2D)oGraphic;
// ↑(JPanel を継承しているので)ここの Graphics の実態は Graphics2D となるから、
// キャストしてやれば Graphics2D が使える.
oGraphic2D.setBackground(Color.BLACK);
oGraphic2D.clearRect(0, 0, iWidth, iHeight);
oGraphic2D.setColor(Color.WHITE);
oGraphic2D.drawLine(0, iHeight/2, iWidth, iHeight/2);
iGraphXStep = 0;
vdGraphX1 = 0; vdGraphX2 = 0;
while( iGraphXStep<iWidth ){ // true / false
vdWaveY1 = 0; vdWaveY2 = 0;
vdGraphX1 = vdGraphX2;
for( SinGraph oSinGraph : dl1oSinGraph.toArray(new SinGraph[0])){
// ↑この「new SinGraph[0]」の部分は要素がゼロ個の配列を指定しなければならないらしい。
// それにより指定された型と同一の型が返されるらしい。
oSinGraph.DrawBullet(oGraphic, iGraphXStep);
}
vdGraphX2 = iGraphXStep;
if( 2<=dl1oSinGraph.size( ) && 0<iGraphXStep ){
vdGraphY1 = iHeight/2-vdWaveY1*vdWaveYRate;
vdGraphY2 = iHeight/2-vdWaveY2*vdWaveYRate;
oGraphic2D.setColor(Color.WHITE);
oGraphic2D.drawLine(
(int)Math.round(vdGraphX1), (int)Math.round(vdGraphY1),
(int)Math.round(vdGraphX2), (int)Math.round(vdGraphY2));
}
iGraphXStep++;
}
}
@Override
public void update(Graphics oGraphic){
System.out.println("updat( );");
// Java のバージョンによって違うかもしれないが、
// 動的なグラフィック表示をする場合は
// update メソッドが存在しないと表示のチラツキの原因になる場合がある.
paint(oGraphic);
}
}
static class MainAppFrameObj extends JFrame implements Runnable {
// JApplet oApplet;
Thread oAppThread;
public MainAppFrameObj(String vsAppTitle) {
super(vsAppTitle);
//setVisible(false);
System.out.println("MainAppFrameObj:Construct:Application frame window.");
// oApplet = oEntApplet; // new JAppletcation();
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent event){
// ユーザーがウインドウを閉じようとした時].
Object oSource = event.getSource();
if (oSource == MainAppFrameObj.this){
System.out.println("MainAppFrameObj:WindowEvent:windowClosing.");
Close();
}
}
}
);
addWindowListener(
new WindowAdapter(){
public void windowClosed(WindowEvent event){
// ウインドウが閉じた時.
Object oSource = event.getSource();
if (oSource == MainAppFrameObj.this){
System.out.println("MainAppFrameObj:WindowEvent:windowClosed.");
}
}
}
);
addWindowListener(
new WindowAdapter(){
public void windowDeiconified(WindowEvent event){
// ウィンドウが最小化された状態から通常の状態に変更された時.
Object oSource = event.getSource();
if (oSource == MainAppFrameObj.this){
System.out.println("MainAppFrameObj:WindowEvent:windowDeiconified.");
oMainApp.repaint();
}
}
}
);
addComponentListener(
new ComponentAdapter(){
public void componentResized(ComponentEvent event){
// コンポーネントのサイズが変更された時.
Object oSource = event.getSource();
if (oSource == MainAppFrameObj.this){
System.out.println("MainAppFrameObj:ComponentEvent:componentResized.");
System.out.println("MainAppFrameObj: "+
"width="+Integer.toString(MainAppFrameObj.this.getSize( ).width)+"; "+
"Height="+Integer.toString(MainAppFrameObj.this.getSize( ).height)+"; "+
"");
oMainApp.repaint();
}
}
}
);
getContentPane().add(oMainApp,"Center");
oAppThread = new Thread(this);
}
public synchronized void SetPreSize(Component oComponent){
System.out.println("MainAppFrameObj:SetPreSize.");
// dispose();
System.out.println(
"oComponent.getWidth( )="+Integer.toString(oComponent.getWidth())+"; "+
"oComponent.getHeight( )="+Integer.toString(oComponent.getHeight())+"; "+
"oComponent.getSize( ).width="+Integer.toString(oComponent.getSize( ).width)+"; "+
"oComponent.getSize( ).height="+Integer.toString(oComponent.getSize( ).height)+"; "+
"oComponent.getMinimumSize( ).width="+Integer.toString(oComponent.getMinimumSize().width)+"; "+
"oComponent.getMinimumSize( ).height="+Integer.toString(oComponent.getMinimumSize().height)+"; "+
"");
getContentPane( ).setPreferredSize(
new Dimension(Math.max(oComponent.getSize( ).width, oComponent.getMinimumSize( ).width),
Math.max(oComponent.getSize( ).height, oComponent.getMinimumSize( ).height)));
pack( );
setVisible(true);
requestFocus( );
}
public synchronized void run(){
System.out.println("MainAppFrameObj:run.");
oMainApp.init();
oMainApp.start();
oMainApp.repaint();
try{
wait();
}
catch(InterruptedException e){
e.printStackTrace();
}
oMainApp.stop();
oMainApp.destroy();
oAppThread = null;
}
public synchronized void Close(){
System.out.println("MainAppFrameObj:Close:Application frame window.");
dispose();
notify();
}
}
}
|
|
|
<Number>: [000009A7]
<Date>: 2016/01/10 21:09:47
<Title>:
<Name>: amanojaku@管理人
|
|
|
|
|
<Number>: [000009AF]
<Date>: 2016/01/18 19:17:54
<Title>:
<Name>: amanojaku@管理人
|
|
|
以前、作成した「キャンバス(Panel)、ボタン、イベント」Appletcation を(Appletcation 部分を ほんのチョッピリ変更した) Appletcation 10 に換装。
Frame(ウインドウ)の「マウス・ドラッグによるサイズ変更時、最小化・状態から通常・状態への変更時」に Applet を repaint してやる。
普通に Frame のサイズを設定するとタイトルバーの高さのサイズだけ内部の領域が狭くなってしまうが、「getContentPane().setPreferredSize」で内部の領域のサイズが設定でき、その後に「pack()」してやれば良い。
「MainAppFrameObj、MainPanelImplementationObj」 からアプレット独自メソッドにアクセスする場合を想定すると、「MainAppFrameObj、MainPanelImplementationObj」はアプレット・クラス内に設置した方が良いだろう。
(アプリーケーションの初期実行用) main( ) メソッドで(MainAppFrameObjなどのような)クラス内クラスをインスタンス化したい場合(main( ) メソッド実行時には、まだアプレット自体のインスタンスが生成されておらず、通常のクラス内クラスではインスタンス化できないので) 、クラス内クラスには「static」修飾子を付与しなければならない(この場合、オブジェクトは1つだけしか作成できない)。
(分かりやすければ他のファイル名でも良い)「MainPanelDesign」(MainPanelDesign.java)ファイルを Visual Swing for Eclipse でビジュアルに画面デザイン(GUI 部品の配置)を作成している(Java コードが自動生成される)。
クリック時にマウス・カーソルが動いてしまった場合 mouseClicked イベントだとイベントが発生しない"仕様"になっているようなので、mouseReleased イベントを使用している。
変更した部分は mouseReleased イベント用の「jButton0MouseMouseReleased、jButton1MouseMouseReleased、jButton2MouseMouseReleased」メソッドの private 修飾子を削除。
その他、「jPanel0」変数、「getJPanel0」メソッドの private 修飾子を削除。
『Appletcation.java』
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
//Event を使う場合は、その Event に対応する「〜Adapter、〜Event」を import しなければならない。
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Appletcation extends JApplet{
static String vsApplicationTitle = "Application 11 MouseDoubleClicked 1.";
static Appletcation oMainApp;
static boolean lApplication = false; // true;
static MainAppFrameObj oMainAppFrame;
MainPanelImplementationObj oMainPanelImp;
public static void main(String[] args) {
oMainApp = new Appletcation( );
lApplication = true; // false;
oMainAppFrame =
new MainAppFrameObj(vsApplicationTitle);
oMainAppFrame.oAppThread.start();
try{
oMainAppFrame.oAppThread.join();
}
catch(InterruptedException e){ }
System.exit(0);
}
public void init() {
System.out.println("init( );");
oMainApp = this;
// getContentPane().setLayout(null); // Layout 座標指定モード
oMainPanelImp = new MainPanelImplementationObj();
getContentPane().add(oMainPanelImp,"Center");
if( lApplication ){
System.out.println("if(lApplication)");
oMainAppFrame.SetPreSize(this);
}
}
public void start(){
System.out.println("start( );");
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
System.out.println("ScreenWidth="+gc.getBounds().getWidth());
System.out.println("ScreenHeight="+gc.getBounds().getHeight());
repaint();
}
public void stop(){
System.out.println("stop( );");
}
public void destroy(){
System.out.println("destroy( );");
}
class CanvasObj extends JPanel {
int x,y,d;
Color oColor;
@Override
public void paint(Graphics oGraphic){
System.out.println("paint( );");
Graphics2D oGraphic2D = (Graphics2D)oGraphic;
// ↑ここの Graphics の実態は Graphics2D なので、
// キャストしてやれば Graphics2D のメソッドが使える.
System.out.println("oApplet: "+
"width="+Integer.toString(getSize( ).width)+"; "+
"Height="+Integer.toString(getSize( ).height)+"; "+
"");
oGraphic2D.setBackground(Color.WHITE);
oGraphic2D.clearRect(0, 0, getWidth(), getHeight());
if( 0<d ){
oGraphic2D.setColor(oColor);
oGraphic2D.fillOval(x, y, d, d);
}
}
@Override
public void update(Graphics oGraphic){
System.out.println("updat( );");
// Java のバージョンによって違うかもしれないが、
// 動的なグラフィック表示をする場合は
// update メソッドが存在しないと表示のチラツキの原因になる場合がある.
paint(oGraphic);
}
}
class MainPanelImplementationObj extends MainPanelDesign {
public MainPanelImplementationObj( ) {
super( );
}
@Override
JPanel getJPanel0() {
if (jPanel0 == null) {
jPanel0 = new CanvasObj();
jPanel0.setLayout(new org.dyno.visual.swing.layouts.GroupLayout());
// ↑「org.dyno.visual.swing.layouts.GroupLayout」は Java 標準の Layout パッケージではない。
// 「MainPanelDesign.java」で import されているパッケージ名を使用している。
}
return jPanel0;
}
@Override
void jButton0MouseMouseReleased(MouseEvent event) {
System.out.println("MainPanelImplementationObj:MouseEvent:jButton0MouseMouseReleased.");
CanvasObj oCv = (CanvasObj)jPanel0;
oCv.oColor = Color.BLUE;
oCv.x = 5;
oCv.y = 5;
oCv.d = 80;
oCv.repaint();
}
@Override
void jButton1MouseMouseReleased(MouseEvent event) {
System.out.println("MainPanelImplementationObj:MouseEvent:jButton1MouseMouseReleased.");
CanvasObj oCv = (CanvasObj)jPanel0;
oCv.oColor = Color.YELLOW;
oCv.x = 95;
oCv.y = 5;
oCv.d = 80;
oCv.repaint();
}
@Override
void jButton2MouseMouseReleased(MouseEvent event) {
System.out.println("MainPanelImplementationObj:MouseEvent:jButton2MouseMouseReleased.");
CanvasObj oCv = (CanvasObj)jPanel0;
oCv.oColor = Color.RED;
oCv.x = 190;
oCv.y = 5;
oCv.d = 80;
oCv.repaint();
}
}
static class MainAppFrameObj extends JFrame implements Runnable {
// JApplet oApplet;
Thread oAppThread;
public MainAppFrameObj(String vsAppTitle) {
super(vsAppTitle);
//setVisible(false);
System.out.println("MainAppFrameObj:Construct:Application frame window.");
// oApplet = oEntApplet; // new JAppletcation();
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent event){
// ユーザーがウインドウを閉じようとした時].
Object oSource = event.getSource();
if (oSource == MainAppFrameObj.this){
System.out.println("MainAppFrameObj:WindowEvent:windowClosing.");
Close();
}
}
}
);
addWindowListener(
new WindowAdapter(){
public void windowClosed(WindowEvent event){
// ウインドウが閉じた時.
Object oSource = event.getSource();
if (oSource == MainAppFrameObj.this){
System.out.println("MainAppFrameObj:WindowEvent:windowClosed.");
}
}
}
);
addWindowListener(
new WindowAdapter(){
public void windowDeiconified(WindowEvent event){
// ウィンドウが最小化された状態から通常の状態に変更された時.
Object oSource = event.getSource();
if (oSource == MainAppFrameObj.this){
System.out.println("MainAppFrameObj:WindowEvent:windowDeiconified.");
oMainApp.repaint();
}
}
}
);
addComponentListener(
new ComponentAdapter(){
public void componentResized(ComponentEvent event){
// コンポーネントのサイズが変更された時.
Object oSource = event.getSource();
if (oSource == MainAppFrameObj.this){
System.out.println("MainAppFrameObj:ComponentEvent:componentResized.");
System.out.println("MainAppFrameObj: "+
"width="+Integer.toString(MainAppFrameObj.this.getSize( ).width)+"; "+
"Height="+Integer.toString(MainAppFrameObj.this.getSize( ).height)+"; "+
"");
oMainApp.repaint();
}
}
}
);
getContentPane().add(oMainApp,"Center");
oAppThread = new Thread(this);
}
public synchronized void SetPreSize(Component oComponent){
System.out.println("MainAppFrameObj:SetPreSize.");
// dispose();
System.out.println(
"oComponent.getWidth( )="+Integer.toString(oComponent.getWidth())+"; "+
"oComponent.getHeight( )="+Integer.toString(oComponent.getHeight())+"; "+
"oComponent.getSize( ).width="+Integer.toString(oComponent.getSize( ).width)+"; "+
"oComponent.getSize( ).height="+Integer.toString(oComponent.getSize( ).height)+"; "+
"oComponent.getMinimumSize( ).width="+Integer.toString(oComponent.getMinimumSize().width)+"; "+
"oComponent.getMinimumSize( ).height="+Integer.toString(oComponent.getMinimumSize().height)+"; "+
"");
getContentPane( ).setPreferredSize(
new Dimension(Math.max(oComponent.getSize( ).width, oComponent.getMinimumSize( ).width),
Math.max(oComponent.getSize( ).height, oComponent.getMinimumSize( ).height)));
pack( );
setVisible(true);
requestFocus( );
}
public synchronized void run(){
System.out.println("MainAppFrameObj:run.");
oMainApp.init();
oMainApp.start();
oMainApp.repaint();
try{
wait();
}
catch(InterruptedException e){
e.printStackTrace();
}
oMainApp.stop();
oMainApp.destroy();
oAppThread = null;
}
public synchronized void Close(){
System.out.println("MainAppFrameObj:Close:Application frame window.");
dispose();
notify();
}
}
}
『MainPanelDesign.java』
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;
//VS4E -- DO NOT REMOVE THIS LINE!
public class MainPanelDesign extends JPanel {
private static final long serialVersionUID = 1L;
private JButton jButton0;
private JButton jButton2;
private JButton jButton1;
JPanel jPanel0;
public MainPanelDesign() {
initComponents();
}
private void initComponents() {
setLayout(new GroupLayout());
add(getJButton0(), new Constraints(new Leading(5, 10, 10), new Leading(210, 10, 10)));
add(getJButton2(), new Constraints(new Leading(189, 12, 12), new Leading(210, 12, 12)));
add(getJButton1(), new Constraints(new Leading(96, 12, 12), new Leading(210, 12, 12)));
add(getJPanel0(), new Constraints(new Leading(5, 278, 10, 10), new Leading(5, 199, 12, 12)));
setSize(288, 248);
}
JPanel getJPanel0() {
if (jPanel0 == null) {
jPanel0 = new JPanel();
jPanel0.setLayout(new GroupLayout());
}
return jPanel0;
}
private JButton getJButton0() {
if (jButton0 == null) {
jButton0 = new JButton();
jButton0.setText("jButton0");
jButton0.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
jButton0MouseMouseReleased(event);
}
});
}
return jButton0;
}
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setText("jButton1");
jButton1.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
jButton1MouseMouseReleased(event);
}
});
}
return jButton1;
}
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setText("jButton2");
jButton2.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
jButton2MouseMouseReleased(event);
}
});
}
return jButton2;
}
void jButton2MouseMouseReleased(MouseEvent event) {
}
void jButton1MouseMouseReleased(MouseEvent event) {
}
void jButton0MouseMouseReleased(MouseEvent event) {
}
}
|
|
|