Browse Source

improved GUI and RMI handling

master
Sebastian Rieger 7 years ago
parent
commit
0b538c539f
  1. 77
      VerteilteSysteme-Examples/src/verteiltesysteme/mandelbrot/rmi/MGuiRMI.java

77
VerteilteSysteme-Examples/src/verteiltesysteme/mandelbrot/rmi/MGuiRMI.java

@ -1,5 +1,7 @@
package verteiltesysteme.mandelbrot.rmi;
import java.awt.BorderLayout;
//Mandelbot Example A. Gepperth
import java.awt.Color;
@ -22,6 +24,8 @@ import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
@ -66,8 +70,10 @@ public class MGuiRMI implements ActionListener, ChangeListener, MouseListener, M
JButton endButton;
JButton backButton;
JButton calcButton;
JTextField hostTextField;
JSlider maxIterations;
MyJLabel view;
JLabel message;
int nrIterations;
// box management
@ -79,8 +85,8 @@ public class MGuiRMI implements ActionListener, ChangeListener, MouseListener, M
// how many points in the image?
// change if you want a higher resolution
public static final int RESX = 600;
public static final int RESY = 600;
public static int RESX = 600;
public static int RESY = 600;
MGuiRMI() {
// set up GUI
@ -88,17 +94,23 @@ public class MGuiRMI implements ActionListener, ChangeListener, MouseListener, M
this.panel = new JPanel();
this.endButton = new JButton("Ende");
this.panel.add(this.endButton);
this.backButton = new JButton("Zurueck");
this.backButton = new JButton("Zurück");
this.panel.add(this.backButton);
this.calcButton = new JButton("Rechnen");
this.hostTextField = new JTextField(20);
this.hostTextField.setText("localhost");
this.panel.add(this.calcButton);
this.panel.add(this.hostTextField);
this.frame.add(this.panel);
this.frame.setSize(500, 500);
this.frame.setSize(700, 720);
this.frame.setVisible(true);
this.view = new MyJLabel("Platzhalter");
this.view = new MyJLabel();
this.maxIterations = new JSlider(0, 50, 30);
this.panel.add(this.maxIterations);
this.panel.add(this.view);
this.message = new JLabel("Status");
this.message.setBorder(new BevelBorder(BevelBorder.LOWERED));
this.frame.add(this.message,BorderLayout.SOUTH);
this.endButton.addActionListener(this);
this.backButton.addActionListener(this);
@ -117,15 +129,9 @@ public class MGuiRMI implements ActionListener, ChangeListener, MouseListener, M
this.boxW = MGuiRMI.RESX;
this.boxH = MGuiRMI.RESY;
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Non-Gui stuff
String host = "";
try {
Registry registry = LocateRegistry.getRegistry(host);
this.remoteCalcObj = (RMIMandelbrotCalculationsInterface) registry.lookup("RMIEchoInterface");
} catch (RemoteException | NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.nrIterations = 1000;
}
@ -157,6 +163,7 @@ public class MGuiRMI implements ActionListener, ChangeListener, MouseListener, M
if (ae.getSource() == this.backButton) {
System.out.println("Zurueck");
// Zoom und Ausschnitt zurücksetzen
double ULx = -0.16099999999999995;
double ULy = -0.9365333333333333;
double LRx = -0.03533333333333327;
@ -167,31 +174,46 @@ public class MGuiRMI implements ActionListener, ChangeListener, MouseListener, M
this.boxH = MGuiRMI.RESY;
this.view.setBox(this.boxULx, this.boxULy, this.boxW, this.boxH);
this.view.repaint();
if (this.remoteCalcObj == null)
{
String message = "Fehler: noch nicht verbunden, noch keine Berechnung durchgeführt";
this.message.setText(message);
System.out.println(message);
}
else
{
try {
this.remoteCalcObj.setView(ULx, ULy, LRx, LRy);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.message.setText("Ausschnitt zurückgesetzt");
}
} else
if (ae.getSource() == this.calcButton) {
String hostname = this.hostTextField.getText();
System.out.println("Verbinde mit RMI Registry auf Host: " + hostname);
try {
Registry registry = LocateRegistry.getRegistry(hostname);
this.remoteCalcObj = (RMIMandelbrotCalculationsInterface) registry.lookup("RMIEchoInterface");
System.out.println("Rechnen");
long timestampStart = System.currentTimeMillis();
//this.boxW = this.boxW / 2;
//MGuiRMI.RESX = MGuiRMI.RESX / 2;
double ULx = 0;
double ULy = 0;
double LRx = 0;
double LRy = 0;
try {
ULx = this.remoteCalcObj.getULx();
ULy = this.remoteCalcObj.getULy();
LRx = this.remoteCalcObj.getLRx();
LRy = this.remoteCalcObj.getLRy();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
double w = LRx - ULx;
double h = LRy - ULy;
ULx = ULx + w * this.boxULx / (double) MGuiRMI.RESX;
@ -206,16 +228,13 @@ public class MGuiRMI implements ActionListener, ChangeListener, MouseListener, M
this.boxW = MGuiRMI.RESX;
this.boxH = MGuiRMI.RESY;
int[] colors = null;
try {
//this.remoteCalcObj.setView(ULx, ULy, LRx, LRy);
this.remoteCalcObj.setView(ULx, ULy, LRx, LRy);
this.remoteCalcObj.iterateAllPoints(this.maxIterations.getValue() * 1000, MGuiRMI.RESX, MGuiRMI.RESY);
int[] lookupTable = generateLookupTable(this.maxIterations.getValue() * 1000);
colors = new int[MGuiRMI.RESX * MGuiRMI.RESY];
MGuiRMI.applyLookupTable(remoteCalcObj.getResult(), colors, lookupTable);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedImage colorImage = new BufferedImage(MGuiRMI.RESX, MGuiRMI.RESY, BufferedImage.TYPE_INT_ARGB);
colorImage.setRGB(0, 0, MGuiRMI.RESX, MGuiRMI.RESY, colors, 0, MGuiRMI.RESX);
@ -226,9 +245,14 @@ public class MGuiRMI implements ActionListener, ChangeListener, MouseListener, M
long timestampEnd = System.currentTimeMillis();
String timeElapsed = "Zeit: " + ((timestampEnd - timestampStart) / 1000.0) + " sec";
String timeElapsed = "Dauer: " + ((timestampEnd - timestampStart) / 1000.0) + " sec";
System.out.println(timeElapsed);
this.view.setText(timeElapsed);
this.message.setText(timeElapsed);
} catch (RemoteException | NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@ -238,6 +262,7 @@ public class MGuiRMI implements ActionListener, ChangeListener, MouseListener, M
this.boxW = 1;
this.boxH = 1;
this.view.setBox(this.boxULx, this.boxULy, this.boxW, this.boxH);
this.message.setText("boxULx: " + this.boxULx + " boxULy: " + this.boxULy + " boxW: " + this.boxW + " boxH: " + this.boxH);
}
public void mouseDragged(MouseEvent e) {
@ -247,6 +272,7 @@ public class MGuiRMI implements ActionListener, ChangeListener, MouseListener, M
this.boxW = m;
this.boxH = m;
this.view.setBox(this.boxULx, this.boxULy, this.boxW, this.boxH);
this.message.setText("boxULx: " + this.boxULx + " boxULy: " + this.boxULy + " boxW: " + this.boxW + " boxH: " + this.boxH);
this.view.repaint();
}
@ -254,6 +280,7 @@ public class MGuiRMI implements ActionListener, ChangeListener, MouseListener, M
if (e.getButton() == MouseEvent.BUTTON1) {
this.view.repaint();
this.message.setText("Gesetzt: boxULx: " + this.boxULx + " boxULy: " + this.boxULy + " boxW: " + this.boxW + " boxH: " + this.boxH);
}
}

Loading…
Cancel
Save