在java图形中使用鼠标单击绘制三角形

在java图形中使用鼠标单击绘制三角形,java,graphics,mouse,Java,Graphics,Mouse,这是我的密码。画一个圆很有效,但我现在画三角形有困难。。应使用鼠标单击显示三角形,但在运行应用程序后立即显示三角形。请帮忙。谢谢 package mouse; import java.awt.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt

这是我的密码。画一个圆很有效,但我现在画三角形有困难。。应使用鼠标单击显示三角形,但在运行应用程序后立即显示三角形。请帮忙。谢谢

    package mouse;

import java.awt.*;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.event.*;

import java.awt.geom.*;

public class triangle extends JFrame implements ActionListener, MouseListener {
    int xx[]= {20, 50, 80};
    int yy[]= {80, 30, 80};



    public triangle () {
        setSize(2000,2000);
        addMouseListener(this);
    }

    public static void main(String[] args) {
        //TODO code application logic here
        java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                   triangle frame = new triangle();
                   frame.setVisible(true);
              }
        });
    }

    public void actionPerformed(ActionEvent ae) {

    }

    public void drawPolygon(int x, int y, int i)
    {
        Graphics g = this.getGraphics();
        g.setColor(Color.BLACK);
        g.drawPolygon(xx, yy, 3);

    }

    int x, y;

    public void mouseClicked(MouseEvent e) {
        x = e.getX();
        y = e.getY();

        repaint();
    }


    @Override
    public void paint(Graphics g) {
        drawPolygon(x, y, 3);
    }



    public void mouseExited(MouseEvent e) {

    }

    public void mousePressed(MouseEvent e) {

    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {

    }
}``

您遇到的问题是您重写了paint方法,因此drawPolygon方法已经在JFrame的初始绘制中被调用

为了获得想要的效果,应该避免覆盖paint方法,并且仅在调用MouseListener时使用drawPolygon方法

这应该是这样的:

@Override
public void mouseClicked(MouseEvent e) {
    x = e.getX();
    y = e.getY();

    drawPolygon(x,y,3);

    repaint();
}
public void drawPolygon(int x, int y, int i)
{
    Graphics g = this.getGraphics();
    g.setColor(Color.BLACK);

    int[] drawx = {xx[0]+x,xx[1]+x,xx[2]+x};
    int[] drawy = {yy[0]+y,yy[1]+y,yy[2]+y};

    g.drawPolygon(drawx, drawy, i);

}
要在鼠标单击的位置绘制三角形,需要将当前位置添加到原始三角形坐标,如下所示:

@Override
public void mouseClicked(MouseEvent e) {
    x = e.getX();
    y = e.getY();

    drawPolygon(x,y,3);

    repaint();
}
public void drawPolygon(int x, int y, int i)
{
    Graphics g = this.getGraphics();
    g.setColor(Color.BLACK);

    int[] drawx = {xx[0]+x,xx[1]+x,xx[2]+x};
    int[] drawy = {yy[0]+y,yy[1]+y,yy[2]+y};

    g.drawPolygon(drawx, drawy, i);

}
我希望这能回答你的问题

关于编程的一些一般性意见: 你绝对应该包括一个

               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
否则,程序不会在关闭帧时终止并继续运行

如果您避免将所有内容都放在一个类中,并将代码拆分为多个类,那么这也有助于提高代码的可读性。
这是设计类结构的一个很好的通用指南

您遇到的问题是您重写了paint方法,因此drawPolygon方法已在JFrame的初始绘制中调用

为了获得想要的效果,应该避免覆盖paint方法,并且仅在调用MouseListener时使用drawPolygon方法

这应该是这样的:

@Override
public void mouseClicked(MouseEvent e) {
    x = e.getX();
    y = e.getY();

    drawPolygon(x,y,3);

    repaint();
}
public void drawPolygon(int x, int y, int i)
{
    Graphics g = this.getGraphics();
    g.setColor(Color.BLACK);

    int[] drawx = {xx[0]+x,xx[1]+x,xx[2]+x};
    int[] drawy = {yy[0]+y,yy[1]+y,yy[2]+y};

    g.drawPolygon(drawx, drawy, i);

}
要在鼠标单击的位置绘制三角形,需要将当前位置添加到原始三角形坐标,如下所示:

@Override
public void mouseClicked(MouseEvent e) {
    x = e.getX();
    y = e.getY();

    drawPolygon(x,y,3);

    repaint();
}
public void drawPolygon(int x, int y, int i)
{
    Graphics g = this.getGraphics();
    g.setColor(Color.BLACK);

    int[] drawx = {xx[0]+x,xx[1]+x,xx[2]+x};
    int[] drawy = {yy[0]+y,yy[1]+y,yy[2]+y};

    g.drawPolygon(drawx, drawy, i);

}
我希望这能回答你的问题

关于编程的一些一般性意见: 你绝对应该包括一个

               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
否则,程序不会在关闭帧时终止并继续运行

如果您避免将所有内容都放在一个类中,并将代码拆分为多个类,那么这也有助于提高代码的可读性。 这是设计类结构的一个很好的通用指南