簡體   English   中英

Java Mousepress()繪制圖像

[英]Java Mousepress() to draw images

我創建此程序是在鼠標的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);      
    }
  }      

我似乎然后沒有調用drawfish方法。

好了,這很容易驗證。 您需要做的就是將調試代碼添加到該方法中,以確定是否正確。 然后,您可以告訴我們是否是問題所在,而不用猜測。

其他問題:

  1. 不要在繪畫方法中將MouseListener添加到組件中。 偵聽器應添加到類的構造函數中。

  2. 不要覆蓋paint()。 通過覆蓋paintComponent()方法來完成自定義繪制。 並且不要忘記調用super.paintComponent(...)。

  3. 擴展JPanel而不是JComponent。 然后,您可以只使用setBackground()方法繪制背景。

但是,真正的問題是,當您單擊鼠標時,可能會畫出魚,但是Timer進行了重新繪制,將在2ms后清除面板,因此您永遠不會真正看到魚。 擺脫計時器。 計時器無需畫魚。

假設您要繪制多條魚,則需要跟蹤單擊的每個位置,然后繪制所有魚。 做到這一點的兩種方法是:

  1. 保留要繪制魚的點的ArrayList,然后在繪制方法中遍歷此列表
  2. 當鼠標單擊發生時,將魚繪制在BufferedImage上,然后僅繪制圖像。

有關這兩種方法的工作示例,請參見自定義繪畫方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM