Logikproblem Umschalten von booleans in anderer Klasse

magdaStone

Mitglied
Hey, da bin ich wieder. Wie bei meinem vorherigen Beitrag schlage ich mich noch immer mit diesen blöden settern rum um in meiner Circle Klasse via JRadioButtons booleans welche in meiner Circle Klasse sind umzuschalten. Ja... ich kriege zwar keine Fehlermeldungen, aber es funktioniert einfach nicht. Ich bin kurz vor der Explosion. Mir ist einfach nicht klar wieso ich sie nicht umschalten kann und bin wirklich schon mit meinem Latein am Ende :confused:

vl hab ich auch eine Riesen-Wissenslücke was Polymorphismus betrifft.

Java:
package forgui;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import shapezeug.*;
import panelzeug.*;



/* EProg  Musterloesung Aufgabe 5, WS 2014, Markus Seidl */
public class Painter_Uebung_3_EK extends JPanel implements ActionListener{   
    //Deklaration der Instanzvariablen
   
    CirclePanel circlePanel;
    RectPanel rectPanel;
    JPanel toolPanel = new JPanel();
    Circle circle = new Circle(); //hier hatte ich die Implementierung vergessen! deswegen nullpointerexception!!! :(
    GeoPainterEK myPainter = new GeoPainterEK(this);
    boolean dashedStrokeTest = false;
   

    //Konstruktor
    Painter_Uebung_3_EK(/*GeoPainterEK painter*/) {
       
            toolPanel.setPreferredSize(new Dimension(600, 800));
            toolPanel.setBackground(Color.red);
            add(toolPanel);
           generateCirclePanel();
           generateRectPanel();
           myPainter.setPreferredSize(new Dimension(400, 400));   
           add(myPainter);  
           
    }
   
