webForumDet fria alternativet

addMouseListener(new MouseAdapter() vill inte fungera???

3 svar · 217 visningar · startad av dr_ll

dr_llMedlem sedan nov. 20046 inlägg
#1

Jag har ett program som den ena filen ritar pilar och de andra filen ritar boxar som man kan flytta och rotera runt.

Problemet ligger i den andra filen alltsa klassen GraphicArrow, dess addMouseListener vill inte fungera nar jag klickar pa musen, kan nagon tala om vad det ar som ar fel?

Har kommer koden:
klassen GraphicArrow

import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;

public class GraphicArrow extends JComponent
{

   GeneralPath arrow   =  new GeneralPath();
   Stroke dashStroke   =  new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                             10f, new float[] {4f, 4f}, 0f);
   Paint gradientPaint =  new GradientPaint(-30, 0, Color.red, 10, 0, Color.yellow);
   private int xA, yA;

   public GraphicArrow() {
      arrow.moveTo(-20, -10);
   }

   public GraphicArrow(int x, int y)
   {
      arrow.moveTo(-20, -10);
      arrow.lineTo(0, -10);
      arrow.lineTo(0, -20);
      arrow.lineTo(20, 0);
      arrow.lineTo(0, 20);
      arrow.lineTo(0, 10);
      arrow.lineTo(-20, 10);
      arrow.lineTo(-20, -10);

      xA = x;
      yA = y;

      addMouseListener(new MouseAdapter()
      {
         Rectangle hitRect = new Rectangle(0, 0, 5, 5);

         public void mouseClicked(MouseEvent e)
         {
            Graphics2D g = (Graphics2D) getGraphics();
            AffineTransform xform = new AffineTransform();

            // puts hit rectangle 2 pixels around click point
            hitRect.x = e.getX() - 2;
            hitRect.y = e.getY() - 2;

            Rectangle bounds = arrow.getBounds();

            if (g.hit(hitRect, arrow, false))
                System.out.println("Traff i pilen ");
         }
      });
    }

   public void paintComponent(Graphics _g) 
   {
      super.paintComponent(_g);
      Graphics2D g = (Graphics2D) _g;

      // Translates and rotates the arrow
      g.translate(xA, yA);
      g.rotate(Math.PI / 1);

      // Fills and outlines the arrow
      g.setPaint(gradientPaint);
      g.fill(arrow);
      g.setPaint(Color.BLACK);
      g.draw(arrow);

      // Translates and rotates back to normal
      g.rotate(Math.PI / -1);
      g.translate(-xA, -yA);
   }

   public static void main(String args[]) {
      JFrame frame = new JFrame("Arrow");
      frame.getContentPane().add(new GraphicArrow());
      frame.setBackground(Color.white);
      frame.setSize(new Dimension(400, 350));
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
    }
}
dr_llMedlem sedan nov. 20046 inlägg
#2

///////////////////////////////////////Den andra klassen:

///////////////////////////////////////Den andra klassen:

 
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.*;

public class Box extends JApplet
{
    Vector shape      = new Vector();
    Vector arrowArray = new Vector();
    int theta;

    class axel extends JPanel implements KeyListener
    {
        GeneralPath arrow   = new GeneralPath();
        Stroke dashStroke   = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10f, new float[] {10f, 10f}, 5f);
        Paint gradientPaint = new GradientPaint( -30, 0, Color.red, 10, 0, Color.yellow);

        boolean delete_box, addSquare, addArrow, dragging;
        axel selectedRect;
        RectangularShape boxen;
        AffineTransform xform;

        RectSelector action;
        String texten;

        public axel()
        {
            shape.addElement(new axel("Text1", 40, 50, 50, 60));
            shape.addElement(new axel("Text2", 150, 100, 50, 60));
            shape.addElement(new axel("Text3", 40, 150, 50, 60));

            setBackground(Color.white);

            action = new RectSelector(this);
            addMouseListener(action);
            addMouseMotionListener(action);
            addKeyListener(this);
        }

        public axel(String text, int x, int y, int w, int h)
        {
            texten = text;

            boxen  = new RoundRectangle2D.Double(x, y, w, h, 20, 20);
            xform  = new AffineTransform();
            theta  = 5;
        }

