Java Mousepress()绘制图像

时间:2016-05-07 15:54:45

标签: java swing paint mouselistener jcomponent

当我在鼠标的x和y坐标上按下鼠标时,我创建它来画一条鱼。但我似乎没有调用drawfish方法。我找不到它无法正常工作的原因。我会非常感谢任何帮助。

  /*FishTank*/
  import java.awt.Graphics;
  import javax.swing.JComponent;
  import javax.swing.JFrame;
  import java.awt.event.*;
  import javax.swing.*;
  import java.awt.*;
  import java.awt.geom.*;
  /*FishTank class-contains a frame and the WinstonCanvas.*/
  public class FishTank{

    public static void main ( String[] args ){
      javax.swing.SwingUtilities.invokeLater(new Runnable(){
  public void run(){
          JFrame window = new JFrame();
          window.setTitle("Fish Tank");
          window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          window.setBounds(30, 30, 700, 430);
          window.getContentPane().add(new FishTankCanvas());
          window.setVisible(true);
        }
      });
    }
  }
  /*FishTankCanvas is a component that allows drawing shapes.*/
  class FishTankCanvas extends JComponent {
    static Graphics2D g;
    int x = 11;
    Timer myTimer;
    public FishTankCanvas(){
      myTimer = new Timer (2, new ActionListener(){
        public void actionPerformed (ActionEvent evt){
          repaint();
        }
      });
      myTimer.start();
    }

    public void paint(Graphics graphics) {
      g = (Graphics2D)graphics;
      //makes the background white
      Color backgroundColor = new Color(89, 216, 255);//light blue
      g.setColor(backgroundColor);
      g.fillRect(0,0,this.getWidth(),this.getHeight());

      //  drawfish (Graphics graphics, int bodyX, int bodyY, int            bodyLength,int bodyHeight, int tailwidth, int eyesize,int tailcolor, int bodycolor)

      // Mouselistener and mouseadapter  
      this.addMouseListener (new MouseAdapter() {
        public void mousePressed(MouseEvent e) {  
          //call drawfish method
          drawfish(FishTankCanvas.g,e.getX(), e.getY(),118,74,1,((int)            (Math.random()*(4 - 0))));
          repaint();
        }
      });

      // x coordinate plus 1 of fish (animate)
      x= x + 1; 
    }
    // drawfish method
    public void drawfish(Graphics graphics, int bodyX, int bodyY, int       bodyLength,int bodyHeight,int tailcolor, int bodycolor ){

      Graphics2D g = (Graphics2D)graphics;
      bodyX +=x;
      //colours
      Color[] colours= new Color[5];
      colours[0] =  new Color(0, 0, 0);//black
      colours[1] = new Color(162, 0, 255);//purple
      colours[2] = Color.red;//red
      colours[3] = new Color(255,255,0);// yellow
      colours[4] = new Color(60,179,113);//green

      //draw fish
      // body
      g.setColor(colours[bodycolor]);
      g.fillOval(bodyX, bodyY, bodyLength, bodyHeight);
      // tail
      g.setColor(colours[tailcolor]);
      int tailWidth = bodyLength/4;
      int tailHeight = bodyHeight/2;
      int[] tailPointx = new int[3];
      int[] tailPointy = new int[3];
      tailPointx[0]=bodyX;
      tailPointy[0]=bodyY+bodyHeight/2;
      tailPointx[1]=bodyX-tailWidth;
      tailPointy[1]=bodyY+bodyHeight/2-tailHeight;
      tailPointx[2]=bodyX-tailWidth;
      tailPointy[2]=bodyY+tailHeight+tailHeight;
      g.fillPolygon(tailPointx, tailPointy, 3);
      // eye
      g.setColor(colours[0]);
      g.fillOval(bodyX+3*bodyLength/4, bodyY+bodyHeight/2-bodyHeight/5,          bodyHeight/5, bodyHeight/5);      
    }
  }      

1 个答案:

答案 0 :(得分:3)

  

我似乎没有调用drawfish方法。

这很容易验证。您需要做的就是向方法添加调试代码以确定是否为真。然后你可以告诉我们这是问题而不是猜测。

其他问题:

  1. 不要在绘图方法中将MouseListener添加到组件中。应该在你的类的构造函数中添加监听器。

  2. 不要覆盖paint()。通过覆盖paintComponent()方法完成自定义绘制。并且不要忘记调用super.paintComponent(...)。

  3. 扩展JPanel而不是JComponent。然后你可以使用setBackground()方法绘制背景。

  4. 然而,真正的问题是,当你点击鼠标时,鱼可能会被绘制,但是然后Timer会重新绘制,这将在2ms之后清除面板,所以你永远不会真正看到鱼。摆脱计时器。定时器不需要画鱼。

    假设你要画多条鱼,你需要跟踪你点击的每个地方,然后画掉所有的鱼。这样做的两种方式是:

    1. 保留要绘制鱼的点的ArrayList,然后在绘画方法中遍历此列表
    2. 当鼠标点击发生时,在BufferedImage上绘制鱼,然后只绘制图像。
    3. 有关这两种方法的工作示例,请参阅Custom Painting Approaches