|
<Number>: [00000977]
<Date>: 2015/12/31 17:51:41
<Title>:
<Name>: amanojaku@管理人
|
|
|
『MainPanelDesign.java』
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.dyno.visual.swing.layouts.Bilateral;
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 cbtHostLogin;
private JButton cbtHostLogout;
private JButton cbtFTPUpload;
private JButton cbtFTPBreak;
private JButton cbtFTPDownload;
private JButton cbtSetupFileList_HostFolder;
private JButton cbtSetupFileList_LocalFolder;
private JButton cbtTransferList_Delete;
private JButton cbtTransferList_Entry;
private JButton cbtFTPSnapShot_Make;
private JButton cbtFTPSnapShot_Delete;
private JButton cbtFTPSynchronize;
JTextField ctfHostUserName;
JTextField ctfHostName;
JPasswordField cpfHostPassword;
JTextField ctfHostFolder;
JTextField ctfLocalFolder;
JTextField ctfHostPortNum;
JTextArea ctaMessage;
JList cltHostFileList;
JList cltLocalFileList;
JComboBox ccbFTPFileDataType;
JTabbedPane ctpFileListTabs;
JCheckBox cckHostPASVMode;
JComboBox ccbHostEncode;
JPanel cpnFTPSetting;
JTabbedPane ctpBaseTabs;
JList cltHostTransferList;
JList cltLocalTransferList;
JTabbedPane ctpTransferListTabs;
private JLabel jLabel2;
private JLabel jLabel6;
private JLabel jLabel5;
private JLabel jLabel0;
private JLabel jLabel1;
private JLabel jLabel7;
private JLabel jLabel9;
private JScrollPane jScrollPane0;
private JScrollPane jScrollPane1;
private JScrollPane jScrollPane2;
private JScrollPane jScrollPane3;
private JScrollPane jScrollPane4;
private JLabel jLabel3;
private JLabel jLabel4;
public MainPanelDesign() {
initComponents();
}
private void initComponents() {
setMinimumSize(new Dimension(479, 400));
setPreferredSize(new Dimension(479, 400));
setLayout(new GroupLayout());
add(getCbtHostLogin(), new Constraints(new Leading(10, 72, 75, 282), new Leading(254, 12, 12)));
add(getJLabel7(), new Constraints(new Leading(289, 12, 12), new Leading(195, 12, 12)));
add(getCtpBaseTabs(), new Constraints(new Leading(8, 271, 23, 23), new Leading(7, 237, 10, 10)));
add(getJLabel3(), new Constraints(new Leading(289, 12, 12), new Leading(15, 87, 177)));
add(getCtpTransferListTabs(), new Constraints(new Bilateral(289, 12, 57), new Leading(46, 139, 10, 10)));
add(getCbtHostLogout(), new Constraints(new Leading(94, 12, 12), new Leading(254, 12, 12)));
add(getCbtTransferList_Entry(), new Constraints(new Leading(368, 12, 12), new Leading(10, 12, 12)));
add(getCbtTransferList_Delete(), new Constraints(new Leading(443, 12, 12), new Leading(10, 12, 12)));
add(getCcbFTPFileDataType(), new Constraints(new Leading(135, 12, 12), new Leading(292, 12, 12)));
add(getJLabel9(), new Constraints(new Leading(10, 12, 12), new Leading(295, 18, 12, 12)));
add(getCbtSetupFileList_HostFolder(), new Constraints(new Leading(8, 12, 12), new Leading(330, 12, 12)));
add(getCtfHostFolder(), new Constraints(new Leading(135, 144, 12, 12), new Leading(333, 12, 12)));
add(getCbtSetupFileList_LocalFolder(), new Constraints(new Leading(8, 12, 12), new Leading(368, 12, 12)));
add(getCtfLocalFolder(), new Constraints(new Leading(136, 144, 12, 12), new Leading(371, 12, 12)));
add(getCbtFTPSynchronize(), new Constraints(new Leading(10, 12, 12), new Leading(445, 10, 10)));
add(getJLabel4(), new Constraints(new Leading(8, 12, 12), new Leading(411, 12, 12)));
add(getCbtFTPUpload(), new Constraints(new Leading(228, 12, 12), new Leading(445, 12, 12)));
add(getCbtFTPDownload(), new Constraints(new Leading(126, 12, 12), new Leading(445, 12, 12)));
add(getCbtFTPBreak(), new Constraints(new Leading(313, 12, 12), new Leading(445, 12, 12)));
add(getCtpFileListTabs(), new Constraints(new Leading(289, 266, 10, 10), new Leading(217, 220, 10, 10)));
add(getCbtFTPSnapShot_Delete(), new Constraints(new Leading(172, 12, 12), new Leading(406, 12, 12)));
add(getCbtFTPSnapShot_Make(), new Constraints(new Leading(94, 12, 12), new Leading(406, 12, 12)));
setSize(565, 486);
}
private JButton getCbtFTPSynchronize() {
if (cbtFTPSynchronize == null) {
cbtFTPSynchronize = new JButton();
cbtFTPSynchronize.setText("Synchronize");
cbtFTPSynchronize.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
FTPSynchronize_CbtMouseMouseReleased(event);
}
});
}
return cbtFTPSynchronize;
}
private JButton getJButton0() {
if (cbtFTPSynchronize == null) {
cbtFTPSynchronize = new JButton();
cbtFTPSynchronize.setText("Synchronize");
}
return cbtFTPSynchronize;
}
private JLabel getJLabel4() {
if (jLabel4 == null) {
jLabel4 = new JLabel();
jLabel4.setText("FTPSnapShot");
}
return jLabel4;
}
private JButton getCbtFTPSnapShot_Delete() {
if (cbtFTPSnapShot_Delete == null) {
cbtFTPSnapShot_Delete = new JButton();
cbtFTPSnapShot_Delete.setText("Delete");
cbtFTPSnapShot_Delete.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
FTPSnapShot_Delete_CbtMouseMouseReleased(event);
}
});
}
return cbtFTPSnapShot_Delete;
}
private JButton getCbtFTPSnapShot_Make() {
if (cbtFTPSnapShot_Make == null) {
cbtFTPSnapShot_Make = new JButton();
cbtFTPSnapShot_Make.setText("Make");
cbtFTPSnapShot_Make.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
FTPSnapShot_Makera_CbtMouseMouseReleased(event);
}
});
}
return cbtFTPSnapShot_Make;
}
private JLabel getJLabel3() {
if (jLabel3 == null) {
jLabel3 = new JLabel();
jLabel3.setText("TransferList");
}
return jLabel3;
}
private JScrollPane getJScrollPane4() {
if (jScrollPane4 == null) {
jScrollPane4 = new JScrollPane();
jScrollPane4.setViewportView(getCltLocalTransferList());
}
return jScrollPane4;
}
private JList getCltLocalTransferList() {
if (cltLocalTransferList == null) {
cltLocalTransferList = new JList();
DefaultListModel listModel = new DefaultListModel();
cltLocalTransferList.setModel(listModel);
}
return cltLocalTransferList;
}
private JScrollPane getJScrollPane3() {
if (jScrollPane3 == null) {
jScrollPane3 = new JScrollPane();
jScrollPane3.setViewportView(getCltHostTransferList());
}
return jScrollPane3;
}
private JList getCltHostTransferList() {
if (cltHostTransferList == null) {
cltHostTransferList = new JList();
DefaultListModel listModel = new DefaultListModel();
cltHostTransferList.setModel(listModel);
}
return cltHostTransferList;
}
private JButton getCbtTransferList_Entry() {
if (cbtTransferList_Entry == null) {
cbtTransferList_Entry = new JButton();
cbtTransferList_Entry.setText("Entry");
cbtTransferList_Entry.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
TransferList_Entry_CbtMouseMouseReleased(event);
}
});
}
return cbtTransferList_Entry;
}
private JButton getCbtTransferList_Delete() {
if (cbtTransferList_Delete == null) {
cbtTransferList_Delete = new JButton();
cbtTransferList_Delete.setText("Delete");
cbtTransferList_Delete.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
TransferList_Delete_CbtMouseMouseReleased(event);
}
});
}
return cbtTransferList_Delete;
}
private JTabbedPane getCtpTransferListTabs() {
if (ctpTransferListTabs == null) {
ctpTransferListTabs = new JTabbedPane();
ctpTransferListTabs.addTab("Host", getJScrollPane3());
ctpTransferListTabs.addTab("Local", getJScrollPane4());
}
return ctpTransferListTabs;
}
private JTabbedPane getCtpBaseTabs() {
if (ctpBaseTabs == null) {
ctpBaseTabs = new JTabbedPane();
ctpBaseTabs.addTab("FTP", getCpnFTPSetting());
ctpBaseTabs.addTab("Message", getJScrollPane0());
}
return ctpBaseTabs;
}
private JPanel getCpnFTPSetting() {
if (cpnFTPSetting == null) {
cpnFTPSetting = new JPanel();
cpnFTPSetting.setLayout(new GroupLayout());
cpnFTPSetting.add(getJLabel1(), new Constraints(new Leading(7, 12, 12), new Leading(42, 134, 134)));
cpnFTPSetting.add(getJLabel2(), new Constraints(new Leading(7, 12, 12), new Leading(176, 12, 12)));
cpnFTPSetting.add(getJLabel6(), new Constraints(new Leading(7, 12, 12), new Leading(140, 12, 12)));
cpnFTPSetting.add(getJLabel5(), new Constraints(new Leading(7, 12, 12), new Leading(74, 12, 12)));
cpnFTPSetting.add(getCckHostPASVMode(), new Constraints(new Leading(7, 12, 12), new Leading(8, 12, 12)));
cpnFTPSetting.add(getCtfHostName(), new Constraints(new Leading(96, 142, 10, 10), new Leading(72, 12, 12)));
cpnFTPSetting.add(getCpfHostPassword(), new Constraints(new Leading(97, 140, 12, 12), new Leading(172, 12, 12)));
cpnFTPSetting.add(getCtfHostPortNum(), new Constraints(new Leading(96, 142, 12, 12), new Leading(104, 12, 12)));
cpnFTPSetting.add(getJLabel0(), new Constraints(new Leading(7, 12, 12), new Leading(106, 12, 12)));
cpnFTPSetting.add(getCtfHostUserName(), new Constraints(new Leading(97, 142, 12, 12), new Leading(138, 12, 12)));
cpnFTPSetting.add(getCcbHostEncode(), new Constraints(new Leading(96, 12, 12), new Leading(38, 12, 12)));
}
return cpnFTPSetting;
}
private JButton getCbtFTPBreak() {
if (cbtFTPBreak == null) {
cbtFTPBreak = new JButton();
cbtFTPBreak.setText("Break");
cbtFTPBreak.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
FTPBreak_CbtMouseMouseReleased(event);
}
});
}
return cbtFTPBreak;
}
private JComboBox getCcbHostEncode() {
if (ccbHostEncode == null) {
ccbHostEncode = new JComboBox();
ccbHostEncode.setModel(new DefaultComboBoxModel(new Object[] { "UTF-8", "Windows", "EUC-JP", "ASCII" }));
ccbHostEncode.setDoubleBuffered(false);
ccbHostEncode.setBorder(null);
}
return ccbHostEncode;
}
private JButton getCbtSetupFileList_HostFolder() {
if (cbtSetupFileList_HostFolder == null) {
cbtSetupFileList_HostFolder = new JButton();
cbtSetupFileList_HostFolder.setText("HostFolder >");
cbtSetupFileList_HostFolder.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
SetupFileList_HostFolder_CbtMouseMouseReleased(event);
}
});
}
return cbtSetupFileList_HostFolder;
}
private JButton getCbtSetupFileList_LocalFolder() {
if (cbtSetupFileList_LocalFolder == null) {
cbtSetupFileList_LocalFolder = new JButton();
cbtSetupFileList_LocalFolder.setText("LocalFolder >");
cbtSetupFileList_LocalFolder.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
SetupFileList_LocalFolder_CbtMouseMouseReleased(event);
}
});
}
return cbtSetupFileList_LocalFolder;
}
private JButton getCbtFTPUpload() {
if (cbtFTPUpload == null) {
cbtFTPUpload = new JButton();
cbtFTPUpload.setText("Upload");
cbtFTPUpload.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
FTPUpload_CbtMouseMouseReleased(event);
}
});
}
return cbtFTPUpload;
}
private JComboBox getCcbFTPFileDataType() {
if (ccbFTPFileDataType == null) {
ccbFTPFileDataType = new JComboBox();
ccbFTPFileDataType.setModel(new DefaultComboBoxModel(new Object[] { "Binary", "ASCII" }));
ccbFTPFileDataType.setDoubleBuffered(false);
ccbFTPFileDataType.setBorder(null);
}
return ccbFTPFileDataType;
}
private JLabel getJLabel9() {
if (jLabel9 == null) {
jLabel9 = new JLabel();
jLabel9.setText("FileDtata");
}
return jLabel9;
}
private JCheckBox getCckHostPASVMode() {
if (cckHostPASVMode == null) {
cckHostPASVMode = new JCheckBox();
cckHostPASVMode.setSelected(true);
cckHostPASVMode.setText("PASVMode");
}
return cckHostPASVMode;
}
private JList getCltLocalFileList() {
if (cltLocalFileList == null) {
cltLocalFileList = new JList();
cltLocalFileList.setBackground(Color.white);
DefaultListModel listModel = new DefaultListModel();
cltLocalFileList.setModel(listModel);
cltLocalFileList.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
LocalFileList_CltNativeMouseMousePressed(event);
}
public void mouseReleased(MouseEvent event) {
LocalFileList_CltNativeMouseMouseReleased(event);
}
});
}
return cltLocalFileList;
}
private JLabel getJLabel7() {
if (jLabel7 == null) {
jLabel7 = new JLabel();
jLabel7.setText("FileList");
}
return jLabel7;
}
private JTabbedPane getCtpFileListTabs() {
if (ctpFileListTabs == null) {
ctpFileListTabs = new JTabbedPane();
ctpFileListTabs.addTab("Host", getJScrollPane1());
ctpFileListTabs.addTab("Local", getJScrollPane2());
}
return ctpFileListTabs;
}
private JScrollPane getJScrollPane2() {
if (jScrollPane2 == null) {
jScrollPane2 = new JScrollPane();
jScrollPane2.setBackground(Color.white);
jScrollPane2.setViewportView(getCltLocalFileList());
}
return jScrollPane2;
}
private JList getJList0() {
if (cltLocalFileList == null) {
cltLocalFileList = new JList();
DefaultListModel listModel = new DefaultListModel();
listModel.addElement("item0");
listModel.addElement("item1");
listModel.addElement("item2");
listModel.addElement("item3");
cltLocalFileList.setModel(listModel);
}
return cltLocalFileList;
}
private JLabel getJLabel1() {
if (jLabel1 == null) {
jLabel1 = new JLabel();
jLabel1.setText("HostEncode");
}
return jLabel1;
}
private JButton getCbtFTPDownload() {
if (cbtFTPDownload == null) {
cbtFTPDownload = new JButton();
cbtFTPDownload.setText("Download");
cbtFTPDownload.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
FTPDownload_CbtMouseMouseReleased(event);
}
});
}
return cbtFTPDownload;
}
private JScrollPane getJScrollPane1() {
if (jScrollPane1 == null) {
jScrollPane1 = new JScrollPane();
jScrollPane1.setEnabled(false);
jScrollPane1.setViewportView(getCltHostFileList());
}
return jScrollPane1;
}
private JList getCltHostFileList() {
if (cltHostFileList == null) {
cltHostFileList = new JList();
DefaultListModel listModel = new DefaultListModel();
cltHostFileList.setModel(listModel);
cltHostFileList.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
HostFileList_CltNativeMouseMousePressed(event);
}
public void mouseReleased(MouseEvent event) {
HostFileList_CltNativeMouseMouseReleased(event);
}
});
}
return cltHostFileList;
}
private JScrollPane getJScrollPane0() {
if (jScrollPane0 == null) {
jScrollPane0 = new JScrollPane();
jScrollPane0.setViewportView(getCtaMessage());
}
return jScrollPane0;
}
private JTextArea getCtaMessage() {
if (ctaMessage == null) {
ctaMessage = new JTextArea();
}
return ctaMessage;
}
private JTextField getCtfHostPortNum() {
if (ctfHostPortNum == null) {
ctfHostPortNum = new JTextField();
}
return ctfHostPortNum;
}
private JLabel getJLabel0() {
if (jLabel0 == null) {
jLabel0 = new JLabel();
jLabel0.setText("PortNo");
}
return jLabel0;
}
private JLabel getJLabel5() {
if (jLabel5 == null) {
jLabel5 = new JLabel();
jLabel5.setText("HostName");
}
return jLabel5;
}
private JLabel getJLabel6() {
if (jLabel6 == null) {
jLabel6 = new JLabel();
jLabel6.setText("UserName");
}
return jLabel6;
}
private JTextField getCtfLocalFolder() {
if (ctfLocalFolder == null) {
ctfLocalFolder = new JTextField();
ctfLocalFolder.setBackground(Color.white);
ctfLocalFolder.setText("");
}
return ctfLocalFolder;
}
private JButton getCbtHostLogin() {
if (cbtHostLogin == null) {
cbtHostLogin = new JButton();
cbtHostLogin.setText("Login");
cbtHostLogin.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
HostLogin_CbtMouseMouseReleased(event);
}
});
}
return cbtHostLogin;
}
private JTextField getCtfHostFolder() {
if (ctfHostFolder == null) {
ctfHostFolder = new JTextField();
ctfHostFolder.setBackground(Color.white);
}
return ctfHostFolder;
}
private JButton getCbtHostLogout() {
if (cbtHostLogout == null) {
cbtHostLogout = new JButton();
cbtHostLogout.setText("Logout");
cbtHostLogout.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent event) {
HostLogout_CbtMouseMouseReleased(event);
}
});
}
return cbtHostLogout;
}
private JLabel getJLabel2() {
if (jLabel2 == null) {
jLabel2 = new JLabel();
jLabel2.setText("Password");
}
return jLabel2;
}
private JTextField getCtfHostUserName() {
if (ctfHostUserName == null) {
ctfHostUserName = new JTextField();
}
return ctfHostUserName;
}
private JTextField getCtfHostName() {
if (ctfHostName == null) {
ctfHostName = new JTextField();
}
return ctfHostName;
}
private JPasswordField getCpfHostPassword() {
if (cpfHostPassword == null) {
cpfHostPassword = new JPasswordField();
cpfHostPassword.setEchoChar('*');
}
return cpfHostPassword;
}
void HostLogout_CbtMouseMouseReleased(MouseEvent event) {
}
void HostLogin_CbtMouseMouseReleased(MouseEvent event) {
}
void FTPDownload_CbtMouseMouseReleased(MouseEvent event) {
}
void FTPUpload_CbtMouseMouseReleased(MouseEvent event) {
}
void HostFileList_CltNativeMouseMousePressed(MouseEvent event) {
}
void HostFileList_CltNativeMouseMouseReleased(MouseEvent event) {
}
void LocalFileList_CltNativeMouseMousePressed(MouseEvent event) {
}
void LocalFileList_CltNativeMouseMouseReleased(MouseEvent event) {
}
void SetupFileList_HostFolder_CbtMouseMouseReleased(MouseEvent event) {
}
void SetupFileList_LocalFolder_CbtMouseMouseReleased(MouseEvent event) {
}
void FTPBreak_CbtMouseMouseReleased(MouseEvent event) {
}
void TransferList_Entry_CbtMouseMouseReleased(MouseEvent event) {
}
void TransferList_Delete_CbtMouseMouseReleased(MouseEvent event) {
}
void FTPSnapShot_Makera_CbtMouseMouseReleased(MouseEvent event) {
}
void FTPSnapShot_Delete_CbtMouseMouseReleased(MouseEvent event) {
}
void FTPSynchronize_CbtMouseMouseReleased(MouseEvent event) {
}
}
|
|
|
<Number>: [00000978]
<Date>: 2016/01/03 21:52:57
<Title>:
<Name>: amanojaku@管理人
|
|
|
|
|
<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) {
}
}
|
|
|