You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

453 lines
14 KiB

  1. package verteiltesysteme.mandelbrot.rmi;
  2. import java.awt.BorderLayout;
  3. //Mandelbot Example A. Gepperth
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.MouseEvent;
  9. import java.awt.event.MouseListener;
  10. import java.awt.event.MouseMotionListener;
  11. import java.awt.image.BufferedImage;
  12. import java.rmi.NotBoundException;
  13. import java.rmi.RemoteException;
  14. import java.rmi.registry.LocateRegistry;
  15. import java.rmi.registry.Registry;
  16. import javax.swing.ImageIcon;
  17. import javax.swing.JButton;
  18. import javax.swing.JFrame;
  19. import javax.swing.JLabel;
  20. import javax.swing.JPanel;
  21. import javax.swing.JSlider;
  22. import javax.swing.JTextField;
  23. import javax.swing.border.BevelBorder;
  24. import javax.swing.border.EmptyBorder;
  25. import javax.swing.event.ChangeEvent;
  26. import javax.swing.event.ChangeListener;
  27. class MyJLabel extends JLabel {
  28. /**
  29. *
  30. */
  31. private static final long serialVersionUID = -7750935382747799343L;
  32. int boxULx, boxULy, boxW, boxH;
  33. MyJLabel() {
  34. super();
  35. }
  36. MyJLabel(String s) {
  37. super(s);
  38. }
  39. void setBox(int boxULx, int boxULy, int boxW, int boxH) {
  40. this.boxULx = boxULx;
  41. this.boxULy = boxULy;
  42. this.boxW = boxW;
  43. this.boxH = boxH;
  44. }
  45. @Override
  46. protected void paintComponent(Graphics g) {
  47. super.paintComponent(g);
  48. g.setColor(Color.black);
  49. g.drawRect(this.boxULx, this.boxULy, this.boxW, this.boxH);
  50. }
  51. }
  52. class RMIMandelbrotCalculationThread extends Thread {
  53. MyJLabel view;
  54. RMIMandelbrotCalculationsInterface remoteCalcObj;
  55. int boxULx, boxULy, boxW, boxH;
  56. public static int RESX = 600;
  57. public static int RESY = 600;
  58. Boolean upperPart;
  59. int maxIterations;
  60. RMIMandelbrotCalculationThread(Boolean upperPart, MyJLabel view, RMIMandelbrotCalculationsInterface remoteCalcObj, int maxIterations) {
  61. this.view = view;
  62. this.remoteCalcObj = remoteCalcObj;
  63. this.upperPart = upperPart;
  64. this.maxIterations = maxIterations;
  65. }
  66. // visualization related stuff, do not touch!
  67. int[] generateLookupTable(int nrHues) {
  68. int[] tmp = new int[nrHues + 1];
  69. int cycle = 2048;
  70. for (int i = 0; i < nrHues; i++) {
  71. float hue = 1 * ((float) (i % cycle)) / cycle/* ((float)nrHues) */ ;
  72. tmp[i] = Color.HSBtoRGB(hue, 0.8f, 1.0f);
  73. }
  74. tmp[0] = Color.HSBtoRGB(0f, 1.0f, 0.0f);
  75. return tmp;
  76. }
  77. public void run() {
  78. try {
  79. this.view.setIcon(new ImageIcon());
  80. long timestampStart = System.currentTimeMillis();
  81. //double ULx = -0.16099999999999995, ULy = -0.9365333333333333, LRx = -0.03533333333333327, LRy = -0.8108666666666666;
  82. double ULx = -2;
  83. double ULy = 2;
  84. double LRx = 2;
  85. double LRy = -2;
  86. //double ULx = this.remoteCalcObj.getULx();
  87. //double ULy = this.remoteCalcObj.getULy();
  88. //double LRx = this.remoteCalcObj.getLRx();
  89. //double LRy = this.remoteCalcObj.getLRy();
  90. System.out.println("actual="+ULx + "/" + ULy + "/" + LRx + "/" + LRy);
  91. double w = LRx - ULx;
  92. double h = LRy - ULy;
  93. // AG change define box such that it contains only upper half of apple man :-)
  94. if (this.upperPart == true)
  95. {
  96. boxULx = 0 ; boxULy = 0; boxW = MGuiRMI.RESX ; boxH = MGuiRMI.RESY/2;
  97. }
  98. else
  99. {
  100. // for lower half:
  101. boxULx = 0 ; boxULy = RESY/2; boxW = RESX ; boxH = RESY/2;
  102. }
  103. ULx = ULx + w * this.boxULx / (double) MGuiRMI.RESX;
  104. ULy = ULy + h * this.boxULy / (double) MGuiRMI.RESY;
  105. w *= this.boxW / (double) (RESX);
  106. h *= this.boxH / (double) (RESY);
  107. LRx = ULx + w;
  108. LRy = ULy + h;
  109. System.out.println("computed="+ULx + "/" + ULy + "/" + LRx + "/" + LRy);
  110. this.boxULx = 0;
  111. this.boxULy = 0;
  112. this.boxW = MGuiRMI.RESX;
  113. this.boxH = MGuiRMI.RESY;
  114. //this.remoteCalcObj.setView(ULx, ULy, LRx, LRy);
  115. // end alex
  116. int[] colors = null;
  117. this.remoteCalcObj.iterateAllPoints(this.maxIterations * 1000, MGuiRMI.RESX, MGuiRMI.RESY/2, ULx, ULy, LRx, LRy);
  118. /*
  119. int[] lookupTable = generateLookupTable(this.maxIterations.getValue() * 1000);
  120. colors = new int[MGuiRMI.RESX * MGuiRMI.RESY];
  121. MGuiRMI.applyLookupTable(remoteCalcObj.getResult(), colors, lookupTable);
  122. BufferedImage colorImage = new BufferedImage(MGuiRMI.RESX, MGuiRMI.RESY, BufferedImage.TYPE_INT_ARGB);
  123. colorImage.setRGB(0, 0, MGuiRMI.RESX, MGuiRMI.RESY, colors, 0, MGuiRMI.RESX);
  124. this.view.setIcon(new ImageIcon(colorImage));
  125. this.view.setBox(this.boxULx, this.boxULy, this.boxW, this.boxH);
  126. this.view.repaint();
  127. long timestampEnd = System.currentTimeMillis();
  128. String timeElapsed = "Dauer: " + ((timestampEnd - timestampStart) / 1000.0) + " sec";
  129. System.out.println(timeElapsed);
  130. this.message.setText(timeElapsed);
  131. * */
  132. int[] lookupTable = generateLookupTable(this.maxIterations * 1000);
  133. colors = new int[MGuiRMI.RESX * MGuiRMI.RESY/2];
  134. MGuiRMI.applyLookupTable(remoteCalcObj.getResult(), colors, lookupTable);
  135. BufferedImage colorImage = new BufferedImage(MGuiRMI.RESX, MGuiRMI.RESY/2, BufferedImage.TYPE_INT_ARGB);
  136. colorImage.setRGB(0, 0, MGuiRMI.RESX, MGuiRMI.RESY/2, colors, 0, MGuiRMI.RESX);
  137. this.view.setIcon(new ImageIcon(colorImage));
  138. long timestampEnd = System.currentTimeMillis();
  139. String timeElapsed = "Zeit: " + ((timestampEnd - timestampStart) / 1000.0) + " sec";
  140. System.out.println(timeElapsed);
  141. //this.view.setText(timeElapsed);
  142. } catch (RemoteException e) {
  143. // TODO Auto-generated catch block
  144. e.printStackTrace();
  145. }
  146. }
  147. }
  148. public class MGuiRMI implements ActionListener, ChangeListener, MouseListener, MouseMotionListener {
  149. // GUI elements
  150. JFrame frame;
  151. JPanel panel;
  152. JButton endButton;
  153. JButton backButton;
  154. JButton calcButton;
  155. JTextField host1TextField;
  156. JTextField host2TextField;
  157. JSlider maxIterations;
  158. MyJLabel viewUpper;
  159. MyJLabel viewLower;
  160. JLabel message;
  161. int nrIterations;
  162. // box management
  163. int boxULx, boxULy, boxW, boxH;
  164. boolean dragging = false;
  165. // this one does all the calculating work
  166. RMIMandelbrotCalculationsInterface remoteCalcObj;
  167. RMIMandelbrotCalculationsInterface remoteCalcObj2;
  168. // how many points in the image?
  169. // change if you want a higher resolution
  170. public static int RESX = 600;
  171. public static int RESY = 600;
  172. MGuiRMI() {
  173. // set up GUI
  174. this.frame = new JFrame("Mandelbrotmenge");
  175. this.panel = new JPanel();
  176. this.panel.setBorder(new EmptyBorder(0,0,0,0));
  177. this.endButton = new JButton("Ende");
  178. this.panel.add(this.endButton);
  179. this.backButton = new JButton("Zur�ck");
  180. this.panel.add(this.backButton);
  181. this.calcButton = new JButton("Rechnen");
  182. this.host1TextField = new JTextField(10);
  183. this.host1TextField.setText("localhost");
  184. this.host2TextField = new JTextField(10);
  185. this.host2TextField.setText("localhost");
  186. this.panel.add(this.calcButton);
  187. this.panel.add(this.host1TextField);
  188. this.panel.add(this.host2TextField);
  189. this.frame.add(this.panel);
  190. this.frame.setSize(700, 720);
  191. this.frame.setVisible(true);
  192. this.viewUpper = new MyJLabel();
  193. this.viewLower = new MyJLabel();
  194. this.maxIterations = new JSlider(0, 100, 30);
  195. this.panel.add(this.maxIterations);
  196. this.panel.add(this.viewUpper);
  197. this.panel.add(this.viewLower);
  198. this.message = new JLabel("Status");
  199. this.message.setBorder(new BevelBorder(BevelBorder.LOWERED));
  200. this.frame.add(this.message,BorderLayout.SOUTH);
  201. this.endButton.addActionListener(this);
  202. this.backButton.addActionListener(this);
  203. this.calcButton.addActionListener(this);
  204. this.maxIterations.addChangeListener(this);
  205. this.maxIterations.setMajorTickSpacing(10);
  206. this.maxIterations.setPaintTicks(true);
  207. this.maxIterations.setPaintLabels(true);
  208. this.viewUpper.addMouseListener(this);
  209. this.viewUpper.addMouseMotionListener(this);
  210. this.viewLower.addMouseListener(this);
  211. this.viewLower.addMouseMotionListener(this);
  212. this.boxULx = 0;
  213. this.boxULy = 0;
  214. this.boxW = MGuiRMI.RESX;
  215. this.boxH = MGuiRMI.RESY;
  216. this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  217. // Non-Gui stuff
  218. this.nrIterations = 1000;
  219. }
  220. // visualization
  221. static void applyLookupTable(int[] data, int[] dest, int[] lookupTable) {
  222. for (int i = 0; i < data.length; i++) {
  223. dest[i] = lookupTable[data[i]];
  224. }
  225. }
  226. // here we check what the buttons do
  227. public void actionPerformed(ActionEvent ae) {
  228. if (ae.getSource() == this.endButton) {
  229. System.out.println("Ende");
  230. System.exit(0);
  231. } else
  232. if (ae.getSource() == this.backButton) {
  233. System.out.println("Zurueck");
  234. // Zoom und Ausschnitt zur�cksetzen
  235. this.boxULx = 0;
  236. this.boxULy = 0;
  237. this.boxW = MGuiRMI.RESX;
  238. this.boxH = MGuiRMI.RESY;
  239. this.viewUpper.setBox(this.boxULx, this.boxULy, this.boxW, this.boxH);
  240. this.viewUpper.repaint();
  241. this.viewLower.setBox(this.boxULx, this.boxULy, this.boxW, this.boxH);
  242. this.viewLower.repaint();
  243. if (this.remoteCalcObj == null)
  244. {
  245. String message = "Fehler: noch nicht verbunden, noch keine Berechnung durchgef�hrt";
  246. this.message.setText(message);
  247. System.out.println(message);
  248. }
  249. else
  250. {
  251. /*
  252. try {
  253. this.remoteCalcObj.setView(ULx, ULy, LRx, LRy);
  254. } catch (RemoteException e) {
  255. // TODO Auto-generated catch block
  256. e.printStackTrace();
  257. }
  258. */
  259. this.message.setText("Ausschnitt zur�ckgesetzt");
  260. }
  261. if (this.remoteCalcObj2 == null)
  262. {
  263. String message = "Fehler: noch nicht verbunden, noch keine Berechnung durchgef�hrt";
  264. this.message.setText(message);
  265. System.out.println(message);
  266. }
  267. else
  268. {
  269. /*
  270. try {
  271. this.remoteCalcObj2.setView(ULx, ULy, LRx, LRy);
  272. } catch (RemoteException e) {
  273. // TODO Auto-generated catch block
  274. e.printStackTrace();
  275. }
  276. */
  277. this.message.setText("Ausschnitt zur�ckgesetzt");
  278. }
  279. } else
  280. if (ae.getSource() == this.calcButton) {
  281. String hostname1 = this.host1TextField.getText();
  282. String hostname2 = this.host2TextField.getText();
  283. System.out.println("Verbinde mit RMI Registry auf Host: " + hostname1);
  284. try {
  285. //double ULx = -0.16099999999999995, ULy = -0.9365333333333333, LRx = -0.03533333333333327, LRy = -0.8108666666666666;
  286. //double ULx = -2, ULy = 2, LRx = 2, LRy = -2;
  287. Registry registry = LocateRegistry.getRegistry(hostname1);
  288. //Registry registry = LocateRegistry.createRegistry(1099);
  289. this.remoteCalcObj = (RMIMandelbrotCalculationsInterface) registry.lookup("RMIMandelbrotCalculationsInterface");
  290. //this.remoteCalcObj.setView(ULx, ULy, LRx, LRy);
  291. Registry registry2 = LocateRegistry.getRegistry(hostname2);
  292. this.remoteCalcObj2 = (RMIMandelbrotCalculationsInterface) registry2.lookup("RMIMandelbrotCalculationsInterface");
  293. //this.remoteCalcObj2.setView(ULx, ULy, LRx, LRy);
  294. /** CODE SEBASTIAN
  295. System.out.println("Rechnen");
  296. long timestampStart = System.currentTimeMillis();
  297. //this.boxW = this.boxW / 2;
  298. //MGuiRMI.RESX = MGuiRMI.RESX / 2;
  299. double ULx = 0;
  300. double ULy = 0;
  301. double LRx = 0;
  302. double LRy = 0;
  303. ULx = this.remoteCalcObj.getULx();
  304. ULy = this.remoteCalcObj.getULy();
  305. LRx = this.remoteCalcObj.getLRx();
  306. LRy = this.remoteCalcObj.getLRy();
  307. double w = LRx - ULx;
  308. double h = LRy - ULy;
  309. ULx = ULx + w * this.boxULx / (double) MGuiRMI.RESX;
  310. ULy = ULy + h * this.boxULy / (double) MGuiRMI.RESY;
  311. w *= this.boxW / (double) (RESX);
  312. h *= this.boxH / (double) (RESX);
  313. LRx = ULx + w;
  314. LRy = ULy + h;
  315. System.out.println(ULx + "/" + ULy + "/" + LRx + "/" + LRy);
  316. this.boxULx = 0;
  317. this.boxULy = 0;
  318. this.boxW = MGuiRMI.RESX;
  319. this.boxH = MGuiRMI.RESY;
  320. int[] colors = null;
  321. //this.remoteCalcObj.setView(ULx, ULy, LRx, LRy);
  322. this.remoteCalcObj.setView(ULx, ULy, LRx, LRy);
  323. end */
  324. /* Code Alex */
  325. System.out.println("Rechnen");
  326. this.message.setText("Rechnen");
  327. RMIMandelbrotCalculationThread rmimct = new RMIMandelbrotCalculationThread(true, this.viewUpper, this.remoteCalcObj, maxIterations.getValue());
  328. new Thread(rmimct).start();
  329. RMIMandelbrotCalculationThread rmimct2 = new RMIMandelbrotCalculationThread(false, this.viewLower, this.remoteCalcObj2, maxIterations.getValue());
  330. new Thread(rmimct2).start();
  331. } catch (RemoteException | NotBoundException e) {
  332. // TODO Auto-generated catch block
  333. e.printStackTrace();
  334. }
  335. }
  336. }
  337. // Zoom not working for lower...
  338. public void mousePressed(MouseEvent e) {
  339. this.boxULx = e.getX();
  340. this.boxULy = e.getY();
  341. this.boxW = 1;
  342. this.boxH = 1;
  343. this.viewUpper.setBox(this.boxULx, this.boxULy, this.boxW, this.boxH);
  344. this.message.setText("boxULx: " + this.boxULx + " boxULy: " + this.boxULy + " boxW: " + this.boxW + " boxH: " + this.boxH);
  345. }
  346. public void mouseDragged(MouseEvent e) {
  347. this.boxW = e.getX() - this.boxULx + 1;
  348. this.boxH = e.getY() - this.boxULy + 1;
  349. int m = Math.min(boxW, boxH);
  350. this.boxW = m;
  351. this.boxH = m;
  352. this.viewUpper.setBox(this.boxULx, this.boxULy, this.boxW, this.boxH);
  353. this.message.setText("boxULx: " + this.boxULx + " boxULy: " + this.boxULy + " boxW: " + this.boxW + " boxH: " + this.boxH);
  354. this.viewUpper.repaint();
  355. }
  356. public void mouseReleased(MouseEvent e) {
  357. if (e.getButton() == MouseEvent.BUTTON1) {
  358. this.viewUpper.repaint();
  359. this.message.setText("Gesetzt: boxULx: " + this.boxULx + " boxULy: " + this.boxULy + " boxW: " + this.boxW + " boxH: " + this.boxH);
  360. }
  361. }
  362. public void mouseMoved(MouseEvent e) {
  363. }
  364. public void mouseExited(MouseEvent e) {
  365. }
  366. public void mouseEntered(MouseEvent e) {
  367. }
  368. public void mouseClicked(MouseEvent e) {
  369. }
  370. // process a new slider value
  371. public void stateChanged(ChangeEvent ce) {
  372. if (ce.getSource() == this.maxIterations) {
  373. // System.out.println("Neuer Wert");
  374. this.nrIterations = this.maxIterations.getValue() * 1000;
  375. }
  376. }
  377. // main, just instantiate an MGui object, gives control to the Gui
  378. public static void main(String[] args) {
  379. @SuppressWarnings("unused")
  380. MGuiRMI gui = new MGuiRMI();
  381. }
  382. }