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.

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