java - 编译器错误说我不重写抽象方法 keyReleased

标签 java jframe geometry keylistener mouselistener

编写一个程序,根据给出的指令绘制形状。 这是说明。但是,每当我编译时,我都会收到消息

DrawShapes is not abstract and does not override abstract method keyReleased(java.awt.event.KeyEvent) in java.awt.event.KeyListener".

我已经将方法keyReleased放入程序中,所以我不知道为什么它一直说我没有重写抽象方法keyReleased。我无法编译该程序,但我也相当确定即使编译了该程序也无法运行。谢谢!

Write a program that will allow the user to draw a shape with a mouse. The shape to draw should be determined by keyPressed event using the following keys: c draws a circle, o draws an oval, r draws a rectangle and l draws a line. The size and placement of the shape should be determined by mousePressed and mouseReleased events. Display the name of the current shape in a JLabel in the SOUTH region of a BorderLayout. The initial shape should default to a circle. Make sure the shape displays in the direction of the movement of the mouse. Use JFrame and Paint . You should have two files: ShapesViewer and DrawShapes (20 points)

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 DrawShapes()
  {
    this.setSize(300, 300);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.add(new PaintSurface(), BorderLayout.CENTER);
    this.setVisible(true);
  }

  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.abs(lx1 - lx2), Math.abs(ly1 - ly2));
    }

    public void keyPressed(KeyEvent event)
    {
        key = event.getKeyText(event.getKeyCode());
    }


    public void keyReleased(KeyEvent event)
    {

    }

    //handle press of any action key
    public void keyTyped(KeyEvent event)
    {

    }

  }
}

……

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);
    }


}

最佳答案

是的,但是这不是在DrawShape类中,而是在它的私有(private)内部类PaintSurface中。将该方法移至 DrawShape 类中。

关于java - 编译器错误说我不重写抽象方法 keyReleased,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30297879/

相关文章:

JAVA绘制从角色发射的子弹到鼠标位置的有效方法?

c++ - 绕圆轴的最短距离;使用学位

java - Weblogic MBean 服务器仅在 15 秒后提供该值

java - 对 child 有限制的 hibernate 标准

java swing按钮 Action

添加 JTextfield 后 Java 不呈现组件

algorithm - 如何在 O(nlogn) 中找到相交线的上包络线?

Java:JTable 重新排序行并刷新

java - 使用MS Access基础制作授权JFrame

java - 当用户单击窗口的关闭按钮时隐藏框架而不是关闭