    /*public boolean getStandardStroke(){
        //return standardStroke;
       
    }*/
    public void generateCirclePanel()
    {
        this.circlePanel = new CirclePanel(this, myPainter, circle);
        circlePanel.setPreferredSize(new Dimension(600,800));
    }
    public void generateRectPanel()
    {
        this.rectPanel = new RectPanel(this, myPainter, null);
        rectPanel.setPreferredSize(new Dimension(600,800));
    }
   
   
    public JMenuBar createMenuBar() {
       
        //Create the menu bar.  Make it have a green background.
       JMenuBar greenMenuBar = new JMenuBar();
       greenMenuBar.setOpaque(true);
       greenMenuBar.setBackground(new Color(154, 165, 127));
       greenMenuBar.setPreferredSize(new Dimension(200, 20));      
       
       //Menupunkt  hinzufuegen
       JMenu menu_file = new JMenu("Zeichnen");
       greenMenuBar.add(menu_file);
     
       
       //Menueeintraege hinzufuegen

       JMenuItem item_kreis = new JMenuItem("Kreis");
       item_kreis.addActionListener(this);    
       JMenuItem item_rechteck = new JMenuItem("Rechteck");
       item_rechteck.addActionListener(this);    

       menu_file.add(item_kreis);
       menu_file.add(item_rechteck);
       
       // JMenuBar grrenMenuBar zur�ckgeben
       return greenMenuBar;
       
    }
   
   
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     * Problem: Das muss eine statische Methode sein, um thread-save aufgerufen
     * werden zu koennen (siehe Methode main()).
     * Allerdings, eine statische Methode kann nicht mit den Instanzvariablen der Klasse interagieren.
     * Wir umgehen das, indem wir von JPanel erben, und dieses als ContentPane in unseren Frame geben.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Painter Uebung 5");
        frame.setPreferredSize(new Dimension(1500,800));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   
        /* JPanel MiniPhotoShop als ContentPane, this nicht m�gloch, umweg */      
        Painter_Uebung_3_EK ourTopLevelDemo = new Painter_Uebung_3_EK();        
       //Damit k�nnen wir jede Komponente in unserer Hauptklasse adden!
        frame.setContentPane(ourTopLevelDemo);
       
       
        //MenuBar setzen
        frame.setJMenuBar(ourTopLevelDemo.createMenuBar());
        ourTopLevelDemo.setBackground(Color.blue);
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
               
            }
        });
    }
   

   
    //Beim Toolpanelwechsel wird immer ein evtl. vorhandenes geloescht.
    //Deshalb buendeln wir die Funktonalitaet in eine Methode
    void removeSelectedTool ()  {
        if (toolPanel.getComponents().length > 0) {
            toolPanel.removeAll(); 
           
          }   
        revalidate();
        repaint();
    }
   
   
   
    @Override
    public void actionPerformed(ActionEvent e) {
       
       
        JMenuItem itemClicked;
        //Falls es ein MenuItem ist
        if (e.getSource() instanceof JMenuItem) {
            itemClicked = (JMenuItem)e.getSource();
            //Falls es das Item "Rot" war
           
            if (itemClicked.getText() == "Kreis") {
                 
                    removeSelectedTool();
                    circlePanel.submitCircle.setText("Draw Circle");
                    toolPanel.add(circlePanel);
                    revalidate();
                    repaint();
               
                }
            if (itemClicked.getText() == "Rechteck") {
                //Falls eine Komponente dort ist
                removeSelectedTool();
                rectPanel.submitRect.setText("Draw Rectangle");
                toolPanel.add(rectPanel);
               
                revalidate();
                repaint();

            }
        //Button   
        } else if (e.getSource() instanceof JButton){
            JButton buttonClicked = (JButton)e.getSource();
            if (buttonClicked.getText() == "Draw Circle") {
               
               
                               
                   
                int circX = Integer.parseInt(circlePanel.xCircle.getText().toString());
                int circY = Integer.parseInt(circlePanel.yCircle.getText().toString());
                int circRadius = Integer.parseInt(circlePanel.radius.getText().toString());
                if (((circX-circRadius) < 0) || ((circY-circRadius) < 0) ) System.out.println("Circle is outside of the painting area!!");
                else{
                    if(circlePanel.wideStrokeBtn.isSelected()) circle.setWideStroke(true); //das hier !!! :(
                    System.out.println("ist wideStrokeBtn selected? -->"+ circlePanel.wideStrokeBtn.isSelected()+"\n ist wideStroke positiv? -->" +circle.getWideStroke());

                    myPainter.addCircle(circX, circY, circRadius);
                    repaint();
                    removeSelectedTool();
                }
            } 
            if (buttonClicked.getText() == "Draw Rectangle") {
                int rectX =  Integer.parseInt(rectPanel.xRect.getText().toString());
                int rectY =  Integer.parseInt(rectPanel.yRect.getText().toString());
                int rectWidth =  Integer.parseInt(rectPanel.widthRect.getText().toString());
                int rectHeight =  Integer.parseInt(rectPanel.heightRect.getText().toString());
                if ((rectX < 0) || (rectY < 0) || (rectWidth < 0) || (rectHeight < 0)) System.out.println("Negative values are not allowed!");
                else {
               
                    myPainter.addRectangle(rectX,rectY,rectWidth,rectHeight);
                   
                    repaint();
                    removeSelectedTool();
                 
                }
            }    
             
           
        /*}else 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.isSelected()){
                      if(radioButton.getText() =="Dashed Stroke") {//circle.dashedStroke = true; //fehlerhaft
                      circlePanel.dashedStrokeBtn.setText("test123");
                      circle.dashedStroke= true;
                      }
                      else circle.dashedStroke = false;
                     
                      System.out.println("activated");
                  // }
        }
        elseif(radioButton.getText()=="Stroke Color"){
           
               
               
            }
            /*else if(radioButton.getText()=="Fill Color"){
               
               
                //System.out.println(circle.fillColor);
               
            }
            else if(radioButton.getText()=="Standard Stroke"){
                //standardStroke = true;
               
            }
            else if(radioButton.getText()=="Wide Stroke"){
                //wideStroke = true;
               
            }
           
           
        }else if (e.getSource() instanceof JColorChooser){
            /*if(){
                //GeoPainterEK.circle.newShapeColor = farbAuswahl.getColor();
            }*/
        }
    }}
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;



