package rohr;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.attribute.standard.PrintQuality;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.DocPrintJob;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import javax.print.Doc;
import javax.print.SimpleDoc;
import javax.print.PrintException;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Stroke;
import java.awt.BasicStroke;
import java.awt.Polygon;
import java.awt.geom.Line2D;
import java.awt.geom.Arc2D;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.util.Arrays;
public class Rohr extends JFrame {
JPanel contentPane;
JPanel Ausgabe;
JPanel Eingabe = new JPanel();
JLabel Ein = new JLabel("Eingabe der Nennweite");
JTextField DN = new JTextField();
JLabel hoehe = new JLabel("Eingabe der Höhe (h)");
JTextField hText = new JTextField();
JLabel breite = new JLabel("Eingabe des Abstandes (b)");
JTextField bText = new JTextField();
JButton zuruecksetzen = new JButton("Zurücksetzen");
JButton ok = new JButton("Ok");
BorderLayout borderLayout1 = new BorderLayout();
BorderLayout borderLayout2 = new BorderLayout();
GridLayout EinSeite = new GridLayout(5, 2, 5, 5);
JMenuBar jMenuBar2 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileExit = new JMenuItem();
JMenuItem jMenuFilePrint = new JMenuItem();
Kreis kreis;
Hoehe rHoehe;
Breite rBreite;
BerechnungZeichnen bz;
public Rohr() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(1000, 800));
setTitle("Rohr");
jMenuFile.setText("Optionen");
jMenuFileExit.setText("Beenden");
jMenuFileExit.addActionListener(new Rohr_jMenuFileExit_ActionAdapter(this));
jMenuFilePrint.setText("Drucken");
jMenuFilePrint.setEnabled(false);
jMenuBar2.add(jMenuFile);
jMenuFile.add(jMenuFileExit);
jMenuFile.add(jMenuFilePrint);
Eingabe.setLayout(EinSeite);
Eingabe.add(Ein);
DN.addFocusListener(new Rohr_dnTextField_Focus_Event(this));
hText.addFocusListener(new Rohr_hoeheTextField_Focus_Event(this));
bText.addFocusListener(new Rohr_breiteTextField_Focus_Event(this));
ok.addActionListener(new okButton(this));
zuruecksetzen.addActionListener(new zurueckButton(this));
Eingabe.add(DN);
Eingabe.add(hoehe);
Eingabe.add(hText);
Eingabe.add(breite);
Eingabe.add(bText);
Eingabe.add(ok);
Eingabe.add(zuruecksetzen);
Ausgabe = new JPanel();
Ausgabe.setLayout(borderLayout2);
setJMenuBar(jMenuBar2);
getContentPane().add("Center", Ausgabe);
getContentPane().add("East", Eingabe);
}
void eingaben() {
int dn;
int r = 0;
float d = 0;
if (DN.getText().equals("")) {
dn = 0;
} else {
dn = Integer.parseInt(DN.getText());
}
try {
switch (dn) {
case 10:
r = 28;
d = 17.2F;
break;
case 15:
r = 28;
d = 21.3F;
break;
case 20:
r = 29;
d = 26.9F;
break;
case 25:
r = 38;
d = 33.7F;
break;
case 32:
r = 48;
d = 42.4F;
break;
case 40:
r = 57;
d = 48.3F;
break;
case 50:
r = 76;
d = 60.3F;
break;
case 65:
r = 95;
d = 76.1F;
break;
case 80:
r = 114;
d = 88.9F;
break;
case 100:
r = 150;
d = 104.0F;
break;
case 125:
r = 188;
d = 129.0F;
break;
case 150:
r = 225;
d = 154.0F;
break;
case 200:
r = 300;
d = 204.0F;
break;
case 250:
r = 350;
d = 255.F;
break;
case 300:
r = 400;
d = 306.0F;
break;
case 350:
r = 450;
d = 356.0F;
break;
case 400:
r = 500;
d = 406.0F;
break;
default:
JOptionPane.showMessageDialog(null,
"Bitte die Eingabe korrigieren!",
"Nur Nennduchmesser in DN eingeben",
JOptionPane.ERROR_MESSAGE);
break;
}
if (hText.getText().equals("") || bText.getText().equals("")) {
JOptionPane.showMessageDialog(null,
"Es wurde(n) kein(e) Höhen- und/oder Abstandswert(e) eingegeben.",
"Höhen- bzw. Abstandswerte überprüfen.",
JOptionPane.ERROR_MESSAGE);
} else {
Ausgabe.removeAll();
Ausgabe.add("Center", bz = new BerechnungZeichnen(this, Integer.parseInt(hText.getText()), Integer.parseInt(bText.getText()), r, d));
Ausgabe.validate();
getContentPane().add("Center", Ausgabe);
this.validate();
repaint();
}
}
catch (NumberFormatException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null,
"Ausnahme:" + e,
"Number Format Exception",
JOptionPane.ERROR_MESSAGE);
}
}
void dnTextField_Focus_Event(FocusEvent focusEvent) {
Ausgabe.removeAll();
Ausgabe.add("Center", kreis = new Kreis(150));
Ausgabe.validate();
getContentPane().add("Center", Ausgabe);
this.validate();
repaint();
}
void hoeheTextField_Focus_Event(FocusEvent focusEvent) {
Ausgabe.removeAll();
Ausgabe.add("Center", rHoehe = new Hoehe());
Ausgabe.validate();
getContentPane().add("Center", Ausgabe);
this.validate();
repaint();
}
void breiteTextField_Focus_Event(FocusEvent focusEvent) {
Ausgabe.removeAll();
Ausgabe.add("Center", rBreite = new Breite());
Ausgabe.validate();
getContentPane().add("Center", Ausgabe);
this.validate();
repaint();
}
void datenUebergabe(ActionEvent evt) {
eingaben();
}
void datenLoeschen(ActionEvent evt) {
DN.setText("");
hText.setText("");
bText.setText("");
}
void jMenuFileExit_actionPerformed(ActionEvent actionEvent) {
System.exit(0);
}
void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw,
int gh, int wx, int wy) {
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}
}
class BerechnungZeichnen extends JPanel {
Rohr adaptee;
int i;
int j;
int r;
float d;
BerechnungZeichnen(Rohr adaptee, int i, int j, int r, float d) {
this.adaptee = adaptee;
this.i = i; //höhenmaß
this.j = j; //breitenmaß
this.r = r; //radius
this.d = d; //durchmesser
}
public void paintComponent(Graphics comp) {
this.adaptee.jMenuFilePrint.setEnabled(true);
this.adaptee.jMenuFilePrint.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
new PrintOut(i, j, r, d);
}
});
Graphics2D comp2D = (Graphics2D) comp;
Color c1 = new Color(255, 0, 0);
Color c2 = new Color(0, 0, 0);
Stroke gestrichelt = new BasicStroke(1.0F, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0F, new float[] {21.0F, 9.0F, 3.0F, 9.0F }, 0.0F);
Stroke durchgehend = new BasicStroke(2.0F, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER);
comp2D.setColor(c2);
comp2D.setStroke(durchgehend);
// erstes Rohrstück
comp2D.draw(new Line2D.Float(10F, 10F, 60F, 10F)); //obere Waagerechte
comp2D.draw(new Line2D.Float(10F, 10F+d, 60F, 10F+d)); //untere Waagerechte
comp2D.draw(new Line2D.Float(10F, 10F, 10F, 10F+d)); //linke Senkrechte
comp2D.draw(new Line2D.Float(60F, 10F, 60F, 10F+d)); //rechte Senkrechte
// zweites Rohrstück
comp2D.draw(new Line2D.Float(60F+j, 10F+i, 110F+j, 10F+i)); //obere Waagerechte
comp2D.draw(new Line2D.Float(60F+j, 10F+i+d, 110F+j, 10F+i+d)); //untere Waagerechte
comp2D.draw(new Line2D.Float(60F+j, 10F+i, 60F+j, 10F+i+d)); //linek Senkrechte
comp2D.draw(new Line2D.Float(110F+j, 10F+i, 110F+j, 10F+i+d)); //rechte Senkrechte
// Mittellinie erstes Rohrstück
comp2D.setColor(c1);
comp2D.setStroke(gestrichelt);
comp2D.draw(new Line2D.Float(3, 10+(d/2), 60, 10+(d/2)));
// Mittellinie zweites Rohrstück
comp2D.draw(new Line2D.Float(60+j, 10F+i+(d/2), 120+j, 10F+i+(d/2)));
// Berechnung des Winkels
double winkelGamma = 0;
double bogenmassDelta = 0;
double bogenmassAlpha = 0;
int winkelGammaInt = 0;
if ((i/2)>=r){
double wurzelHalbeHoeheMinusRadiusUndBreite = Math.sqrt((((i / 2) -
r) * ((i / 2) - r)) + ((j / 2) * (j / 2)));
double halbesRohrZwischenstück = Math.sqrt((
wurzelHalbeHoeheMinusRadiusUndBreite *
wurzelHalbeHoeheMinusRadiusUndBreite) - (r * r)); //Das Zwischenstück muß verdoppelt werden, damit es das reelle wird!!!
bogenmassAlpha = Math.acos((halbesRohrZwischenstück /
wurzelHalbeHoeheMinusRadiusUndBreite));
double bogenmassBeta = Math.acos((j / 2) /
wurzelHalbeHoeheMinusRadiusUndBreite);
double winkelAlpha = Math.toDegrees(bogenmassAlpha);
double winkelBeta = Math.toDegrees(bogenmassBeta);
winkelGamma = winkelAlpha + winkelBeta;
winkelGammaInt = (int) winkelGamma;
comp2D.drawString("If-Schleife. Winkel ist : "+ winkelGamma , 20, 600);
} else {
double wurzelHalbeHoeheMinusRadiusUndBreite = Math.sqrt(((j / 2) * (j / 2)) + ((r - (i/2)) * (r - (i/2))));
bogenmassDelta = Math.acos(r/wurzelHalbeHoeheMinusRadiusUndBreite);
bogenmassAlpha = Math.acos((j/2)/wurzelHalbeHoeheMinusRadiusUndBreite);
double winkelDelta = Math.toDegrees(bogenmassDelta);
double winkelAlpha = Math.toDegrees(bogenmassAlpha);
winkelGamma = 90-winkelDelta-winkelAlpha;
winkelGammaInt = (int) winkelGamma;
comp2D.drawString("Else-Schleife. Winkel ist : "+ winkelGamma, 20, 600);
}
// oberer Bogen in schwarz
comp2D.setColor(c1);
comp2D.setStroke(gestrichelt);
int t = (int)d; // float Durchmesser zu int Durchmesser
Arc2D.Float bogen1 = new Arc2D.Float(60-(r+(d/2)), 10, ((r+(d/2))*2), ((r+(d/2))*2), 90, -winkelGammaInt,Arc2D.OPEN); //Außenbogen
Arc2D.Float bogen2 = new Arc2D.Float(60-(r-(d/2)), 10+d, ((r-(d/2))*2), ((r-(d/2))*2), 90, -winkelGammaInt,Arc2D.OPEN); //Innenbogen
Arc2D.Float bogen3 = new Arc2D.Float(60-r, 10+(d/2), r*2, r*2, 90, -winkelGammaInt,Arc2D.OPEN); //Mittelbogen
comp2D.draw(bogen3); //Mitte
comp2D.setColor(c2);
comp2D.setStroke(durchgehend);
comp2D.draw(bogen1); //Außen
comp2D.draw(bogen2); //Innen
// unterer Bogen
comp2D.setColor(c1);
comp2D.setStroke(gestrichelt);
Arc2D.Float bogen4 = new Arc2D.Float(60+j-(r+(d/2)), 10+i-(r*2), ((r+(d/2))*2), ((r+(d/2))*2), 270, -winkelGammaInt,Arc2D.OPEN); //Außenbogen
Arc2D.Float bogen5 = new Arc2D.Float(60+j-(r-(d/2)), 10+i-r-(r-d), ((r-(d/2))*2), ((r-(d/2))*2), 270, -winkelGammaInt,Arc2D.OPEN); //Innenbogen
Arc2D.Float bogen6 = new Arc2D.Float(60+j-r, 10+i-(r*2)+d-(d/2), r*2, r*2, 270, -winkelGammaInt,Arc2D.OPEN); //Mittelbogen
comp2D.draw(bogen6); //Mitte
comp2D.setColor(c2);
comp2D.setStroke(durchgehend);
comp2D.draw(bogen4); //Außen
comp2D.draw(bogen5); //Innen
// Zwischenstück Mittellinie
double xAuslage1 = Math.toRadians(90-winkelGamma);
double xAuslage = Math.cos(xAuslage1); // Ergiebt , multipliziert mit "r" , die "X-Achse"
xAuslage *= r;
double yAuslage = Math.sqrt((r*r)-(xAuslage*xAuslage));
comp2D.setColor(c1);
comp2D.setStroke(gestrichelt);
comp2D.drawLine(((int)xAuslage)+60, 10+r+((int)(d/2))-((int)yAuslage), 60+j-((int)xAuslage), 10+i+((int)(d/2))-r+((int)yAuslage));
// Zwischenstück obere und untere Linie
xAuslage = Math.cos(xAuslage1);
double xAuslage2 = xAuslage*(r+(d/2)); //oben
double yAuslage2 = Math.sqrt(((r+(d/2))*(r+(d/2)))-(xAuslage2*xAuslage2)); //oben
double xAuslage3 = xAuslage*(r-(d/2)); //unten
double yAuslage3 = Math.sqrt(((r-(d/2))*(r-(d/2)))-(xAuslage3*xAuslage3)); //unten
comp2D.setColor(c2);
comp2D.setStroke(durchgehend);
comp2D.drawLine(((int)xAuslage2)+60, 10+r+((int)(d/2))-((int)yAuslage2), 60+j-((int)xAuslage3), 10+i+((int)(d/2))-r+((int)yAuslage3)); // oben
comp2D.drawLine(((int)xAuslage3)+60, 10+r+((int)(d/2))-((int)yAuslage3), 60+j-((int)xAuslage2), 10+i+((int)(d/2))-r+((int)yAuslage2)); // unten
// Linien an den Bögen
comp2D.drawLine(((int)xAuslage2)+60, 10+r+((int)(d/2))-((int)yAuslage2), ((int)xAuslage3)+60, 10+r+((int)(d/2))-((int)yAuslage3));
comp2D.drawLine(60+j-((int)xAuslage3), 10+i+((int)(d/2))-r+((int)yAuslage3), 60+j-((int)xAuslage2), 10+i+((int)(d/2))-r+((int)yAuslage2));
}
}
class Hoehe extends JPanel {
public void paintComponent(Graphics comp) {
Graphics2D comp2D = (Graphics2D)comp;
Color c1 = new Color(255, 0, 0);
Color c2 = new Color(255,255,255);
comp2D.setColor(c1);
comp2D.drawLine(10, 10, 60, 10);
comp2D.drawLine(10, 60, 60, 60);
comp2D.drawLine(10, 10, 10, 60);
comp2D.drawOval(55, 10, 12, 25);
Arc2D.Float bogen1 = new Arc2D.Float(55, 35, 12, 25, 90,-180, Arc2D.OPEN);
comp2D.draw(bogen1);
for (int i = 3; i<92; i+=8) {
comp2D.drawLine(i, 35, i+3, 35);
comp2D.drawLine(i+5, 35, i+6, 35);
}
comp2D.drawLine(210, 210, 260, 210);
comp2D.drawLine(260, 210, 260, 260);
comp2D.drawLine(210, 260, 260, 260);
comp2D.drawOval(205, 210, 12, 25);
Arc2D.Float bogen2 = new Arc2D.Float(205, 235, 12, 25, 270,-180, Arc2D.OPEN);
comp2D.draw(bogen2);
for (int i = 3; i<92; i+=8) {
comp2D.drawLine(i+170, 235, i+173, 235);
comp2D.drawLine(i+175, 235, i+176, 235);
}
comp2D.drawLine(135, 35, 135, 235);
comp2D.drawLine(98, 35, 155, 35);
comp2D.drawLine(115, 235, 173, 235);
int xCoords1[] = {135, 138, 132, 135};
int yCoords1[] = {35, 45, 45, 35};
int points1 = xCoords1.length;
Polygon poly = new Polygon(xCoords1, yCoords1, points1);
comp2D.fillPolygon(poly);
int xCoords2[] = {135, 138, 132, 135};
int yCoords2[] = {235, 225, 225, 235};
int points2 = xCoords2.length;
poly = new Polygon(xCoords2, yCoords2, points2);
comp2D.fillPolygon(poly);
AffineTransform at = new AffineTransform();
at.setToTranslation(30.0, 60.0);
comp2D.transform(at);
at.rotate(-Math.PI/4.0);
for (int i = 0; i < 8; i++) {
if (i == 2)
comp2D.drawString("h", 0.0F, 0.0F);
comp2D.transform(at);
}
}
}
class Kreis extends JPanel {
int i;
Kreis(int i) {
this.i = i;
}
public void paintComponent(Graphics comp) {
Graphics2D comp2D = (Graphics2D)comp;
Color c1 = new Color(255, 0, 0);
Color c2 = new Color(255,255,255);
comp2D.setColor(c1);
comp2D.fillOval(100, 100, i, i);
comp2D.setColor(c2);
comp2D.fillOval((i/2)+100-((i-4)/2), (i/2)+100-((i-4)/2), i-4, i-4);
comp2D.setColor(c1);
comp2D.drawLine(100, 120+i ,99+i ,120+i);
comp2D.drawLine(100, 100+(i/2), 100, 130+i);
comp2D.drawLine(99+i, 100+(i/2), 99+i, 130+i);
comp2D.drawString("DN", 95+(i/2),117+i);
int xCoords1[] = {99+i, 89+i, 89+i, 99+i};
int yCoords1[] = {120+i, 117+i, 123+i, 120+i};
int points1 = xCoords1.length;
Polygon poly = new Polygon(xCoords1, yCoords1, points1);
comp2D.fillPolygon(poly);
int xCoords2[] = {100, 110, 110, 100};
int yCoords2[] = {120+i, 117+i, 123+i, 120+i};
int points2 = xCoords2.length;
poly = new Polygon(xCoords2, yCoords2, points2);
comp2D.fillPolygon(poly);
}
}
class Breite extends JPanel {
public void paintComponent(Graphics comp) {
Graphics2D comp2D = (Graphics2D) comp;
Color c1 = new Color(255, 0, 0);
Color c2 = new Color(255, 255, 255);
comp2D.setColor(c1);
comp2D.drawLine(10, 10, 60, 10);
comp2D.drawLine(10, 60, 60, 60);
comp2D.drawLine(10, 10, 10, 60);
comp2D.drawOval(55, 10, 12, 25);
Arc2D.Float bogen1 = new Arc2D.Float(55, 35, 12, 25, 90, -180,
Arc2D.OPEN);
comp2D.draw(bogen1);
for (int i = 3; i < 92; i += 8) {
comp2D.drawLine(i, 35, i + 3, 35);
comp2D.drawLine(i + 5, 35, i + 6, 35);
}
comp2D.drawLine(210, 210, 260, 210);
comp2D.drawLine(260, 210, 260, 260);
comp2D.drawLine(210, 260, 260, 260);
comp2D.drawOval(205, 210, 12, 25);
Arc2D.Float bogen2 = new Arc2D.Float(205, 235, 12, 25, 270, -180,
Arc2D.OPEN);
comp2D.draw(bogen2);
for (int i = 3; i < 92; i += 8) {
comp2D.drawLine(i + 170, 235, i + 173, 235);
comp2D.drawLine(i + 175, 235, i + 176, 235);
}
comp2D.drawLine(60,10, 60, 155);
comp2D.drawLine(210,260, 210, 115);
comp2D.drawLine(60,135, 210, 135);
int xCoords1[] = {60, 70, 70, 60};
int yCoords1[] = {135, 132, 138, 135};
int points1 = xCoords1.length;
Polygon poly = new Polygon(xCoords1, yCoords1, points1);
comp2D.fillPolygon(poly);
int xCoords2[] = {210, 200, 200, 210};
int yCoords2[] = {135, 138, 132, 135};
int points2 = xCoords2.length;
poly = new Polygon(xCoords2, yCoords2, points2);
comp2D.fillPolygon(poly);
comp2D.drawString("b", 135, 125);
}
}
class okButton implements ActionListener {
Rohr adaptee;
okButton(Rohr adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent evt) {
adaptee.datenUebergabe(evt);
}
}
class zurueckButton implements ActionListener {
Rohr adaptee;
zurueckButton(Rohr adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent evt) {
adaptee.datenLoeschen(evt);
}
}
class Rohr_dnTextField_Focus_Event implements FocusListener {
Rohr adaptee;
Rohr_dnTextField_Focus_Event(Rohr adaptee) {
this.adaptee = adaptee;
}
public void focusGained(FocusEvent focusEvent) {
adaptee.dnTextField_Focus_Event(focusEvent);
}
public void focusLost(FocusEvent focusEvent) {
}
}
class Rohr_hoeheTextField_Focus_Event implements FocusListener {
Rohr adaptee;
Rohr_hoeheTextField_Focus_Event(Rohr adaptee) {
this.adaptee = adaptee;
}
public void focusGained(FocusEvent focusEvent) {
adaptee.hoeheTextField_Focus_Event(focusEvent);
}
public void focusLost(FocusEvent focusEvent) {
}
}
class Rohr_breiteTextField_Focus_Event implements FocusListener {
Rohr adaptee;
Rohr_breiteTextField_Focus_Event(Rohr adaptee) {
this.adaptee = adaptee;
}
public void focusGained(FocusEvent focusEvent) {
adaptee.breiteTextField_Focus_Event(focusEvent);
}
public void focusLost(FocusEvent focusEvent) {
}
}
class Rohr_jMenuFileExit_ActionAdapter implements ActionListener {
Rohr adaptee;
Rohr_jMenuFileExit_ActionAdapter(Rohr adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent actionEvent) {
adaptee.jMenuFileExit_actionPerformed(actionEvent);
}
}
class PrintOut {
PageFormat pageFormat;
Paper paper;
public PrintOut(int i, int j, int r, float d) {
final String sErrNoPrintService = "Es ist kein passender Print-Service installiert.";
String s2ndParm = null;
int idxPrintService = -1;
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.ISO_A4);
aset.add(OrientationRequested.LANDSCAPE);
aset.add(PrintQuality.DRAFT);
aset.add(new MediaPrintableArea(10, 10, 190, 277, MediaPrintableArea.MM));
try {
PrintService prservDflt = PrintServiceLookup.lookupDefaultPrintService();
PrintService[] prservices = PrintServiceLookup.lookupPrintServices(flavor, aset);
if (null == prservices || 0 >= prservices.length) {
if (null != prservDflt) {
prservices = new PrintService[] { prservDflt };
} else {
System.err.println(sErrNoPrintService);
System.exit(3);
}
}
PrintService prserv = null;
if (0 <= idxPrintService && idxPrintService < prservices.length) {
prserv = prservices[idxPrintService];
} else {
if (!Arrays.asList(prservices).contains(prservDflt)) {
prservDflt = null;
}
prserv = ServiceUI.printDialog(null, 50, 50, prservices, prservDflt, null, aset);
}
if (null != prserv ) {
pageFormat = new PageFormat();
paper = new Paper();
paper.setSize(594.936, 841.536);
paper.setImageableArea(100, 100, 594.836, 841.436);
pageFormat.setOrientation(PageFormat.LANDSCAPE);
pageFormat.setPaper(paper);
if (null == s2ndParm || !s2ndParm.equalsIgnoreCase("np")) {
DocPrintJob pj = prserv.createPrintJob();
Doc doc = new SimpleDoc(new PrintObjects(i, j, r, d), flavor, null);
pj.print(doc, aset);
}
}
} catch (PrintException pe) {
System.err.println(pe);
}
}
}
class PrintObjects implements Printable {
public int iResMul = 4;
int i;
int j;
int r;
float d;
public PrintObjects(int i, int j, int r, float d) {
this.i = i; //höhenmaß
this.j = j; //breitenmaß
this.r = r; //radius
this.d = d; //durchmesser
}
public int print(Graphics g, PageFormat pf, int iPage) throws PrinterException {
if (0 != iPage) {
return NO_SUCH_PAGE;
}
try {
int iPosX = 1;
int iPosY = 1;
float fa = 50; // 10F
int ia = (int)fa;
Graphics2D g2 = (Graphics2D)g;
Color c1 = new Color(255, 0, 0);
Color c2 = new Color(0, 0, 0);
Stroke gestrichelt = new BasicStroke(1.0F, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0F, new float[] {21.0F, 9.0F, 3.0F, 9.0F }, 0.0F);
Stroke durchgehend = new BasicStroke(2.0F, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER);
// erstes Rohrstück
g2.draw(new Line2D.Float(fa, fa, fa+50, fa)); //obere Waagerechte
g2.draw(new Line2D.Float(fa, fa+d, fa+50, fa+d)); //untere Waagerechte
g2.draw(new Line2D.Float(fa, fa, fa, fa+d)); //linke Senkrechte
g2.draw(new Line2D.Float(fa+50, fa, fa+50, fa+d)); //rechte Senkrechte
// zweites Rohrstück
g2.draw(new Line2D.Float(fa+50+j, fa+i, fa+100+j, fa+i)); //obere Waagerechte
g2.draw(new Line2D.Float(fa+50+j, fa+i+d, fa+100+j, fa+i+d)); //untere Waagerechte
g2.draw(new Line2D.Float(fa+50+j, fa+i, fa+50+j, fa+i+d)); //linek Senkrechte
g2.draw(new Line2D.Float(fa+100+j, fa+i, fa+100+j, fa+i+d)); //rechte Senkrechte
// Mittellinie erstes Rohrstück
g2.setColor(c1);
g2.setStroke(gestrichelt);
g2.draw(new Line2D.Float(fa-5, fa+(d/2), fa+50, fa+(d/2)));
// Mittellinie zweites Rohrstück
g2.draw(new Line2D.Float(fa+50+j, fa+i+(d/2), fa+120+j, fa+i+(d/2))); // lange Striche
// Berechnung des Winkels
double winkelGamma = 0;
double bogenmassDelta = 0;
double bogenmassAlpha = 0;
int winkelGammaInt = 0;
if ((i/2)>=r){
double wurzelHalbeHoeheMinusRadiusUndBreite = Math.sqrt((((i / 2) - r) * ((i / 2) - r)) + ((j / 2) * (j / 2)));
double halbesRohrZwischenstück = Math.sqrt((wurzelHalbeHoeheMinusRadiusUndBreite * wurzelHalbeHoeheMinusRadiusUndBreite) - (r * r)); //Das Zwischenstück muß verdoppelt werden, damit es das reelle wird!!!
bogenmassAlpha = Math.acos((halbesRohrZwischenstück / wurzelHalbeHoeheMinusRadiusUndBreite));
double bogenmassBeta = Math.acos((j / 2) / wurzelHalbeHoeheMinusRadiusUndBreite);
double winkelAlpha = Math.toDegrees(bogenmassAlpha);
double winkelBeta = Math.toDegrees(bogenmassBeta);
winkelGamma = winkelAlpha + winkelBeta;
winkelGammaInt = (int) winkelGamma;
g2.drawString("Eingegebene Maße:", fa, 360);
g2.drawString("Rohrdurchmesser: " + d , fa, 370);
g2.drawString("Höhe: " + i, fa,380);
g2.drawString("Abstand: " + j, fa,390);
g2.drawString("Der Winkel beträgt :" + winkelGamma , fa, 400);
} else {
double wurzelHalbeHoeheMinusRadiusUndBreite = Math.sqrt(((j / 2) * (j / 2)) + ((r - (i/2)) * (r - (i/2))));
bogenmassDelta = Math.acos(r/wurzelHalbeHoeheMinusRadiusUndBreite);
bogenmassAlpha = Math.acos((j/2)/wurzelHalbeHoeheMinusRadiusUndBreite);
double winkelDelta = Math.toDegrees(bogenmassDelta);
double winkelAlpha = Math.toDegrees(bogenmassAlpha);
winkelGamma = 90-winkelDelta-winkelAlpha;
winkelGammaInt = (int) winkelGamma;
g2.drawString("Eingegebene Maße:", fa, 360);
g2.drawString("Rohrdurchmesser: " + d , fa, 370);
g2.drawString("Höhe: " + i, fa,380);
g2.drawString("Abstand: " + j, fa,390);
g2.drawString("Der Winkel beträgt :" + winkelGamma , fa, 400);
}
// oberer Bogen
g2.setColor(c1);
g2.setStroke(gestrichelt);
int t = (int)d; // float Durchmesser zu int Durchmesser
Arc2D.Float bogen1 = new Arc2D.Float(fa+50-(r+(d/2)), fa, ((r+(d/2))*2), ((r+(d/2))*2), 90, -winkelGammaInt,Arc2D.OPEN); //Außenbogen
Arc2D.Float bogen2 = new Arc2D.Float(fa+50-(r-(d/2)), fa+d, ((r-(d/2))*2), ((r-(d/2))*2), 90, -winkelGammaInt,Arc2D.OPEN); //Innenbogen
Arc2D.Float bogen3 = new Arc2D.Float(fa+50-r, fa+(d/2), r*2, r*2, 90, -winkelGammaInt,Arc2D.OPEN); //Mittelbogen
g2.draw(bogen3); //Mitte
g2.setColor(c2);
g2.setStroke(durchgehend);
g2.draw(bogen1); //Außen
g2.draw(bogen2); //Innen
// unterer Bogen
g2.setColor(c1);
g2.setStroke(gestrichelt);
Arc2D.Float bogen4 = new Arc2D.Float(fa+50+j-(r+(d/2)), fa+i-(r*2), ((r+(d/2))*2), ((r+(d/2))*2), 270, -winkelGammaInt,Arc2D.OPEN); //Außenbogen
Arc2D.Float bogen5 = new Arc2D.Float(fa+50+j-(r-(d/2)), fa+i-r-(r-d), ((r-(d/2))*2), ((r-(d/2))*2), 270, -winkelGammaInt,Arc2D.OPEN); //Innenbogen
Arc2D.Float bogen6 = new Arc2D.Float(fa+50+j-r, fa+i-(r*2)+d-(d/2), r*2, r*2, 270, -winkelGammaInt,Arc2D.OPEN); //Mittelbogen
g2.draw(bogen6); //Mitte
g2.setColor(c2);
g2.setStroke(durchgehend);
g2.draw(bogen4); //Außen
g2.draw(bogen5); //Innen
// Zwischenstück Mittellinie
double xAuslage1 = Math.toRadians(90-winkelGamma);
double xAuslage = Math.cos(xAuslage1); // Ergiebt , multipliziert mit "r" , die "X-Achse"
xAuslage *= r;
double yAuslage = Math.sqrt((r*r)-(xAuslage*xAuslage));
g2.setColor(c1);
g2.setStroke(gestrichelt);
g2.drawLine(((int)xAuslage)+ia+50, ia+r+((int)(d/2))-((int)yAuslage), ia+50+j-((int)xAuslage), ia+i+((int)(d/2))-r+((int)yAuslage));
// Zwischenstück obere und untere Linie
xAuslage = Math.cos(xAuslage1);
double xAuslage2 = xAuslage*(r+(d/2)); //oben
double yAuslage2 = Math.sqrt(((r+(d/2))*(r+(d/2)))-(xAuslage2*xAuslage2)); //oben
double xAuslage3 = xAuslage*(r-(d/2)); //unten
double yAuslage3 = Math.sqrt(((r-(d/2))*(r-(d/2)))-(xAuslage3*xAuslage3)); //unten
g2.setColor(c2);
g2.setStroke(durchgehend);
g2.drawLine(((int)xAuslage2)+ia+50, ia+r+((int)(d/2))-((int)yAuslage2), ia+50+j-((int)xAuslage3), ia+i+((int)(d/2))-r+((int)yAuslage3)); // oben
g2.drawLine(((int)xAuslage3)+ia+50, ia+r+((int)(d/2))-((int)yAuslage3), ia+50+j-((int)xAuslage2), ia+i+((int)(d/2))-r+((int)yAuslage2)); // unten
// Linien an den Bögen
g2.drawLine(((int)xAuslage2)+ia+50, ia+r+((int)(d/2))-((int)yAuslage2), ((int)xAuslage3)+ia+50, ia+r+((int)(d/2))-((int)yAuslage3));
g2.drawLine(ia+50+j-((int)xAuslage3), ia+i+((int)(d/2))-r+((int)yAuslage3), ia+50+j-((int)xAuslage2), ia+i+((int)(d/2))-r+((int)yAuslage2));
} catch (Exception e) {
throw new PrinterException(e.getMessage());
}
return PAGE_EXISTS;
}
}