diff --git a/src/main/java/info/istlab/Zemi01/App.java b/src/main/java/info/istlab/Zemi01/App.java index c022a1c..f09e901 100644 --- a/src/main/java/info/istlab/Zemi01/App.java +++ b/src/main/java/info/istlab/Zemi01/App.java @@ -11,6 +11,12 @@ System.out.println( "info.istlab.Zemi01.App.main()が実行されました。" ); - RectangleFrame.main(null); + // 個別に、起動するクラスを指定する場合 + // RectangleFrame.main(null); + + // ランチャーを起動する場合 + String[] opts = {"RectangleFrame", "TransparentClock"}; + Launcher.show(opts); + } } diff --git a/src/main/java/info/istlab/Zemi01/Launcher.java b/src/main/java/info/istlab/Zemi01/Launcher.java new file mode 100644 index 0000000..164c28e --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/Launcher.java @@ -0,0 +1,72 @@ +package info.istlab.Zemi01; + +import java.awt.Dimension; +import java.awt.Point; +import java.awt.Toolkit; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; + +public class Launcher extends JPanel implements ActionListener { + String[] options; + + public static void show(String[] opts) { + JFrame jf = new JFrame("Launcher"); + jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + jf.getContentPane().add(new Launcher(opts)); + jf.pack(); // 内容物でサイズを自動設定する + + Dimension winSize = jf.getSize(); + jf.setLocation(centerOfScreen(winSize)); + jf.setVisible(true); + } + + // ここで、画面中心にウィンドウ表示するため、いろいろ計算する + public static Point centerOfScreen(Dimension winSize) { + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + return new Point((screenSize.width - winSize.width) / 2, (screenSize.height - winSize.height) / 2); + } + + public Launcher(String[] opts) { + options = opts; + + setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); + + for (String str : options) { + JButton b = new JButton(str); + b.addActionListener(this); + add(b); + } + } + + @Override + public void actionPerformed(ActionEvent e) { + String clsname = e.getActionCommand(); //押されたボタンのラベル文字列 + + // 無駄にリフレクションAPIをつかって、クラス名からmainメソッドを起動してみる + try { + // クラス名はパッケージ名を含めた名前が必要なので、このクラスのパッケージ名を取得する + String myClassName = Launcher.class.getPackageName(); + System.out.println("クラス "+myClassName + "." + clsname +" のmainメソッドを呼びます。"); + Class targetcls = Class.forName(myClassName + "." + clsname); + Class[] aarg = new Class[1]; + aarg[0] = String[].class; // 文字列の配列。探したいメソッドは main(String[] args)なので。 + + Method main = targetcls.getMethod("main", aarg); + String[] arg_for_invoke = new String[0]; + main.invoke(targetcls, new Object[] { arg_for_invoke }); + } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException + | IllegalArgumentException | InvocationTargetException e1) { + e1.printStackTrace(); + } + + } + +} diff --git a/src/main/java/info/istlab/Zemi01/RectangleFrame.java b/src/main/java/info/istlab/Zemi01/RectangleFrame.java index d99184f..7666367 100644 --- a/src/main/java/info/istlab/Zemi01/RectangleFrame.java +++ b/src/main/java/info/istlab/Zemi01/RectangleFrame.java @@ -30,7 +30,7 @@ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { - RectangleFrame rf = new RectangleFrame(100, 100, 400, 300); + RectangleFrame rf = new RectangleFrame(400, 300); rf.setVisible(true); JPopupMenu menu = new JPopupMenu(); @@ -49,6 +49,11 @@ } }); } + // ここで、画面中心にウィンドウ表示するため、いろいろ計算する + public static Point centerOfScreen(Dimension winSize) { + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + return new Point((screenSize.width - winSize.width) / 2, (screenSize.height - winSize.height) / 2); + } public void setVisible(boolean f) { for (int i = 0; i < 4; i++) { @@ -102,22 +107,22 @@ /** * コンストラクタ * - * @param x - * @param y * @param w * @param h */ - public RectangleFrame(int x, int y, int w, int h) { - location = new Point(x, y); + public RectangleFrame(int w, int h) { width = w; height = h; frameWidth = 10; + location = RectangleFrame.centerOfScreen(new Dimension(w, h)); + for (int i = 0; i < 4; i++) { rects[i] = new TransparentRect(this); // rects[i].setFocusableWindowState(true); } layout(); + System.out.println("つかいかた:ドラッグで枠移動、右下で枠をリサイズする、ダブルクリックで画面キャプチャ、右上でウィンドウを閉じる"); } public void registerPopupMenu(JPopupMenu pM) { diff --git a/src/main/java/info/istlab/Zemi01/TransparentClock.java b/src/main/java/info/istlab/Zemi01/TransparentClock.java new file mode 100644 index 0000000..9984b46 --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/TransparentClock.java @@ -0,0 +1,137 @@ +package info.istlab.Zemi01; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Point; +import java.awt.Toolkit; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; +import java.time.LocalDateTime; + +import javax.swing.JWindow; + +public class TransparentClock extends JWindow implements MouseListener, MouseMotionListener, Runnable { + + public static void main(String[] arg) { + new TransparentClock(); + } + + Thread thread; + + Point pressP; + Point pressWinP; // マウスプレスしたときの、RectangleFrameの位置 + + public TransparentClock() { + addMouseListener(this); + addMouseMotionListener(this); + setBackground(new Color(0, 0, 0, 30)); + Dimension dim = new Dimension(200, 100); + setSize(dim); + setLocation(centerOfScreen(dim)); + setVisible(true); + thread = new Thread(this); + thread.start(); + System.out.println("つかいかた:右上クリックで終了"); + } + + public void paint(Graphics g) { + Dimension dim = getSize(); + g.clearRect(0, 0, dim.width, dim.height); + // super.paint(g); + Graphics2D g2 = (Graphics2D) g; + g2.setColor(new Color(0, 255, 0, 50)); + + for(int i=0;i<4;i++){ + g2.drawRect(i,i, dim.width-i*2, dim.height-i*2); //x,y,w,h + } + + g2.setColor(new Color(0, 0, 255, 100)); + LocalDateTime ldt = LocalDateTime.now(); + String ldtstr = ldt.toString(); + String datestr = ldtstr.substring(0, 10); + String timestr = ldtstr.substring(11, 19); + Font font1 = new Font(Font.SANS_SERIF, Font.PLAIN, 22); + g2.setFont(font1); + g2.drawString(datestr, 30, 30); + g2.drawString(timestr, 30, 60); + } + + // ここで、画面中心にウィンドウ表示するため、いろいろ計算する + public static Point centerOfScreen(Dimension winSize) { + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + return new Point((screenSize.width - winSize.width) / 2, (screenSize.height - winSize.height) / 2); + } + + @Override + public void run() { + while (thread != null) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + repaint(); + } + + } + + @Override + public void mouseDragged(MouseEvent e) { + Point curP = e.getLocationOnScreen(); + int diffx = curP.x - pressP.x; + int diffy = curP.y - pressP.y; + setLocation(pressWinP.x + diffx, pressWinP.y + diffy); + } + + @Override + public void mouseMoved(MouseEvent e) { + Point p = e.getPoint(); + Point nep = new Point(getSize().width, 0); // ウィンドウの右上 + if (distance(p, nep) < 10) { + setBackground(new Color(255, 0, 0, 80)); + } else { + setBackground(new Color(255, 255, 255, 60)); + } + } + + @Override + public void mouseClicked(MouseEvent e) { + } + + @Override + public void mousePressed(MouseEvent e) { + Point p = e.getPoint(); + Point nep = new Point(getSize().width, 0); // ウィンドウの右上 + if (distance(p, nep) < 10) { + System.exit(0); + } + + // ドラッグ準備 + pressP = e.getLocationOnScreen(); + pressWinP = getLocationOnScreen(); + + } + + @Override + public void mouseReleased(MouseEvent e) { + } + + @Override + public void mouseEntered(MouseEvent e) { + setBackground(new Color(255, 255, 255, 100)); + } + + @Override + public void mouseExited(MouseEvent e) { + setBackground(new Color(255, 255, 255, 60)); + } + + public float distance(Point p1, Point p2) { + return (float) p1.distance(p2); + } + +}