宿題スレPart53(Appletにする前にまず改良版) 875-876


宿題スレPart53(Appletにする前にまず改良版) >>875-876

	//
	// Ex8B_3ex.java (third source)
	//
	// EX875> javac Ex8B_3ex.java
	// EX875> java Ex8B_3ex
	//
	// camouflaged MVC pattern (View changes Model)
	//  Model       => GraphInfo()
	//  View        => GraphPanel(GraphInfo)
	//  Controller  => InfoPanel(GraphInfo, GraphPanel)
	//
	// original source
	// http://pc8.2ch.net/test/read.cgi/tech/1123639475/708-711
	// http://sourcepost.sytes.net/sourcepost/sourceview.aspx?source_id=26306
	//
	// second source
	// http://pc8.2ch.net/test/read.cgi/tech/1123639475/875-876
	// http://www.wikiroom.com/java/index.php?%A5%BD%A1%BC%A5%B9%C5%BD%C9%D5%A4%B1
	//
	// 上記二つのソースはラジアン系への変換、位相シフト、座標変換が出鱈目なので修正
	//
	
	import java.awt.BorderLayout;
	import java.awt.GridLayout;
	import java.awt.Container;
	import java.awt.Color;
	import java.awt.Dimension;
	import java.awt.Font;
	import javax.swing.JRadioButton;
	import java.awt.Graphics;
	import java.awt.FontMetrics;
	import javax.swing.BoxLayout;
	import javax.swing.JPanel;
	import javax.swing.JFrame;
	import javax.swing.JTextField;
	import javax.swing.JLabel;
	import javax.swing.ButtonGroup;
	import javax.swing.JMenuBar;
	import javax.swing.JMenu;
	import javax.swing.JMenuItem;
	import javax.swing.JComponent;
	import javax.swing.KeyStroke;
	import javax.swing.JOptionPane;
	import javax.swing.JButton;
	import javax.swing.ButtonGroup;
	import javax.swing.Timer;
	import java.awt.event.ActionEvent;
	import java.awt.event.ActionListener;
	import java.awt.event.MouseMotionAdapter;
	import java.awt.event.MouseAdapter;
	import java.awt.event.MouseEvent;
	import java.awt.event.WindowEvent;
	import java.awt.event.WindowAdapter;
	import java.awt.event.InputEvent;
	import java.awt.event.KeyEvent;
	import java.awt.event.MouseMotionAdapter;
	
		class Ex8B_3ex extends JFrame{
			Ex8B_3ex(){
				Ex7B_1 ex = new Ex7B_1();
				MenuBar menubar = new MenuBar();
				setJMenuBar(menubar);
				Container pane = getContentPane();
				pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
				pane.add(ex);
				pack();
				setVisible(true);
			}
		
		public static void main(String[] args){
			Ex8B_3ex frame = new Ex8B_3ex();
			frame.addWindowListener(
				new WindowAdapter(){
					public void windowClosing(WindowEvent e){
						System.exit(0);
					}
				}
			);
			frame.setTitle("Ex8B_3ex");
		}
	}
	
	class Ex7B_1 extends JPanel{
		Ex7B_1(){
			Graphinfo gi = new Graphinfo();
			JPanel panel = new JPanel();
			JLabel position = new JLabel();
			JLabel status = new JLabel();
			panel.add(position);
			panel.add(status);
			GraphPanel gp = new GraphPanel(gi, position, status);
			InfoPanel info = new InfoPanel(gi, gp);
			setBackground(Color.white);
			setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
			add(info);
			add(gp);
			add(panel);
		}
	}
	
	class MenuBar extends JMenuBar{
		Listener_Command listener_Command;
		final String Menu_File = "MF";
		final String Menu_Help = "MH";
	
		MenuBar(){
			listener_Command = new Listener_Command();
	
			JMenu menu1 = new JMenu("ファイル");
			JMenuItem item1 = new JMenuItem("終了(Ctrl+Shift+x)");
			item1.setActionCommand(Menu_File);
			item1.addActionListener(listener_Command);
			item1.setAccelerator(KeyStroke.getKeyStroke("ctrl shift X"));
			add(menu1);
			menu1.add(item1);
	
			JMenu menu2 = new JMenu("ヘルプ");
			JMenuItem item2 = new JMenuItem("バージョン情報(Ctrl+Shift+v)");
			item2.setActionCommand(Menu_Help);
			item2.addActionListener(listener_Command);
			item2.setAccelerator(KeyStroke.getKeyStroke("ctrl shift V"));
			add(menu2);
			menu2.add(item2);
		}
	
		class Listener_Command implements ActionListener{
			public void actionPerformed(ActionEvent e){
				String str = e.getActionCommand();
				if(str.compareTo(Menu_File) == 0){
					System.exit(0);
				}else if(str.compareTo(Menu_Help) == 0){
					JOptionPane.showMessageDialog(
						null,
						"Ex8B_3ex?n" +
					"Produced by ★★ Java の宿題ここで答えます Part 52 ★★?n" +
						"version 1.3.0",
						"Version情報",
						JOptionPane.INFORMATION_MESSAGE
					);
				}
			}
		}
	}
	
	class Graphinfo{
		private int RD = 1;
		private float val[] = {120, 1, 0, 0, 360, 1, 1, 1};
	
		int getRD() { return RD; }
		void setRD(int rd){ RD = rd; }
		float getval(int n){ return val[n]; }
		void setval(int n, float f){ val[n] = f; }
	}
	
	class InfoPanel extends JPanel{
		Graphinfo gi;
		GraphPanel gp;
		Listener_PanelCtrl listener_PanelCtrl;
		JRadioButton rd1, rd2;
		ParamField[] param = new ParamField[8];
		JButton b1, b2;
		Listener_Timer listener_Timer;
		Timer timer;
	
		InfoPanel(Graphinfo g1, GraphPanel g2){
			gi = g1;
			gp = g2;
			setLayout(new GridLayout(5,4));
			listener_PanelCtrl = new Listener_PanelCtrl();
			listener_Timer = new Listener_Timer();
			timer = new Timer((int)gi.getval(7), listener_Timer);   // Delay
			timer.start();
	
			JLabel labelsc = new JLabel("関数:");
			rd1 = new JRadioButton("sin", gi.getRD() == 1);
			rd1.addActionListener(listener_PanelCtrl);
			rd2 = new JRadioButton("cos", gi.getRD() == 2);
			rd2.addActionListener(listener_PanelCtrl);
			ButtonGroup groupsc = new ButtonGroup();
			groupsc.add(rd1);
			groupsc.add(rd2);
			JPanel panelsc = new JPanel();
			panelsc.add(rd1);
			panelsc.add(rd2);
			add(labelsc);
			add(panelsc);
	
			param[0] = new ParamField(gi, 0, null, 0, 0,
				listener_PanelCtrl, "分割数:", 10);
			param[1] = new ParamField(gi, 1,
				"周期0は禁止です。勝手ながら360にさせて頂きます。", 0, 360,
				listener_PanelCtrl, "周期:", 10);
			param[2] = new ParamField(gi, 2, null, 0, 0,
				listener_PanelCtrl, "初期位相:", 10);
			param[3] = new ParamField(gi, 3, null, 0, 0,
				listener_PanelCtrl, "定義域xの最小値:", 10);
			param[4] = new ParamField(gi, 4, null, 0, 0,
				listener_PanelCtrl, "xの最大値:", 10);
			param[5] = new ParamField(gi, 5, null, 0, 0,
				listener_PanelCtrl, "速度:", 10);
			param[6] = new ParamField(gi, 6, null, 0, 0,
				listener_PanelCtrl, "時間刻み:", 10);
			param[7] = new ParamField(gi, 7, null, 0, 0,
				listener_PanelCtrl, "時間ステップ:", 10);
	
			b1 = new JButton("スタート");
			b1.addActionListener(listener_PanelCtrl);
			b2 = new JButton("ストップ");
			b2.addActionListener(listener_PanelCtrl);
			add(b1);
			add(b2);
		}
	
		class ParamField{
			Graphinfo gi;
			int num;
			String str;
			float fchk, fval;
			ActionListener listener;
			JLabel label;
			JTextField txt;
	
			ParamField(Graphinfo g, int n, String s, float fc, float fv,
				ActionListener l, String title, int len){
				gi = g;
				num = n;
				str = s;
				fchk = fc;
				fval = fv;
				listener = l;
				label = new JLabel(title);
				txt = new JTextField(len);
				txt.addActionListener(listener);
				txt.setText(Float.toString(gi.getval(num)));
				add(label);
				add(txt);
			}
	
			void setGIParam(float f){
				txt.setText(Float.toString(f));
			}
	
			void setParam(){
				try{
					float f = Float.valueOf(txt.getText()).floatValue();
					if((str != null) && (f == fchk)){
						JOptionPane.showMessageDialog(
							null,
							str,
							"Error!",
							JOptionPane.ERROR_MESSAGE
						);
						f = fval;
						txt.setText(Float.toString(f));
					}
					gi.setval(num, f);
				}catch(NumberFormatException e){}
			}
		}
	
		class Listener_PanelCtrl implements ActionListener{
			public void actionPerformed(ActionEvent e){
				if(e.getSource() == rd1){           // sin
					gi.setRD(1);
				}else if(e.getSource() == rd2){     // cos
					gi.setRD(2);
				}
	
				for(int i = 0; i <= 7; i++){        // parameter
					param[i].setParam();
				}
	
				if(e.getSource() == b1){            // start button
					gp.timer.setDelay((int)(gi.getval(7)));         // Delay
					gp.timer.start();               // start animation
				}else if(e.getSource() == b2){      // stop button
					gp.timer.stop();                // stop animation
				}
	
				gp.repaint();
			}
		}
	
		class Listener_Timer implements ActionListener{
			public void actionPerformed(ActionEvent e){
				for(int i = 0; i <= 7; i++){
					param[i].setGIParam(gi.getval(i));
				}
			}
		}
	}
	
	class GraphPanel extends JPanel{
		Graphinfo gi;
		JLabel position;
		JLabel status;
		Listener_MouseMotion listener_MouseMotion;
		Listener_MouseClick listener_MouseClick;
		Listener_Timer listener_Timer;
		Timer timer;
		float t = 0;
	
		GraphPanel(Graphinfo g, JLabel p, JLabel s){
			gi = g;
			position = p;
			status = s;
			listener_MouseMotion = new Listener_MouseMotion();
			listener_MouseClick = new Listener_MouseClick();
			listener_Timer = new Listener_Timer();
			timer = new Timer((int)gi.getval(7), listener_Timer);   // Delay
			setBackground(Color.white);
			setMinimumSize(new Dimension(240, 120));
			setPreferredSize(new Dimension(320, 240));
			addMouseListener(listener_MouseClick);
			addMouseMotionListener(listener_MouseMotion);
		}
	
		public void paintComponent(Graphics g){
			super.paintComponent(g);
			Dimension d = getSize();
			float nodes = gi.getval(0);
			float freq = gi.getval(1);
			float phase = gi.getval(2);
			float min = gi.getval(3);
			float max = gi.getval(4);
			float speed = gi.getval(5);
			double x = 0.0, y = 0.0;
			int px = 0, py = 0;
	
			for(int i =0; i <= (int)nodes; i++){
				x = ((max - min) * i / nodes) + (min - phase - speed * t);
				x = (x * freq * Math.PI) / 180;     // convert to radian
				if(gi.getRD() == 1){                                    // sin
					y = Math.sin(x);
				}else if(gi.getRD() == 2){                              // cos
					y = Math.cos(x);
				}
	
				// convert to screen points
				x = 20 + (d.width - 40) * i / nodes;
				y = 20 + (y + 1) * (d.height - 40) / 2;
				if(i != 0){
					g.drawLine(px, py, (int)x, (int)y);
				}
				px = (int)x;
				py = (int)y;
			}
	
			g.drawRect(20, 20, (d.width - 40), (d.height - 40));        // frame
			g.drawLine(20, d.height / 2, d.width - 20, d.height / 2);   // y = 0
			g.drawLine(d.width / 2, d.height / 2 - 10,
				d.width / 2, d.height / 2 + 10);                        // scale
	
			g.setFont(new Font("TimesRoman", Font.ITALIC, 10));
			FontMetrics metrics = g.getFontMetrics();
			int ascent = metrics.getAscent();
			String string1 = "1.0";
			int width1 = metrics.stringWidth(string1);
			g.drawString(string1, 0, 20);
			String string2 = Float.toString(min);
			int width2 = metrics.stringWidth(string2);
			g.drawString(string2, 0, d.height / 2 + ascent);
			String string3 = "-1.0";
			int width3 = metrics.stringWidth(string3);
			g.drawString(string3, 0, d.height - 20 + ascent);
			String string4 = Float.toString(max);
			int width4 = metrics.stringWidth(string4);
			g.drawString(string4,
				d.width - string4.length() * 8, d.height / 2 + ascent);
	
			float smp = nodes / 2;
			float cyc = freq * (max - min) / 360;
			Color fgc = Color.blue;
			String gl = ">";
			if(smp <= cyc){
				fgc = Color.red;
				gl = "<=";
			}
			status.setForeground(fgc);
			status.setText("サンプル数 / 2 (" + smp + ") " +
				gl + "(" + cyc + ") 波数");
		}
	
		class Listener_MouseMotion extends MouseMotionAdapter{
			public void mouseMoved(MouseEvent e){
				Dimension d = getSize();
				float x = e.getX();
				float y = e.getY();
				float min = gi.getval(3);
				float max = gi.getval(4);
				float X1 = (x - 20) * (max - min) / (d.width - 40) + min;
				float Y1 = (d.height / 2 - y) / ((d.height - 40) / 2);
				position.setText(X1 + ", " + Y1);
			}
		}
	
		class Listener_MouseClick extends MouseAdapter{
			public void mouseClicked(MouseEvent e){
				float min = gi.getval(3);
				float max = gi.getval(4);
				if(javax.swing.SwingUtilities.isLeftMouseButton(e)){
					gi.setval(4, (max - min) / 2 + min);
					repaint();
				}else if(javax.swing.SwingUtilities.isRightMouseButton(e)){
					gi.setval(4, (max - min) * 2 + min);
					repaint();
				}
			}
		}
	
		class Listener_Timer implements ActionListener{
			public void actionPerformed(ActionEvent e){
				t += gi.getval(6);
				repaint();
			}
		}
	}

CONTENTS

最新の20件

2020-11-14 2005-12-06 2006-11-04 2012-07-15 2009-06-19 2011-03-03 2006-12-13 2007-11-05 2014-07-22 2014-07-19 2014-07-09 2014-01-14 2012-09-03 2012-03-28

今日の20件

人気の30件

  • counter: 1413
  • today: 1
  • yesterday: 0
  • online: 1