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.

285 lines
7.6 KiB

  1. package verteiltesysteme.mandelbrot.rmi;
  2. //Mandelbot Example A. Gepperth
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.MouseListener;
  9. import java.awt.event.MouseMotionListener;
  10. import java.awt.image.BufferedImage;
  11. import java.rmi.NotBoundException;
  12. import java.rmi.RemoteException;
  13. import java.rmi.registry.LocateRegistry;
  14. import java.rmi.registry.Registry;
  15. import javax.swing.ImageIcon;
  16. import javax.swing.JButton;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19. import javax.swing.JPanel;
  20. import javax.swing.JSlider;
  21. import javax.swing.event.ChangeEvent;
  22. import javax.swing.event.ChangeListener;
  23. class MyJLabel extends JLabel {
  24. /**
  25. *
  26. */
  27. private static final long serialVersionUID = -7750935382747799343L;
  28. int boxULx, boxULy, boxW, boxH;
  29. MyJLabel() {
  30. super();
  31. }
  32. MyJLabel(String s) {
  33. super(s);
  34. }
  35. void setBox(int boxULx, int boxULy, int boxW, int boxH) {
  36. this.boxULx = boxULx;
  37. this.boxULy = boxULy;
  38. this.boxW = boxW;
  39. this.boxH = boxH;
  40. }
  41. @Override
  42. protected void paintComponent(Graphics g) {
  43. super.paintComponent(g);
  44. g.setColor(Color.black);
  45. g.drawRect(this.boxULx, this.boxULy, this.boxW, this.boxH);
  46. }
  47. }
  48. public class MGuiRMI implements ActionListener, ChangeListener, MouseListener, MouseMotionListener {
  49. // GUI elements
  50. JFrame frame;
  51. JPanel panel;
  52. JButton endButton;
  53. JButton backButton;
  54. JButton calcButton;
  55. JSlider maxIterations;
  56. MyJLabel view;
  57. int nrIterations;
  58. // box management
  59. int boxULx, boxULy, boxW, boxH;
  60. boolean dragging = false;
  61. // this one does all the calculating work
  62. RMIMandelbrotCalculationsInterface remoteCalcObj;
  63. // how many points in the image?
  64. // change if you want a higher resolution
  65. public static final int RESX = 600;
  66. public static final int RESY = 600;
  67. MGuiRMI() {
  68. // set up GUI
  69. this.frame = new JFrame("Mandelbrotmenge");
  70. this.panel = new JPanel();
  71. this.endButton = new JButton("Ende");
  72. this.panel.add(this.endButton);
  73. this.backButton = new JButton("Zurueck");
  74. this.panel.add(this.backButton);
  75. this.calcButton = new JButton("Rechnen");
  76. this.panel.add(this.calcButton);
  77. this.frame.add(this.panel);
  78. this.frame.setSize(500, 500);
  79. this.frame.setVisible(true);
  80. this.view = new MyJLabel("Platzhalter");
  81. this.maxIterations = new JSlider(0, 50, 30);
  82. this.panel.add(this.maxIterations);
  83. this.panel.add(this.view);
  84. this.endButton.addActionListener(this);
  85. this.backButton.addActionListener(this);
  86. this.calcButton.addActionListener(this);
  87. this.maxIterations.addChangeListener(this);
  88. this.maxIterations.setMajorTickSpacing(10);
  89. this.maxIterations.setPaintTicks(true);
  90. this.maxIterations.setPaintLabels(true);
  91. this.view.addMouseListener(this);
  92. this.view.addMouseMotionListener(this);
  93. this.boxULx = 0;
  94. this.boxULy = 0;
  95. this.boxW = MGuiRMI.RESX;
  96. this.boxH = MGuiRMI.RESY;
  97. // Non-Gui stuff
  98. String host = "";
  99. try {
  100. Registry registry = LocateRegistry.getRegistry(host);
  101. this.remoteCalcObj = (RMIMandelbrotCalculationsInterface) registry.lookup("RMIEchoInterface");
  102. } catch (RemoteException | NotBoundException e) {
  103. // TODO Auto-generated catch block
  104. e.printStackTrace();
  105. }
  106. this.nrIterations = 1000;
  107. }
  108. // visualization related stuff, do not touch!
  109. int[] generateLookupTable(int nrHues) {
  110. int[] tmp = new int[nrHues + 1];
  111. int cycle = 2048;
  112. for (int i = 0; i < nrHues; i++) {
  113. float hue = 1 * ((float) (i % cycle)) / cycle/* ((float)nrHues) */ ;
  114. tmp[i] = Color.HSBtoRGB(hue, 0.8f, 1.0f);
  115. }
  116. tmp[0] = Color.HSBtoRGB(0f, 1.0f, 0.0f);
  117. return tmp;
  118. }
  119. // visualization
  120. static void applyLookupTable(int[] data, int[] dest, int[] lookupTable) {
  121. for (int i = 0; i < data.length; i++) {
  122. dest[i] = lookupTable[data[i]];
  123. }
  124. }
  125. // here we check what the buttons do
  126. public void actionPerformed(ActionEvent ae) {
  127. if (ae.getSource() == this.endButton) {
  128. System.out.println("Ende");
  129. System.exit(0);
  130. } else
  131. if (ae.getSource() == this.backButton) {
  132. System.out.println("Zurueck");
  133. double ULx = -0.16099999999999995;
  134. double ULy = -0.9365333333333333;
  135. double LRx = -0.03533333333333327;
  136. double LRy = -0.8108666666666666;
  137. this.boxULx = 0;
  138. this.boxULy = 0;
  139. this.boxW = MGuiRMI.RESX;
  140. this.boxH = MGuiRMI.RESY;
  141. this.view.setBox(this.boxULx, this.boxULy, this.boxW, this.boxH);
  142. this.view.repaint();
  143. try {
  144. this.remoteCalcObj.setView(ULx, ULy, LRx, LRy);
  145. } catch (RemoteException e) {
  146. // TODO Auto-generated catch block
  147. e.printStackTrace();
  148. }
  149. } else
  150. if (ae.getSource() == this.calcButton) {
  151. System.out.println("Rechnen");
  152. long timestampStart = System.currentTimeMillis();
  153. double ULx = 0;
  154. double ULy = 0;
  155. double LRx = 0;
  156. double LRy = 0;
  157. try {
  158. ULx = this.remoteCalcObj.getULx();
  159. ULy = this.remoteCalcObj.getULy();
  160. LRx = this.remoteCalcObj.getLRx();
  161. LRy = this.remoteCalcObj.getLRy();
  162. } catch (RemoteException e) {
  163. // TODO Auto-generated catch block
  164. e.printStackTrace();
  165. }
  166. double w = LRx - ULx;
  167. double h = LRy - ULy;
  168. ULx = ULx + w * this.boxULx / (double) MGuiRMI.RESX;
  169. ULy = ULy + h * this.boxULy / (double) MGuiRMI.RESY;
  170. w *= this.boxW / (double) (RESX);
  171. h *= this.boxH / (double) (RESX);
  172. LRx = ULx + w;
  173. LRy = ULy + h;
  174. System.out.println(ULx + "/" + ULy + "/" + LRx + "/" + LRy);
  175. this.boxULx = 0;
  176. this.boxULy = 0;
  177. this.boxW = MGuiRMI.RESX;
  178. this.boxH = MGuiRMI.RESY;
  179. int[] colors = null;
  180. try {
  181. this.remoteCalcObj.setView(ULx, ULy, LRx, LRy);
  182. this.remoteCalcObj.iterateAllPoints(this.maxIterations.getValue() * 1000, MGuiRMI.RESX, MGuiRMI.RESY);
  183. int[] lookupTable = generateLookupTable(this.maxIterations.getValue() * 1000);
  184. colors = new int[MGuiRMI.RESX * MGuiRMI.RESY];
  185. MGuiRMI.applyLookupTable(remoteCalcObj.getResult(), colors, lookupTable);
  186. } catch (RemoteException e) {
  187. // TODO Auto-generated catch block
  188. e.printStackTrace();
  189. }
  190. BufferedImage colorImage = new BufferedImage(MGuiRMI.RESX, MGuiRMI.RESY, BufferedImage.TYPE_INT_ARGB);
  191. colorImage.setRGB(0, 0, MGuiRMI.RESX, MGuiRMI.RESY, colors, 0, MGuiRMI.RESX);
  192. this.view.setIcon(new ImageIcon(colorImage));
  193. this.view.setBox(this.boxULx, this.boxULy, this.boxW, this.boxH);
  194. this.view.repaint();
  195. long timestampEnd = System.currentTimeMillis();
  196. String timeElapsed = "Zeit: " + ((timestampEnd - timestampStart) / 1000.0) + " sec";
  197. System.out.println(timeElapsed);
  198. this.view.setText(timeElapsed);
  199. }
  200. }
  201. public void mousePressed(MouseEvent e) {
  202. this.boxULx = e.getX();
  203. this.boxULy = e.getY();
  204. this.boxW = 1;
  205. this.boxH = 1;
  206. this.view.setBox(this.boxULx, this.boxULy, this.boxW, this.boxH);
  207. }
  208. public void mouseDragged(MouseEvent e) {
  209. this.boxW = e.getX() - this.boxULx + 1;
  210. this.boxH = e.getY() - this.boxULy + 1;
  211. int m = Math.min(boxW, boxH);
  212. this.boxW = m;
  213. this.boxH = m;
  214. this.view.setBox(this.boxULx, this.boxULy, this.boxW, this.boxH);
  215. this.view.repaint();
  216. }
  217. public void mouseReleased(MouseEvent e) {
  218. if (e.getButton() == MouseEvent.BUTTON1) {
  219. this.view.repaint();
  220. }
  221. }
  222. public void mouseMoved(MouseEvent e) {
  223. }
  224. public void mouseExited(MouseEvent e) {
  225. }
  226. public void mouseEntered(MouseEvent e) {
  227. }
  228. public void mouseClicked(MouseEvent e) {
  229. }
  230. // process a new slider value
  231. public void stateChanged(ChangeEvent ce) {
  232. if (ce.getSource() == this.maxIterations) {
  233. //System.out.println("Neuer Wert");
  234. this.nrIterations = this.maxIterations.getValue() * 1000;
  235. }
  236. }
  237. // main, just instantiate an MGui object, gives control to the Gui
  238. public static void main(String[] args) {
  239. @SuppressWarnings("unused")
  240. MGuiRMI gui = new MGuiRMI();
  241. }
  242. }