gpt4 book ai didi

Java Mousepress() 绘制图像

转载 作者:行者123 更新时间:2023-12-01 21:34:20 26 4
gpt4 key购买 nike

我创建这个是为了当在鼠标的 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);
}
}

最佳答案

i seems then that the drawfish method is not being called.

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

其他问题:

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

  2. 不要重写paint()。自定义绘制是通过重写 paintComponent() 方法来完成的。并且不要忘记调用 super.paintComponent(...)。

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

然而,真正的问题是,当您单击鼠标时,鱼可能会被绘制出来,但是计时器会进行重新绘制,这将在 2 毫秒后清除面板,因此您永远不会真正看到鱼。摆脱计时器。计时器不需要画鱼。

假设您想绘制多条鱼,您需要跟踪您单击的每个位置,然后绘制所有鱼。执行此操作的两种方法是:

  1. 保留要绘制鱼的点的 ArrayList,然后在绘制方法中迭代此列表
  2. 当鼠标点击发生时,在 BufferedImage 上绘制鱼,然后绘制图像。

参见Custom Painting Approaches了解这两种方法的工作示例。

关于Java Mousepress() 绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37090592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com