        public void rita(Graphics2D g2)
        {
            double cx = boxen.getCenterX();
            double cy = boxen.getCenterY();

            int mus_x =(int) cx;
            int mus_y =(int) cy;

            AffineTransform orig = g2.getTransform();
            if (!xform.isIdentity())
                g2.setTransform(xform);

            g2.fill(boxen);
            g2.setColor(Color.black);
            g2.setFont(new Font("Helvetica", Font.BOLD, 12));
            g2.drawString(texten, mus_x + -11, mus_y + 5);
            g2.draw(boxen);
            g2.setTransform(orig);
        }

        public void paintComponent(Graphics g)
        {
           super.paintComponent(g);
           Graphics2D g2 = (Graphics2D) g;

           Image bild = getImage(getDocumentBase(), "images.jpg");
           g2.drawImage(bild, 0, 0, this);

           g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                               RenderingHints.VALUE_ANTIALIAS_ON);
           //Draw the Shapes
           for (int i = 0; i < shape.size(); i++)
           {
               axel rsh = (axel) shape.get(i);

               if (rsh == selectedRect)
               {
                   g2.setStroke(dashStroke);
                   g2.setPaint(Color.green);
               }
               else
               {
                   g2.setStroke(new BasicStroke(0));
                   g2.setPaint(Color.yellow);
               }
               rsh.rita(g2);
           }

           //Draw the Arrows
           for (int i = 0; i < arrowArray.size(); i++)
           {
               GraphicArrow arrowen = (GraphicArrow) arrowArray.get(i);
               arrowen.paintComponent(g);
           }

        }

        public void resizen()
        {
            Rectangle r      = boxen.getBounds();
            double    width  = boxen.getWidth()+2;
            double    height = boxen.getHeight()+2;

            boxen.setFrame(r.x, r.y, width, height);
        }

        public void rotateRect()
        {
            double cx = boxen.getCenterX();
            double cy = boxen.getCenterY();
            theta = theta - 5;
            xform.setToRotation(Math.toRadians(theta), cx, cy);
        }

        public void rotateRect_right()
        {
            double cx = boxen.getCenterX();
            double cy = boxen.getCenterY();
            theta = theta + 5;
            xform.setToRotation(Math.toRadians(theta), cx, cy);
        }

        public void keyPressed(KeyEvent e) {
            System.out.println("Test1");
            if (e.isShiftDown()) {
                System.out.println("Test3");
            }
            if (e.getKeyCode() == KeyEvent.VK_DELETE) {
                System.out.println("Test2");
            }
        }

        public void keyReleased(KeyEvent e) {
        }

        public void keyTyped(KeyEvent e) {
        }

        class RectSelector extends MouseInputAdapter
        {
            axel rotationPanel;
            RectangularShape selectedShape;
            axel rs;
            Point offset;
            final int size = 50;

            public RectSelector(axel fp)
            {
                rotationPanel  = fp;
                offset         = new Point();
                delete_box     = false;
                addSquare      = false;
                addArrow       = false;
                dragging       = false;

            }

            public void mousePressed(MouseEvent e)
            {
                Point p = e.getPoint();

                //New Box creates
                if (addSquare)
                {
                  shape.addElement(new axel("Another text", p.x, p.y, 50, 60));
                  rotationPanel.repaint();
                  addSquare = false;
                  return;
                }

                //New Arrow creates
                if (addArrow)
                {
                  arrowArray.addElement(new GraphicArrow(p.x, p.y));
                  rotationPanel.repaint();
                  addArrow = false;
                  return;
                }

                for (int i = 0; i < shape.size(); i++)
                {
                    rs = (axel) shape.get(i);

                    if (rs.boxen.contains(p))
                    {
                        if (delete_box)
                        {
                            shape.removeElementAt(i);
                            delete_box = false;
                            repaint();
                        }

                        Rectangle r = rs.boxen.getBounds();

                        offset.x = p.x - r.x;
                        offset.y = p.y - r.y;
                        selectedRect = rs;
                        selectedShape = rs.boxen;
                        dragging = true;
                        repaint();
                    }
                    //else
                    //    System.out.println("press does not work " + delete_box + i + rs.boxen.contains(p));
                }
            }

            public void mouseReleased(MouseEvent e)
            {
                int x = e.getX();
                int y = e.getY();
                selectedShape = null;
                dragging = false;
                setCursor(new Cursor(Cursor.DEFAULT_CURSOR) );
                //Graphics2D g3  = (Graphics2D) getGraphics();
                //g3.setStroke(new BasicStroke(0));
                //g3.setPaint(Color.yellow);
            }

            public void mouseDragged(MouseEvent e)
            {
                if (dragging)
                {
                    Point p = e.getPoint();
                    if (selectedShape != null)
                    {
                        setCursor(new Cursor(Cursor.MOVE_CURSOR) );
                        Rectangle r = selectedShape.getBounds();

                        System.out.println("dragging " + p + p.x + " " + p.y);

                        r.x = p.x - offset.x;
                        r.y = p.y - offset.y;
                        selectedShape.setFrame(r);
                        repaint();
                    }
                }
            }

            public void mouseMoved(MouseEvent e)
            {

            }

        }
