Logikproblem Umschalten von booleans in anderer Klasse

Diskutiere Logikproblem Umschalten von booleans in anderer Klasse im Allgemeine Java-Themen Forum; Hey, die Verzweiflung hat mich hergetrieben. Ich arbeite momentan an einem kleinen Paintprogramm. Meine Aufgabe ist es bevor ich ein Shape male...

  1. magdaStone
    magdaStone Neues Mitglied
    Hey, die Verzweiflung hat mich hergetrieben.

    Ich arbeite momentan an einem kleinen Paintprogramm. Meine Aufgabe ist es bevor ich ein Shape male via JRadioButtons zB gestrichelte, mit Farben gefüllte Shapes zu machen. Jedoch scheitere ich gerade an einem blöden Problem, welches eigentlich nicht so kompliziert sein sollte. Ich habe in meiner Circle class ein paar booleans, nehmen wir jetzt dashedStroke, welchen ich in true umschalten möchte. Wenn ich in meiner CirclePanel class der JRadioButton dashedStrokeBtn selected ist. Mit circle.dashedStroke = true, oder circle.dashedStroke = dashedStrokeBtn.isSelected() bekomme ich immer wieder NullPointerExceptions. Anscheinend ist der Boolean, welchen ich umschalten möchte leer. Ich hab langsam keinen Plan mehr was ich tun soll.
    (booleans sind auf public)
     

    Anhänge:

  2. Vielleicht hilft dir dieser Kurs hier weiter --> (hier klicken)
  3. Robat
    Robat Aktives Mitglied
    Hallo magdaStone,

    für das nächste mal Poste deinen Code und deine Fehlermeldung bitte in [code=Java]..[/code] Tags. So kann man sie zitieren.

    Poste bitte mal dein Code, damit man sieht was du gemacht hast :)

    Schon mal vorab:
    Sowas würde ich grundsätzlich vermeiden (Wenn es möglich ist).
    Arbeite lieber mit Gettern & Settern.
    D.h. mach dir in deiner Circle-Klasse Methode um die Werte zu verändern..
    Code (Java):

    public void setDashedStroke(boolean state)
    {
         this.dashedStroke = state;
    }
     
    Gruß
    Robert
     
  4. magdaStone
    magdaStone Neues Mitglied
    Code (Java):

    package panelzeug;

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JColorChooser;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    import shapezeug.*;
    import forgui.*;

    public class CirclePanel extends JPanel implements ActionListener{
        //static GeoPainterEK painter; //die übergabe von GeoPainter wurde an circle/rectPanel übergeben
       
        Painter_Uebung_3_EK topLevel;
        GeoPainterEK myPainter;
        Circle circle;
        public JTextField xCircle, yCircle, radius;
        public JButton submitCircle;
        boolean activated;
       
        public JRadioButton strokeColBtn = new JRadioButton("Stroke Color");
        public JRadioButton fillColBtn = new JRadioButton("Fill Color");
        public JRadioButton standardStrokeBtn = new JRadioButton("Standard Stroke");
        public JRadioButton wideStrokeBtn = new JRadioButton("Wide Stroke");
        public JRadioButton dashedStrokeBtn = new JRadioButton("Dashed Stroke");
         
        private boolean dashedStroke = false;
         
         
       
        JPanel toolPanel = new JPanel();
        JPanel farbPanel = new JPanel();
        JPanel btnPanel = new JPanel();
        JPanel textPanel = new JPanel();
       
        JColorChooser farbAuswahl = new JColorChooser();
         
       
       
        public CirclePanel(Painter_Uebung_3_EK topLevel, GeoPainterEK myPainter, Circle circle){
            this.topLevel = topLevel;
            this.myPainter = myPainter;
            this.circle = circle;
           
           
            textPanel.setPreferredSize(new Dimension(150,200));
            setBackground(Color.yellow);
            farbPanel.add(farbAuswahl);
           
            //befüllen des Toolpanels mit den Labels usw
            setPreferredSize(new Dimension(180,190));              
           JLabel xCircLabel = new JLabel ("xCoordinate");
           xCircle = new JTextField(10);
           JLabel yCircLabel = new JLabel ("yCoordinate");
           yCircle = new JTextField(10);
           JLabel radiusCircLabel = new JLabel ("Radius");
           radius = new JTextField(10);
             
           textPanel.add(xCircLabel);
           textPanel.add(xCircle);
           textPanel.add(yCircLabel);
           textPanel.add(yCircle);
           textPanel.add(radiusCircLabel);
           textPanel.add(radius);
           submitCircle = new JButton ("Draw Circle");
           submitCircle.addActionListener(topLevel);          
           textPanel.add(submitCircle);
           
           add(textPanel);

           //befüllen des farb u btn panel mit chooser und btn
         
           strokeColBtn.addActionListener(this);
           fillColBtn.addActionListener(this);
           standardStrokeBtn.addActionListener(this);
           wideStrokeBtn.addActionListener(this);
           dashedStrokeBtn.addActionListener(this);
           boolean activated = false;
           
            btnPanel.add(strokeColBtn);
            btnPanel.add(fillColBtn);
            btnPanel.add(standardStrokeBtn);
            btnPanel.add(wideStrokeBtn);
            btnPanel.add(dashedStrokeBtn);

            add(farbPanel);
            add(btnPanel);
           
           
           
           
        }
        public boolean getDashed(){
            return this.dashedStroke;
           
        }
       
        public void actionPerformed(ActionEvent e){
           
            //circle.dashedStroke = circlePanel.dashedStrokeBtn.isSelected();
            //System.out.println(circle.dashedStroke);
           
            if(e.getSource() instanceof JRadioButton){  // hier die einzelnen Booleans umschalten um sie dann direkt beim Zeichnen ausführen zu können
                JRadioButton radioButton = (JRadioButton)e.getSource();

                     
                 
                if(radioButton.getText() =="Stroke Color") {
                    strokeColBtn.setText("test123");
                   // circle.dashedStroke= true;
                   
                   
                    System.out.println("activated");
                  }
                if(radioButton.getText() =="Dashed Stroke") {//circle.dashedStroke = true; //fehlerhaft
                    dashedStrokeBtn.setText("test123");
                    circle.dashedStroke= dashedStrokeBtn.isSelected();
                   
                   
                    System.out.println("activated");
                  }
                if(radioButton.getText() =="Fill Color") {
                    fillColBtn.setText("test123");
                   //circle.fillColor= true;
                    System.out.println("activated");
                  }
                if(radioButton.getText() =="Standard Stroke") {
                    standardStrokeBtn.setText("test123");
                   // circle.dashedStroke= true;
                    System.out.println("activated");
                  }
                if(radioButton.getText() =="Wide Stroke") {
                    wideStrokeBtn.setText("test123");
                   // circle.dashedStroke= true;
                    System.out.println("activated");
                  }
           
                 
        }
    }}
     

    und hier noch eine class
    Code (Java):

    package shapezeug;

    import java.awt.BasicStroke;



    import java.awt.Color;

    import java.awt.Graphics;

    import java.awt.Graphics2D;

    import java.awt.Stroke;

    import java.awt.event.MouseEvent;



    import forgui.GeoPainterEK;

    import forgui.Painter_Uebung_3_EK;

    import panelzeug.*;



    /** A Circle using the Classes Point and Shape*/

    publicclassCircleextends Shape {



      /** the radius for the circle */

      protecteddoubleradius;


      //für die gestrichelte form

      final static float dash1[] = {10.0f};

      final static BasicStroke dashed =  new BasicStroke(1.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,10.0f, dash1, 0.0f);  

      publicbooleandashedStroke = false;

      publicbooleanwideStroke = false;

      publicbooleanstandardStroke = false;

      publicbooleanfillColor = false;

      publicbooleanstrokeColor = false;

      Color newShapeColor;





      /** Constructor */

      public Circle(Point center, double radius) {

        super(center);

        this.radius = radius;

      }



      /** Constructor */

      public Circle() {

        super();

        radius = 0;

      }


      /** Constructor with a center and a Point p on the perimeter*/

      public Circle(Point center, Point p) {

        super(center);

        this.radius=center.distance(p);

      }



      /** Area */

      publicdouble area(){

        return (2*radius*radius*Math.PI);

      }



      /** Radius */

      public double radius(){

            return radius;

          }


      public void setDashed(boolean dashedStroke){

          this.dashedStroke = dashedStroke;

         

      }




      /** Perimeter */

      public double perimeter() {

        return (2*radius*Math.PI);

      }



      /** toString */

      public String toString(){

        return ("Circle at " + position() + " with radius="+radius );

      }



      /** clone */

      public Object clone(){

        return new Circle((Point) anchor.clone(),radius);

      }



      /** equals */

      public boolean equals(Object pp){

        if(pp instanceof Circle) {

          Circle p = (Circle) pp;

          return (anchor.equals(p.anchor) && p.radius==radius);

        }else{

          returnfalse;

        }      

      }



     public void paint(Graphics g) {

        Graphics2D g2 = (Graphics2D)g;

       

       

        //Farbe aendern bei Auswahl

        /*if (selected == true) g2.setColor(new Color(255,0,0));    else*/

          g2.setColor(new Color(0,0,0));

         

        //Platziert wird ueber setBounds in GeoPainter, daher hier einfach Ecke links oben

        if(dashedStroke) g2.setStroke(dashed);

        g2.drawOval(0, 0, 2*(int)radius(), 2*(int)radius());

         

        if(fillColor){

            g2.setColor(newShapeColor); //wie kriege ich die farbe aus dem color choose

        }

         

         

     }





    }
     


    setter habe ich auch schon probiert (wollte mir die Schreiberei ersparen). ich hab aber die selbe exception bekommen wie in diesem code. ich hab noch viel mehr klassen. stell ich gerne alle rauf wenn dir das beim helfen hilft :)
     
  5. magdaStone
    magdaStone Neues Mitglied
    Okay, hab jetzt setter eingesetzt. Bekomme jedoch wieder nur eine Fehlermeldung...

    Code (Java):

    package panelzeug;

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JColorChooser;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    import shapezeug.*;
    import forgui.*;

    public class CirclePanel extends JPanel implements ActionListener{
       //static GeoPainterEK painter; //die übergabe von GeoPainter wurde an circle/rectPanel übergeben
       
       Painter_Uebung_3_EK topLevel;
       GeoPainterEK myPainter;
       Circle circle;
       public JTextField xCircle, yCircle, radius;
       public JButton submitCircle;
       boolean activated;
       
        public JRadioButton strokeColBtn = new JRadioButton("Stroke Color");
        public JRadioButton fillColBtn = new JRadioButton("Fill Color");
        public JRadioButton standardStrokeBtn = new JRadioButton("Standard Stroke");
        public JRadioButton wideStrokeBtn = new JRadioButton("Wide Stroke");
        public JRadioButton dashedStrokeBtn = new JRadioButton("Dashed Stroke");
       
       
       
        boolean fillColState = false;
        boolean dashedState = false;
       
       
       
       JPanel toolPanel = new JPanel();
       JPanel farbPanel = new JPanel();
      JPanel btnPanel = new JPanel();
      JPanel textPanel = new JPanel();
       
      JColorChooser farbAuswahl = new JColorChooser();
         
       
       
       public CirclePanel(Painter_Uebung_3_EK topLevel, GeoPainterEK myPainter, Circle circle){
         this.topLevel = topLevel;
         this.myPainter = myPainter;
         this.circle = circle;
         
         
         textPanel.setPreferredSize(new Dimension(150,200));
         setBackground(Color.yellow);
         farbPanel.add(farbAuswahl);
         
         //befüllen des Toolpanels mit den Labels usw
         setPreferredSize(new Dimension(180,190));        
        JLabel xCircLabel = new JLabel ("xCoordinate");
        xCircle = new JTextField(10);
        JLabel yCircLabel = new JLabel ("yCoordinate");
        yCircle = new JTextField(10);
        JLabel radiusCircLabel = new JLabel ("Radius");
        radius = new JTextField(10);
         
        textPanel.add(xCircLabel);
        textPanel.add(xCircle);
        textPanel.add(yCircLabel);
        textPanel.add(yCircle);
        textPanel.add(radiusCircLabel);
        textPanel.add(radius);
        submitCircle = new JButton ("Draw Circle");
        submitCircle.addActionListener(topLevel);    
        textPanel.add(submitCircle);
         
        add(textPanel);

        //befüllen des farb u btn panel mit chooser und btn
       
        strokeColBtn.addActionListener(this);
        fillColBtn.addActionListener(this);
        standardStrokeBtn.addActionListener(this);
        wideStrokeBtn.addActionListener(this);
        dashedStrokeBtn.addActionListener(this);
         
         
         btnPanel.add(strokeColBtn);
         btnPanel.add(fillColBtn);
         btnPanel.add(standardStrokeBtn);
         btnPanel.add(wideStrokeBtn);
         btnPanel.add(dashedStrokeBtn);

         add(farbPanel);
         add(btnPanel);
         
         
         
         
       }
       
       
       public void actionPerformed(ActionEvent e){
         
         //circle.dashedStroke = circlePanel.dashedStrokeBtn.isSelected();
         //System.out.println(circle.dashedStroke);
         
         if(e.getSource() instanceof JRadioButton){  // hier die einzelnen Booleans umschalten um sie dann direkt beim Zeichnen ausführen zu können
           JRadioButton radioButton = (JRadioButton)e.getSource();

           
           
           if(radioButton.getText() =="Stroke Color") {
              strokeColBtn.setText("test123");
             
               
             
          }
           if(radioButton.getText() =="Dashed Stroke") {//circle.dashedStroke = true; //fehlerhaft
              dashedStrokeBtn.setText("set Dashed Stroke");
              this.dashedState=!dashedState;
              //circle.dashedStroke= dashedStrokeBtn.isSelected();
              circle.setDashed(dashedState);
               
               
          }
           if(radioButton.getText() =="Fill Color") {
              fillColBtn.setText("setFillColor");
              circle.setFillColor(fillColState);
             
          }
           if(radioButton.getText() =="Standard Stroke") {
              standardStrokeBtn.setText("test123");
             
          }
           if(radioButton.getText() =="Wide Stroke") {
              wideStrokeBtn.setText("test123");
             
          }
         
           
       }
    }}
     
    Code (Java):

    package shapezeug;
    import java.awt.BasicStroke;

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Stroke;
    import java.awt.event.MouseEvent;

    import forgui.GeoPainterEK;
    import forgui.Painter_Uebung_3_EK;
    import panelzeug.*;

    /** A Circle using the Classes Point and Shape*/
    public class Circle extends Shape {

      /** the radius for the circle */
      protected double radius;
     
      //für die gestrichelte form
      final static float dash1[] = {10.0f};
      final static BasicStroke dashed =  new BasicStroke(1.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,10.0f, dash1, 0.0f);  
      private boolean dashedStroke = false;
      private boolean wideStroke = false;
      private boolean standardStroke = false;
      private boolean fillColor = false;
      private boolean strokeColor = false;
     
      private boolean state;
      Color newShapeColor;


      /** Constructor */
      public Circle(Point center, double radius) {
      super(center);
      this.radius = radius;
      }

      /** Constructor */
      public Circle() {
      super();
      radius = 0;
      }
     
      /** Constructor with a center and a Point p on the perimeter*/
      public Circle(Point center, Point p) {
      super(center);
      this.radius=center.distance(p);
      }

      /** Area */
      public double area(){
      return (2*radius*radius*Math.PI);
      }

      /** Radius */
      public double radius(){
        return radius;
        }
     
      public void setDashed(boolean state){
        this.dashedStroke = state;
         
      }
      public void setFillColor(boolean state){
        this.fillColor = state;
      }

     
      /** Perimeter */
      public double perimeter() {
      return (2*radius*Math.PI);
      }

      /** toString */
      public String toString(){
      return ("Circle at " + position() + " with radius="+radius );
      }

      /** clone */
      public Object clone(){
      return new Circle((Point) anchor.clone(),radius);
      }

      /** equals */
      public boolean equals(Object pp){
      if(pp instanceof Circle) {
      Circle p = (Circle) pp;
      return (anchor.equals(p.anchor) && p.radius==radius);
      }else{
      return false;
      }  
      }
     public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
       
       
       //Farbe aendern bei Auswahl
        /*if (selected == true) g2.setColor(new Color(255,0,0));   else*/
        g2.setColor(new Color(0,0,0));
       
        //Platziert wird ueber setBounds in GeoPainter, daher hier einfach Ecke links oben
        if(dashedStroke) g2.setStroke(dashed);
        if(fillColor) g2.setBackground(Color.PINK);
        g2.drawOval(0, 0, 2*(int)radius(), 2*(int)radius());
       
       if(fillColor){
         g2.setColor(newShapeColor); //wie kriege ich die farbe aus dem color choose
       }
       
       
     }


    }
     
    Code (Java):

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
       at panelzeug.CirclePanel.actionPerformed(CirclePanel.java:133)
       at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
       at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
       at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
       at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:308)
       at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
       at java.awt.Component.processMouseEvent(Component.java:6535)
       at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
       at java.awt.Component.processEvent(Component.java:6300)
       at java.awt.Container.processEvent(Container.java:2236)
       at java.awt.Component.dispatchEventImpl(Component.java:4891)
       at java.awt.Container.dispatchEventImpl(Container.java:2294)
       at java.awt.Component.dispatchEvent(Component.java:4713)
       at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
       at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
       at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
       at java.awt.Container.dispatchEventImpl(Container.java:2280)
       at java.awt.Window.dispatchEventImpl(Window.java:2750)
       at java.awt.Component.dispatchEvent(Component.java:4713)
       at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
       at java.awt.EventQueue.access$500(EventQueue.java:97)
       at java.awt.EventQueue$3.run(EventQueue.java:709)
       at java.awt.EventQueue$3.run(EventQueue.java:703)
       at java.security.AccessController.doPrivileged(Native Method)
       at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
       at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
       at java.awt.EventQueue$4.run(EventQueue.java:731)
       at java.awt.EventQueue$4.run(EventQueue.java:729)
       at java.security.AccessController.doPrivileged(Native Method)
       at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
       at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
       at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
       at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
       at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
       at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
       at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
       at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
     
     
  6. Meniskusschaden
    Meniskusschaden Bekanntes Mitglied
    Zunächst mal vergleicht man Strings nicht mit ==, sondern mit equals.

    Dein Quellcode und deine Fehlermeldung passen nicht richtig zusammen, denn Zeile 133 sieht so aus:
    Code (Java):
    if(radioButton.getText() =="Standard Stroke") {
    Da eine NullPointer-Exception geworfen wird, würde das bedeuten, dass radioButton null referenziert. Das könnte zwar tatsächlich der Fehler sein, aber dann müsste bereits in den vorhergehenden Zeilen eine NullPointer-Exception geworfen werden.

    Wahrscheinlich hast du Quellcode und Fehlermeldung nicht zum selben Zeitpunkt kopiert, so dass sich eines davon noch mal geändert hat.
     
  7. magdaStone
    magdaStone Neues Mitglied
    Oh, das tut mir leid. Poste es gleich nochmal :(

    Code (Java):

    package panelzeug;

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JColorChooser;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    import shapezeug.*;
    import forgui.*;

    public class CirclePanel extends JPanel implements ActionListener{
        //static GeoPainterEK painter; //die übergabe von GeoPainter wurde an circle/rectPanel übergeben
       
        Painter_Uebung_3_EK topLevel;
        GeoPainterEK myPainter;
        Circle circle;
        public JTextField xCircle, yCircle, radius;
        public JButton submitCircle;
        boolean activated;
       
        public JRadioButton strokeColBtn = new JRadioButton("Stroke Color");
        public JRadioButton fillColBtn = new JRadioButton("Fill Color");
        public JRadioButton standardStrokeBtn = new JRadioButton("Standard Stroke");
        public JRadioButton wideStrokeBtn = new JRadioButton("Wide Stroke");
        public JRadioButton dashedStrokeBtn = new JRadioButton("Dashed Stroke");
         
       
         
        boolean fillColState = false;
        boolean dashedState = false;
         
         
       
        JPanel toolPanel = new JPanel();
        JPanel farbPanel = new JPanel();
        JPanel btnPanel = new JPanel();
        JPanel textPanel = new JPanel();
       
        JColorChooser farbAuswahl = new JColorChooser();
         
       
       
        public CirclePanel(Painter_Uebung_3_EK topLevel, GeoPainterEK myPainter, Circle circle){
            this.topLevel = topLevel;
            this.myPainter = myPainter;
            this.circle = circle;
           
           
            textPanel.setPreferredSize(new Dimension(150,200));
            setBackground(Color.yellow);
            farbPanel.add(farbAuswahl);
           
            //befüllen des Toolpanels mit den Labels usw
            setPreferredSize(new Dimension(180,190));              
           JLabel xCircLabel = new JLabel ("xCoordinate");
           xCircle = new JTextField(10);
           JLabel yCircLabel = new JLabel ("yCoordinate");
           yCircle = new JTextField(10);
           JLabel radiusCircLabel = new JLabel ("Radius");
           radius = new JTextField(10);
             
           textPanel.add(xCircLabel);
           textPanel.add(xCircle);
           textPanel.add(yCircLabel);
           textPanel.add(yCircle);
           textPanel.add(radiusCircLabel);
           textPanel.add(radius);
           submitCircle = new JButton ("Draw Circle");
           submitCircle.addActionListener(topLevel);          
           textPanel.add(submitCircle);
           
           add(textPanel);

           //befüllen des farb u btn panel mit chooser und btn
         
           strokeColBtn.addActionListener(this);
           fillColBtn.addActionListener(this);
           standardStrokeBtn.addActionListener(this);
           wideStrokeBtn.addActionListener(this);
           dashedStrokeBtn.addActionListener(this);
         
           
            btnPanel.add(strokeColBtn);
            btnPanel.add(fillColBtn);
            btnPanel.add(standardStrokeBtn);
            btnPanel.add(wideStrokeBtn);
            btnPanel.add(dashedStrokeBtn);

            add(farbPanel);
            add(btnPanel);
           
           
           
           
        }
       
       
        public void actionPerformed(ActionEvent e){
           
            //circle.dashedStroke = circlePanel.dashedStrokeBtn.isSelected();
            //System.out.println(circle.dashedStroke);
           
            if(e.getSource() instanceof JRadioButton){  // hier die einzelnen Booleans umschalten um sie dann direkt beim Zeichnen ausführen zu können
                JRadioButton radioButton = (JRadioButton)e.getSource();

                     
                 
                if(radioButton.getText() =="Stroke Color") {
                    strokeColBtn.setText("test123");
                   
                   
                 
                  }
                if(radioButton.getText() =="Dashed Stroke") {//circle.dashedStroke = true; //fehlerhaft
                    dashedStrokeBtn.setText("set Dashed Stroke");
                    this.dashedState=!dashedState;
                    //circle.dashedStroke= dashedStrokeBtn.isSelected();
                    circle.setDashed(dashedState);
                   
                   
                  }
                if(radioButton.getText() =="Fill Color") {
                    fillColBtn.setText("setFillColor");
                    circle.setFillColor(fillColState);
                 
                  }
                if(radioButton.getText() =="Standard Stroke") {
                    standardStrokeBtn.setText("test123");
                 
                  }
                if(radioButton.getText() =="Wide Stroke") {
                    wideStrokeBtn.setText("test123");
                 
                  }
           
                 
        }
    }}
     
    Code (Java):

    package shapezeug;

    import java.awt.BasicStroke;



    import java.awt.Color;

    import java.awt.Graphics;

    import java.awt.Graphics2D;

    import java.awt.Stroke;

    import java.awt.event.MouseEvent;



    import forgui.GeoPainterEK;

    import forgui.Painter_Uebung_3_EK;

    import panelzeug.*;



    /** A Circle using the Classes Point and Shape*/

    publicclassCircleextends Shape {



      /** the radius for the circle */

      protecteddoubleradius;


      //für die gestrichelte form

      final static float dash1[] = {10.0f};

      final static BasicStroke dashed =  new BasicStroke(1.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,10.0f, dash1, 0.0f);  

      privatebooleandashedStroke = false;

      privatebooleanwideStroke = false;

      privatebooleanstandardStroke = false;

      privatebooleanfillColor = false;

      privatebooleanstrokeColor = false;


      privatebooleanstate;

      Color newShapeColor;





      /** Constructor */

      public Circle(Point center, double radius) {

        super(center);

        this.radius = radius;

      }



      /** Constructor */

      public Circle() {

        super();

        radius = 0;

      }


      /** Constructor with a center and a Point p on the perimeter*/

      public Circle(Point center, Point p) {

        super(center);

        this.radius=center.distance(p);

      }



      /** Area */

      publicdouble area(){

        return (2*radius*radius*Math.PI);

      }



      /** Radius */

      public double radius(){

            return radius;

          }


      public void setDashed(boolean state){

          this.dashedStroke = state;

         

      }

      public void setFillColor(boolean state){

          this.fillColor = state;

      }




      /** Perimeter */

      public double perimeter() {

        return (2*radius*Math.PI);

      }



      /** toString */

      public String toString(){

        return ("Circle at " + position() + " with radius="+radius );

      }



      /** clone */

      public Object clone(){

        return new Circle((Point) anchor.clone(),radius);

      }



      /** equals */

      public boolean equals(Object pp){

        if(pp instanceof Circle) {

          Circle p = (Circle) pp;

          return (anchor.equals(p.anchor) && p.radius==radius);

        }else{

          returnfalse;

        }      

      }



     public void paint(Graphics g) {

        Graphics2D g2 = (Graphics2D)g;

       

       

        //Farbe aendern bei Auswahl

        /*if (selected == true) g2.setColor(new Color(255,0,0));    else*/

          g2.setColor(new Color(0,0,0));

         

        //Platziert wird ueber setBounds in GeoPainter, daher hier einfach Ecke links oben

        if(dashedStroke) g2.setStroke(dashed);

        if(fillColor) g2.setBackground(Color.PINK);

        g2.drawOval(0, 0, 2*(int)radius(), 2*(int)radius());

         

        if(fillColor){

            g2.setColor(newShapeColor); //wie kriege ich die farbe aus dem color choose

        }

         

         

     }





    }


     
    Code (Java):

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

        at panelzeug.CirclePanel.actionPerformed(CirclePanel.java:124)

        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)

        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)

        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)

        at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:308)

        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)

        at java.awt.Component.processMouseEvent(Component.java:6535)

        at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)

        at java.awt.Component.processEvent(Component.java:6300)

        at java.awt.Container.processEvent(Container.java:2236)

        at java.awt.Component.dispatchEventImpl(Component.java:4891)

        at java.awt.Container.dispatchEventImpl(Container.java:2294)

        at java.awt.Component.dispatchEvent(Component.java:4713)

        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)

        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)

        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)

        at java.awt.Container.dispatchEventImpl(Container.java:2280)

        at java.awt.Window.dispatchEventImpl(Window.java:2750)

        at java.awt.Component.dispatchEvent(Component.java:4713)

        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)

        at java.awt.EventQueue.access$500(EventQueue.java:97)

        at java.awt.EventQueue$3.run(EventQueue.java:709)

        at java.awt.EventQueue$3.run(EventQueue.java:703)

        at java.security.AccessController.doPrivileged(Native Method)

        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)

        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)

        at java.awt.EventQueue$4.run(EventQueue.java:731)

        at java.awt.EventQueue$4.run(EventQueue.java:729)

        at java.security.AccessController.doPrivileged(Native Method)

        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)

        at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)

        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)

        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)

        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)

        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)


     
    sooooo, hoffe das passt jetzt so:oops:
     
  8. Meniskusschaden
    Meniskusschaden Bekanntes Mitglied
    Dann tritt der Fehler in Zeile 124 auf, also hier:
    Code (Java):
    circle.setDashed(dashedState);
    An der Stelle gibt es offenbar noch kein Circle-Objekt, auf das circle verweist. Im geposteten Programmcode wird circle aktuell nur im Konstruktor von CirclePanel gesetzt. Man sieht in deinem Code nicht, wie du das CirclePanel erzeugst, aber dort übergibst du offenbar kein Circle-Objekt.
     
    magdaStone gefällt das.
  9. magdaStone
    magdaStone Neues Mitglied
    oh... ich dachte mir schon, dass ich einen blöden Fehler gemacht habe. Tatsächlich habe ich vergessen in der Painter class das Circle circle Objekt zu implementieren. Vielen Dank, soweit funktioniert es mal, bzw. krieg ich keine NullPointerException. Jetzt kann ich endlich weiterarbeiten.

    Dankedankedanke und schönen Sonntag noch!:)

    mlG
    Magdalena
     
Die Seite wird geladen...

Logikproblem Umschalten von booleans in anderer Klasse - Ähnliche Themen

Logikproblem Umschalten von booleans in anderer Klasse
Logikproblem Umschalten von booleans in anderer Klasse im Forum Java Basics - Anfänger-Themen
Logikprobleme beim Programmieren
Logikprobleme beim Programmieren im Forum Plauderecke
Logikproblem bzw. Denkfehler
Logikproblem bzw. Denkfehler im Forum Allgemeine Java-Themen
(Java3D) Einen Faden programmieren - Logikproblem
(Java3D) Einen Faden programmieren - Logikproblem im Forum Allgemeine Java-Themen
if-else Logikproblem
if-else Logikproblem im Forum Java Basics - Anfänger-Themen
Thema: Logikproblem Umschalten von booleans in anderer Klasse