/** 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); 
  final static BasicStroke wideStrokeS = new BasicStroke(5.0f);
  private boolean dashedStroke= false;
  private boolean wideStroke;
  private boolean standardStroke = false;
  private boolean fillColor = false;
  private boolean strokeColor = false;
  private boolean state;
  Color newShapeColor;
  Color newStrokeColor;


  /** 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 setWideStroke(boolean wideStroke){ //um den neuen wideStroke wert zu bekommen
     this.wideStroke = wideStroke;
     
  }
  public boolean getWideStroke(){ //zum testen
     return wideStroke;
     
  }
  public void setShapeColor(Color newShapeColor){ //um die Farben zu bekommen
     this.newShapeColor = newShapeColor;
  }

  public void setStrokeColor(Color newStrokeColor){
     this.newStrokeColor = newStrokeColor;
  }

  /** 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 setDashed(boolean dashedStroke){
     this.dashedStroke = dashedStroke;
     }


 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));
     System.out.println("ist der wideStroke in Circle positive? -->" + wideStroke);
     if(this.wideStroke==true){
         g2.setStroke(wideStrokeS);
         
     }
     g2.drawOval(0, 0, 2*(int)radius(), 2*(int)radius());
     
    //Platziert wird ueber setBounds in GeoPainter, daher hier einfach Ecke links oben
    /*if(strokeColor&&dashedStroke) {
        g2.setPaint(newStrokeColor);
        g2.setStroke(dashed);
        g2.drawOval(0, 0, 2*(int)radius(), 2*(int)radius());
        //System.out.println(newStrokeColor);
         
    }
    if(dashedStroke) {
        g2.setStroke(dashed);}
    //System.out.println("Wert des Booleans zum Umschalten" + dashedStroke);
    if(wideStroke) g2.setStroke(wideStrokeS);
     
    if(fillColor) { // wollen wir eine Füllfarbe? wenn nicht... g2.setColor(newShapeColor); 
        g2.setColor(newShapeColor);
        g2.fillOval(0, 0, 2*(int)radius(), 2*(int)radius());
         
         
    }else {
        g2.drawOval(0, 0, 2*(int)radius(), 2*(int)radius());
    }*/
     
   
     
     
 }


}
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 = new 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().equals("Stroke Color")){
                strokeColBtn.setText("test123");
                circle.setStrokeColor(farbAuswahl.getColor());
               
               
             
              } 
            if(radioButton.getText().equals("Dashed Stroke")) {//circle.dashedStroke = true; //fehlerhaft
                dashedStrokeBtn.setText("set Dashed Stroke");
                this.dashedState=!dashedState;
                //circle.dashedStroke= true;
                //dashedState = true;
                circle.setDashed(dashedState);
                //System.out.println("Dashed State Boolean in Circle Panel" + dashedState);
                System.out.println("Wert von isSelected()" + dashedStrokeBtn.isSelected());
               
               
              } 
            if(radioButton.getText().equals("Fill Color")) {
                fillColBtn.setText("setFillColor");
                circle.setShapeColor(farbAuswahl.getColor());
                //circle.setFillColor(fillColState);
                //System.out.println(fillColState);
                System.out.println(farbAuswahl.getColor());
               
             
              } 
            if(radioButton.getText().equals("Standard Stroke")) {
                standardStrokeBtn.setText("test123");
             
              } 
            if(radioButton.getText().equals("Wide Stroke")) { probiere das in Painter_Uebung blabla direkt beim drücken des DrawButtons???
                wideStrokeBtn.setText("Wide Stroke set");
                //circle.setWideStroke(true); setter
                circle.setWideStroke(true);
             
              } */
       
             
    }
}}



hier meine Konsolenausgabe:
ist wideStrokeBtn selected? -->true
ist wideStroke positiv? -->true
ist der wideStroke in Circle positive? -->false
 

Ähnliche Java Themen

Neue Themen


Oben