[Java] Torusdarstellung

Dieses Thema im Forum "Programmierung & Entwicklung" wurde erstellt von huebstAr, 5. Juni 2009 .

Schlagworte:
Status des Themas:
Es sind keine weiteren Antworten möglich.
  1. 5. Juni 2009
    Torusdarstellung

    Hi alle!

    Ich habe ne Aufgabe bekommen nen Torus zu berechnen und diesen darzustellen.
    Das mit dem Berechnen war kein Problem, auch das mit dem Fenster nicht.

    Leider habe ich nicht soo viel Ahnung von den angewendeten Funktionen (Gridlayout usw.).

    Nun mein Problem:
    Ich muss den Torus noch darstellen und zwar im selben Fenster wie die Eingabe und die Ergebnisse.

    Leider verstehe ich nicht so ganz, wie ich nun noch weitere Objekte (die Grafik) dem schon bestehenden Fenster hinzufüge, ich habe überhaupt keinen Ansatz.

    Im Anhang findet ihr den Quelltext, nach Möglichkeit sollte rechts daneben der Torus dargestellt werden, wie ist erstmal egal, wenn ich nen Ansatz habe, mache ich weiter.

    Könnte mir vielleicht jemand die Augen öffnen? :\ Wäre echt klasse!
    Code:
    [B]//Torus.java[/B]
    import java.awt.*;
    
    public class Torus extends Frame
    {
     private Label headline = new Label("<3 this torus..."); 
     private Label radius_quer = new Label("Radius (Querschnitt): ");
     private Label radius_mitte = new Label("Radius (Mittelpunkt zu Kreismitte): ");
     public Label result_volume = new Label("");
     public Label result_surface = new Label("");
     
     public TextField i_rQuer = new TextField(6);
     public TextField i_rMitte = new TextField(6);
     
     public Button b_close = new Button("Beenden");
     public Button b_rechnen = new Button("Berechnen");
     
     private Panel panel_oben = new Panel();
     private Panel panel_mitte = new Panel(new GridLayout(5,2));
     private Panel panel_unten = new Panel(new GridLayout(1,1));
     
     private Panel panel_center1 = new Panel(new FlowLayout(FlowLayout.LEFT));
     private Panel panel_center2 = new Panel(new FlowLayout(FlowLayout.LEFT));
     
     private MyActionListener myActionListener = new MyActionListener(this);
     
     public Torus()
     {
     super("Oh my god, it's a torus!");
     
     // Schriftarten 
     this.headline.setFont(new Font("Arial",Font.BOLD | Font.ITALIC,16));
     this.result_volume.setFont(new Font("Arial",Font.ITALIC,14));
     this.result_surface.setFont(new Font("Arial",Font.ITALIC,14));
     
     // Headline
     this.panel_mitte.add(headline);
     
     // Aufteilung 
     this.panel_center1.add(this.radius_quer);
     this.panel_center1.add(this.i_rMitte);
     this.panel_center2.add(this.radius_mitte);
     this.panel_center2.add(this.i_rQuer);
     
     this.panel_mitte.add(this.panel_center1);
     this.panel_mitte.add(this.panel_center2);
     
     this.panel_mitte.add(this.result_volume);
     this.panel_mitte.add(this.result_surface);
     
     this.panel_unten.add(this.b_rechnen);
     this.panel_unten.add(this.b_close);
     
     this.add(this.panel_oben,BorderLayout.NORTH);
     this.add(this.panel_mitte,BorderLayout.CENTER);
     this.add(this.panel_unten,BorderLayout.SOUTH);
     
     this.pack();
     this.show();
     
     this.addWindowListener(new MyWindowListener());
     this.b_close.addActionListener(myActionListener);
     this.b_rechnen.addActionListener(myActionListener); 
     }
     
     public static void main(String argv[])
     {
     Torus window = new Torus();
     }
    }
    
    Code:
    [B]//MyActionListener.java[/B]
    import java.awt.event.*;
    import java.text.*;
    import java.math.*;
    
    public class MyActionListener implements ActionListener
    {
     public Torus window;
    
     public MyActionListener(Torus window)
     {
     this.window=window;
     }
     
     public void actionPerformed(ActionEvent e)
     {
     Object obj = e.getSource();
    
     if(obj==window.b_close)
     System.exit(0);
    
     DecimalFormat df = new DecimalFormat("#,##0.0");
     double volumen=0;
     double oberflaeche=0;
     double radius_quer=0;
     double radius_mitte=0;
     
     try
     {
     radius_quer = Double.parseDouble(window.i_rQuer.getText().replace(',','.'));
     radius_mitte = Double.parseDouble(window.i_rMitte.getText().replace(',','.'));
     
     if(obj==window.b_rechnen)
     {
     volumen=2*((Math.PI*Math.PI)*radius_mitte*(radius_quer*radius_quer));
     oberflaeche=4*(Math.PI*Math.PI*radius_mitte*radius_quer);
     }
     
     window.result_volume.setText("Volumen: "+df.format(volumen));
     window.result_surface.setText("Flaeche: "+df.format(oberflaeche));
     }
     
     catch(NumberFormatException error)
     {
     window.result_volume.setText("");
     window.result_surface.setText("Eingabefehler! " + "Bitte erneut eingeben!");
     } 
     
     }
     
    }
    
    Code:
    [B]//MyWindowListener.java[/B]
    import java.awt.event.*;
    public class MyWindowListener extends WindowAdapter
    {
     public void windowClosing(WindowEvent e)
     {
     System.exit(0);
     }
    }
    
    Gruß
    huebstAr
     
  2. 5. Juni 2009
    AW: Torusdarstellung

    hi,

    habe dir da mal was schnelles gebastelt:


    Code:
    //Torus.java
    import java.awt.*;
    
    public class Torus extends Frame
    {
     private Label headline = new Label("<3 this torus..."); 
     private Label radius_quer = new Label("Radius (Querschnitt): ");
     private Label radius_mitte = new Label("Radius (Mittelpunkt zu Kreismitte): ");
     public Label result_volume = new Label("");
     public Label result_surface = new Label("");
     
     public TextField i_rQuer = new TextField(6);
     public TextField i_rMitte = new TextField(6);
     
     public Button b_close = new Button("Beenden");
     public Button b_rechnen = new Button("Berechnen");
     
     private Panel panel_oben = new Panel();
     private Panel panel_mitte = new Panel(new GridLayout(5,2));
     private Panel panel_unten = new Panel(new GridLayout(1,1));
    [color=red] // anlegen des neuen panles
     private Panel panel_rechts = new Panel();[/color]
     
     private Panel panel_center1 = new Panel(new FlowLayout(FlowLayout.LEFT));
     private Panel panel_center2 = new Panel(new FlowLayout(FlowLayout.LEFT));
     
     private MyActionListener myActionListener = new MyActionListener(this);
     
     public Torus()
     {
     super("Oh my god, it's a torus!");
     
     // Schriftarten 
     this.headline.setFont(new Font("Arial",Font.BOLD | Font.ITALIC,16));
     this.result_volume.setFont(new Font("Arial",Font.ITALIC,14));
     this.result_surface.setFont(new Font("Arial",Font.ITALIC,14));
     
     // Headline
     this.panel_mitte.add(headline);
     
     // Aufteilung 
     this.panel_center1.add(this.radius_quer);
     this.panel_center1.add(this.i_rMitte);
     this.panel_center2.add(this.radius_mitte);
     this.panel_center2.add(this.i_rQuer);
     
     this.panel_mitte.add(this.panel_center1);
     this.panel_mitte.add(this.panel_center2);
     
     this.panel_mitte.add(this.result_volume);
     this.panel_mitte.add(this.result_surface);
     
     this.panel_unten.add(this.b_rechnen);
     this.panel_unten.add(this.b_close);
     
    
    [color=red] // panel zum zeichnen
     this.panel_rechts.add(new MyPaint());
    [/color] 
     this.add(this.panel_oben,BorderLayout.NORTH);
     this.add(this.panel_mitte,BorderLayout.CENTER);
     this.add(this.panel_unten,BorderLayout.SOUTH);[color=red]
     // hier wird es hinzugefügt
     this.add(this.panel_rechts, BorderLayout.EAST);[/color]
     
     this.pack();
     this.show();
     
     this.addWindowListener(new MyWindowListener());
     this.b_close.addActionListener(myActionListener);
     this.b_rechnen.addActionListener(myActionListener); 
     }
     
     public static void main(String argv[])
     {
     Torus window = new Torus();
     }
    }
    

    das ist eine neue klasse, die das zeichnen übernimmt.

    Code:
    import java.awt.Dimension;
    import java.awt.Graphics;
    
    import javax.swing.BorderFactory;
    import javax.swing.JPanel;
    
    public class MyPaint extends JPanel {
     /**
     * 
     */
     private static final long serialVersionUID = 5812667492078999965L;
    
     public MyDraw() {
     this.setPreferredSize(new Dimension(320, 100));
     this.setBorder(BorderFactory.createLoweredBevelBorder());
     }
     
     public void paintComponent(Graphics g) {
     super.paintComponent(g);
     g.drawLine(0, 0, 100, 100);
     }
     }
    
    viel spass noch beim basteln
     
  3. 5. Juni 2009
    AW: Torusdarstellung


    diese antwort bringt mir leider nichts. Danke trotzdem.
     
  4. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.