dr_llMedlem sedan nov. 20046 inlägg
#3

public JPanel panelen()
{
final JButton rotate_left = new JButton("Rotate left");
final JButton rotate_right = new JButton("Rotate right");
final JButton arrow = new JButton("New Arrow");
final JButton box = new JButton("New Box");
final JButton delete = new JButton("Delete");
final JButton resize = new JButton("Resize");

        rotate_left.setCursor(new Cursor(Cursor.HAND_CURSOR) );
        rotate_right.setCursor(new Cursor(Cursor.HAND_CURSOR) );
        arrow.setCursor(new Cursor(Cursor.HAND_CURSOR) );
        box.setCursor(new Cursor(Cursor.HAND_CURSOR) );
        delete.setCursor(new Cursor(Cursor.HAND_CURSOR) );
        resize.setCursor(new Cursor(Cursor.HAND_CURSOR) );

        rotate_left.addActionListener(new ActionListener()
        {
           public void actionPerformed(ActionEvent e)
           {
              if (e.getSource() == rotate_left && selectedRect != null)
              {
                 selectedRect.rotateRect();
                 repaint();
              }
           }
        });

        rotate_right.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e)
           {
              if (e.getSource() == rotate_right && selectedRect != null)
              {
                 selectedRect.rotateRect_right();
                 repaint();
              }
           }
        });

        arrow.addActionListener(new ActionListener()
        {
           public void actionPerformed(ActionEvent e)
           {
              addArrow = (e.getSource() == arrow);
           }
        });

        box.addActionListener(new ActionListener()
        {
           public void actionPerformed(ActionEvent e)
           {
              addSquare = (e.getSource() == box && selectedRect != null);
           }
        });

        delete.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
               delete_box = (e.getSource() == delete);
            }
        });

        resize.addActionListener(new ActionListener()
        {
           public void actionPerformed(ActionEvent e)
           {
              if (e.getSource() == resize && selectedRect != null)
              {
                 selectedRect.resizen();
                 repaint();
              }
           }
        });

        JPanel panel = new JPanel();
        panel.setBackground( Color.orange );
        panel.add(rotate_left);
        panel.add(rotate_right);
        panel.add(arrow);
        panel.add(box);
        panel.add(delete);
        panel.add(resize);
        return panel;
    }
}

public void init() {
    axel rp = new axel();
    Container cp = getContentPane();
    cp.setLayout(new BorderLayout());
    cp.add(rp.panelen(), "South");
    cp.add(rp);
}

public static void main(String\[\] args)
{
    JApplet applet = new Box();
    JFrame f = new JFrame("Test");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(applet);
    f.setSize(600, 400);
    f.setLocationRelativeTo(null);
    applet.init();
    f.setVisible(true);
}

}

dr_llMedlem sedan nov. 20046 inlägg
#4

det sista var resten av programmet som inte fick plats

144 ms totalt · 3 externa anrop · v20260731065814-full.30151723
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
142 ms — hämta tråd, inlägg och bilagor (db)