程序将识别某些关键输入而不识别其他输入。

时间:2015-05-18 09:52:49

标签: java jframe geometry keylistener mouselistener

程序应绘制用户制作的形状。当用户按下键时," c"应该能够将鼠标拖过屏幕画一个圆圈。当用户按下" r"他们应该绘制一个矩形。什么时候" l"按下一条线应该画出来。什么时候" o"压制成椭圆形。但是,只有关键" c"和" r"可以出于某种原因。如果没有输入使用输入,则程序应默认为圆形。无论如何紧迫" l"和" o"不画椭圆形或线条。

/**
 * @(#)DrawShapes.java
 *
 *
 * @author
 * @version 1.00 2015/5/17
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.awt.geom.Ellipse2D;
import java.awt.event.KeyEvent;


import javax.swing.JComponent;
import javax.swing.JFrame;

public class DrawShapes extends JFrame implements KeyListener{

    String key="";

   public void keyPressed(KeyEvent event)
    {
        char temp = event.getKeyChar();
        key = Character.toString(temp);

    }


 public void keyTyped (KeyEvent event) {}
    public void keyReleased (KeyEvent event) {}

  public DrawShapes()
  {
    this.setSize(300, 300);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.add(new PaintSurface(), BorderLayout.CENTER);
    this.setVisible(true);
    addKeyListener(this);
  }

  private class PaintSurface extends JComponent
  {
    ArrayList<Shape> shapes = new ArrayList<Shape>();

    Point startDrag, endDrag;

    public PaintSurface() {


      this.addMouseListener(new MouseAdapter()
      {
        public void mousePressed(MouseEvent e) {
          startDrag = new Point(e.getX(), e.getY());
          endDrag = startDrag;
          repaint();
        }

        public void mouseReleased(MouseEvent e) {
                  Shape r;


        if (key.equals("l"))
        {
             r = makeLine(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
        }



        if (key.equals("o"))
        {
             r = makeOval(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
        }

        if (key.equals("r"))
        {
             r = makeRectangle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
        }


        else
        {
            r = makeCircle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
        }

          shapes.add(r);
          startDrag = null;
          endDrag = null;
          repaint();
        }
      });


      this.addMouseMotionListener(new MouseMotionAdapter()
      {
        public void mouseDragged(MouseEvent e) {
          endDrag = new Point(e.getX(), e.getY());
          repaint();
        }
      });
    }
    private void paintBackground(Graphics2D g2){
      g2.setPaint(Color.LIGHT_GRAY);



    }
    public void paint(Graphics g) {
      Graphics2D g2 = (Graphics2D) g;




      for (Shape s : shapes)
      {
        g2.setPaint(Color.BLACK);
        g2.draw(s);
        g2.fill(s);
      }

      if (startDrag != null && endDrag != null) {
        g2.setPaint(Color.LIGHT_GRAY);
        Shape r;


        if (key.equals("l"))
        {
             r = makeLine(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
        }



        if (key.equals("o"))
        {
             r = makeOval(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
        }

        if (key.equals("r"))
        {
             r = makeRectangle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
        }


        else
        {
            r = makeCircle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
        }

        g2.draw(r);
      }
    }

    private Rectangle2D.Float makeRectangle(int x1, int y1, int x2, int y2)
    {
      return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
    }

    private Ellipse2D.Float makeCircle(int cx1, int cy1, int cx2, int cy2)
    {
      return new Ellipse2D.Float(Math.min(cx1, cx2), Math.min(cy1, cy2), Math.abs(cx1 - cx2), Math.abs(cx1 - cx2));
    }

    private Ellipse2D.Float makeOval(int ox1, int oy1, int ox2, int oy2)
    {
      return new Ellipse2D.Float(Math.min(ox1, ox2), Math.min(oy1, oy2), Math.abs(ox1 - ox2), Math.abs(oy1 - oy2));
    }

        private Line2D.Float makeLine(int lx1, int ly1, int lx2, int ly2)
    {
      return new Line2D.Float(Math.min(lx1, lx2), Math.min(ly1, ly2), Math.max(lx1, lx2), Math.max(ly1, ly2));
    }


  }

}

...

/**
 * @(#)ShapeViewer.java
 *
 *
 * @author
 * @version 1.00 2015/5/17
 */
import javax.swing.*;

public class ShapeViewer
{

    //Creates and displays the application frame
    public static void main(String[] args)
    {
        JFrame ShapeViewer = new JFrame("Draw Stuff");
        ShapeViewer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ShapeViewer.getContentPane().add(new DrawShapes());

        ShapeViewer.pack();
        ShapeViewer.setVisible(true);
    }


}

1 个答案:

答案 0 :(得分:2)

你的问题在这里

    if (key.equals("l"))
    {
         r = makeLine(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
    }

    if (key.equals("o"))
    {
         r = makeOval(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
    }

    if (key.equals("r"))
    {
         r = makeRectangle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
    }
    else
    {
        r = makeCircle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
    }

唯一可用的输入是"r"和一个圆圈,如果按"l",那么它会r=makeLine(startDrag.x, startDrag.y, endDrag.x, endDrag.y),如果测试将r = makeCircle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);

您需要使用else语句加入所有if语句。

    if (key.equals("l")) {
         r = makeLine(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
    } else if (key.equals("o")) {
         r = makeOval(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
    } else if (key.equals("r"))  {
         r = makeRectangle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
    } else {
        r = makeCircle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
    }