Public feed #531524

Applet invisible

public javascript v1 · immutable
Raw Edit
·published 2007-06-02 21:12 UTC
rendered paste body
import javax.swing.*;import java.awt.Color;import java.awt.*;import java.awt.Shape;import java.awt.event.*;import java.awt.Graphics;import java.applet.*;import javax.swing.event.*;import javax.swing.text.*;import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; //Applet zum Zeichen beliebiger Kreise,Quadrates,Rechteckes und einer Ellipsepublic  class GeoFigurGUI extends Applet implements ActionListener{    //Paint hilfsklasse zum Zeichnen der Geometrischen Formen	Paint paint = new Paint();    //Widgets fuer die GUI	JFrame frame = new JFrame("Geometrische Figuren");	//panels	JPanel panelButtons = new JPanel();	JPanel panelRadius = new JPanel();	JPanel panelAchseX = new JPanel();	JPanel panelAchseY = new JPanel();	JPanel panelSeiteX = new JPanel();	JPanel panelSeiteY = new JPanel();	JPanel panelFarbe = new JPanel();	JPanel panelList = new JPanel();	JPanel panel1 = new JPanel();	JPanel panel2 = new JPanel();	JPanel panel3 = new JPanel();	JPanel panel4 = new JPanel();		// Labels fuer die Textfelder	JLabel labelRadius = new JLabel("Radius");	JLabel labelAchseX = new JLabel("X - Achse");	JLabel labelAchseY = new JLabel("Y - Achse");	JLabel labelSeiteX = new JLabel("Breite");	JLabel labelSeiteY = new JLabel("Laenge");	JLabel labelFarbe  = new JLabel("Farbe");	JLabel labelFlaeche = new JLabel ("Flaeche: ");		// Textfelder    JTextField fieldRadius = new JTextField("",7);	JTextField fieldAchseX = new JTextField("",7);	JTextField fieldAchseY = new JTextField("",7);	JTextField fieldSeiteX= new JTextField("",7);	JTextField fieldSeiteY= new JTextField("",7);		// Button	JButton buttonAusgabe = new JButton("Ausgabe");	JButton buttonReset = new JButton("Reset");    	//auswahlboxes	Object[] colors = {Color.WHITE, Color.BLACK, Color.RED, Color.BLUE, Color.GREEN};    Object[] shapes = {"Kreis", "Ellipse", "Quadrat", "Rechteck"};     JComboBox colorBox = new JComboBox(colors);    JComboBox shapeBox = new JComboBox(shapes); 	    // main ruft nur baueGUI() auf	public static void main (String[] args) 	{		GeoFigurGUI gui = new GeoFigurGUI();		gui.baueGUI();	}	// GUI aufbauen	public void baueGUI () 	{		//button mit Event verknuepfen		buttonAusgabe.addActionListener(this);			//textfield auf 7 eingaben beschränken		fieldRadius.setDocument(new TwoCharField(7));		fieldAchseX.setDocument(new TwoCharField(7));		fieldAchseY.setDocument(new TwoCharField(7));		fieldSeiteX.setDocument(new TwoCharField(7));		fieldSeiteY.setDocument(new TwoCharField(7));			    //jedes Label mit seinem Textfeld in ein eigenes Panel setzen (FlowLayout)		panelRadius.add(labelRadius);		panelRadius.add(fieldRadius);				panelFarbe.add(labelFarbe);		panelFarbe.add(colorBox);				panelAchseX.add(fieldAchseX);		panelAchseX.add(labelAchseX);				panelAchseY.add(labelAchseY);		panelAchseY.add(fieldAchseY);				panelSeiteX.add(labelSeiteX);		panelSeiteX.add(fieldSeiteX);				panelSeiteY.add(labelSeiteY);		panelSeiteY.add(fieldSeiteY);				panelButtons.add(shapeBox);        panelButtons.add(buttonAusgabe);        panelButtons.add(buttonReset);				//panels nebeneinander anordnen		panel1.add(panelRadius);		panel1.add(panelFarbe);				panel2.add(panelAchseX);        panel2.add(panelAchseY);                panel3.add(panelSeiteX);        panel3.add(panelSeiteY);                //4 panels sollen untereinander stehen        panel4.setLayout(new BoxLayout(panel4, BoxLayout.Y_AXIS));		panel4.add(panelButtons);		panel4.add(panel1);		panel4.add(panel2);		panel4.add(panel3);		panel4.add(labelFlaeche);		//paintflaeche bestimmen		paint.setPreferredSize(new Dimension(300,300)); 		panel4.add(paint);			//frame		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		frame.add(panel4); 	    frame.setLocationRelativeTo(null); 	    frame.pack();//Groesse berechnen lassen		frame.setVisible(true);		}		//realisiere methode aus dem interface ActionListener	public void actionPerformed(ActionEvent e)	{		//Button fürs zeichenen wird gedrückt...		if(e.getSource() == buttonAusgabe)		{			//Flaecheninhalt			double flaeche;			//Objects für Quadrat/Rechteck und Kreis/Ellipse			Ellipse2D.Float ellipse = new Ellipse2D.Float();			Rectangle2D.Float rectangle = new Rectangle2D.Float(); 	        			//wir teilen unserer Zeichenkomponente die gewählte Farbe mit 	        paint.setColor((Color)colorBox.getSelectedItem()); 	        	        //wir teilen dem Paint Object mit welche Figur wir haben möchten	        if (shapeBox.getSelectedItem() == "Kreis")	        {	            ellipse.x = Float.parseFloat(fieldAchseX.getText());	            ellipse.y = Float.parseFloat(fieldAchseY.getText());	            ellipse.height = Float.parseFloat(fieldRadius.getText());	            ellipse.width = Float.parseFloat(fieldRadius.getText());	            paint.setShape((Shape)ellipse);                 //Flaeche	            flaeche = Math.PI * ellipse.height * ellipse.width;		        labelFlaeche.setText("Flaeche: "+flaeche);             }	         	        if (shapeBox.getSelectedItem() == "Ellipse")	         {	        	 ellipse.x = Float.parseFloat(fieldAchseX.getText());	             ellipse.y = Float.parseFloat(fieldAchseY.getText());	             ellipse.height = Float.parseFloat(fieldSeiteY.getText());	             ellipse.width = Float.parseFloat(fieldSeiteX.getText());                 paint.setShape((Shape)ellipse);                 //Flaeche	             flaeche = Math.PI * ellipse.height * ellipse.width;		         labelFlaeche.setText("Flaeche: "+flaeche);		     }	         	        if (shapeBox.getSelectedItem() == "Quadrat")	        {	        	rectangle.x = Float.parseFloat(fieldAchseX.getText());	            rectangle.y = Float.parseFloat(fieldAchseY.getText());	            rectangle.height = Float.parseFloat(fieldSeiteX.getText());	            rectangle.width = Float.parseFloat(fieldSeiteX.getText());	            paint.setShape((Shape)rectangle);	            //Flaeche	            flaeche = rectangle.height * rectangle.width;		        labelFlaeche.setText("Flaeche: "+flaeche);		     }	            	        if (shapeBox.getSelectedItem() == "Rechteck")	        {	            rectangle.x = Float.parseFloat(fieldAchseX.getText());	            rectangle.y = Float.parseFloat(fieldAchseY.getText());	            rectangle.height = Float.parseFloat(fieldSeiteY.getText());	            rectangle.width = Float.parseFloat(fieldSeiteX.getText());	            paint.setShape((Shape)rectangle);	            //Flaeche	            flaeche = rectangle.height * rectangle.width;		        labelFlaeche.setText("Flaeche: "+flaeche);		    }	        	        //jetzt soll gezeichnet werden 	        paint.repaint(); 	        frame.pack();	    }				//reseten der Textfelder		if(e.getSource() == buttonReset)		{			labelFlaeche.setText("Flaeche: ");			fieldRadius.setText("");			fieldSeiteX.setText("");			fieldSeiteY.setText("");			fieldAchseX.setText("");			fieldAchseY.setText("");		}	}class Paint extends JComponent{	private Shape shape; 	private Color c; 	@Override 	protected void paintComponent(Graphics g) 	{ 		//dient dazu den Hintergrund zu säubern 	    super.paintComponent(g); 	    //AWT/Swing bestimmt wann paintComponent aufgerufen wird, wir müssen 	    //nun also überprüfen ob shape und color noch gar nicht gesetzt wurden 	    if(shape!=null && c!=null) 	    { 	    	Graphics2D g2d = (Graphics2D)g; 	        g2d.setColor(c);	        g2d.fill(shape); 	    } 	 } 	    	public void setColor(Color c) 	{ 	      this.c = c; 	} 	public void setShape(Shape shape) 	{ 	      this.shape = shape; 	} }}	//my html code//<html>//<body>//<applet code="LottoGUI.class" width="600" height="300"></applet>//</body>//</html>