Graphic was falsch?

Status
Nicht offen für weitere Antworten.

Developer_X

Top Contributor
hi, was mache ich hier falsch, dass sich die Farbe nicht ändert, wenn ich auf den button gelb klicke?
Code:
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

/* -------------------------------
   Main Frame in program PaintShop
   -------------------------------*/
   public class PaintShop extends JFrame
                          implements MouseListener, MouseMotionListener {

   private JButton      button1;
   private JButton      button2;
   private JButton      button3;
   private FlowLayout   layout;

   private Color color = Color.blue;
   private DrawingPanel drawingPanel;

   public PaintShop()
   {
      super( "PaintShop first version" );

      layout = new FlowLayout();

      Container c = getContentPane( );
      c.setLayout ( layout );

      button1      = new JButton("Yellow");
      button2      = new JButton("Punkte");
      button3      = new JButton("Linie");

      drawingPanel = new DrawingPanel(0,0);

      addMouseListener(this);
      addMouseMotionListener(this);

      c.add(button1);
      c.add(button2);
      c.add(button3);
      c.add(drawingPanel);
      
      button1 = new JButton();
	  button1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e)
		{  
		 color = Color.yellow;}});
	    

      setSize( 750, 500 );
      show();
   }

/* ------------------------------
   Listening for mouse activities
   ------------------------------*/
   public void mouseClicked ( MouseEvent e )
   {
   }

   public void mouseEntered ( MouseEvent e )
   {
   }

   public void mouseExited ( MouseEvent e )
   {
   }

   public void mousePressed ( MouseEvent e )
   {
      drawingPanel.setPaint();
   }

   public void mouseReleased ( MouseEvent e )
   {
      drawingPanel.setNotPaint();
   }

/* -----------------------------
   Listening for mouse movements
   -----------------------------*/
   public void mouseMoved ( MouseEvent e )
   {
   }

   public void mouseDragged ( MouseEvent e )
   {
      drawingPanel.setX( e.getX() );
      drawingPanel.setY( e.getY() );
      drawingPanel.paintComponent(getGraphics());
   }

/* ---------------------------------------
   Class DrawingPanel
   This Panel is used to draw a picture on
   ---------------------------------------*/
   private class DrawingPanel extends JPanel {
      private int       xValue;
      private int       yValue;
      private boolean   paint;

      public DrawingPanel(int x, int y) {
         xValue   = x;
         yValue   = y;
         paint    = false;
      }

      public void setX(int x) {
         xValue = x;
      }

      public void setY(int y) {
         yValue = y;
      }

      public void setPaint() {
         paint = true;
      }

      public void setNotPaint() {
         paint = false;
      }

      public void paintComponent(final Graphics g)
      {
    	  g.setColor(color);
    	  g.drawLine(xValue, yValue, 4, 4);
    	  
      }
      public void update(Graphics g)
      {}
   }

/* -----------
   MainProgram
   -----------*/
   public static void main( String args[] )
   {
      PaintShop app = new PaintShop();

      app.addWindowListener(
         new WindowAdapter()
         {
            public void windowClosing( WindowEvent e )
            {
               System.exit( 0 );
            }
         }
      );
   }
}
 
Wie Andre schon sagt... Schmeiss "button1 = new JButton();" raus... sonst wird der ActionListener nämlich diesem zugewiesen und nicht dem für "YELLOW".

mfg Spacerat
 
Bei Fragen, die IRGENDwas mit Zeichnen zu tun haben, habe ich mir angewöhnt, als erstes mit STRG+F eine Suche nach dem "getGraphics()" zu machen - hier wurde das (mal wieder) gefunden. VIelleicht hat es nichts mit deinem eigentlichen Problem zu tun, aber es ist mit an Sicherheit grenzender Wahrscheinlichkeit falsch...
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben