|
<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) {
}
}
|
|
|