Java 如何将数据从事件侦听器发送到paintComponent

Java 如何将数据从事件侦听器发送到paintComponent,java,swing,user-interface,Java,Swing,User Interface,我需要创建一个按钮,进行一些计算,并给我一个列表,我需要在我的绘画中使用该列表,并使这些坐标线。将数据从事件侦听器发送到绘制方法的最佳方式是什么?谢谢大家! 你没有给我们提供足够的信息,你问了很多问题,因此这里有一个粗略的大纲,让你开始使用awt/swing按钮,你可以从那里查找其余的按钮,或者问一些更具体的问题,如如何设置自定义绘制等。请参阅下面代码中的注释和解释 创建一个公开可用的数组或列表来存储坐标。以下是我们将用于在事件侦听器和绘制方法之间共享信息的内容: public static L

我需要创建一个按钮,进行一些计算,并给我一个列表,我需要在我的绘画中使用该列表,并使这些坐标线。将数据从事件侦听器发送到绘制方法的最佳方式是什么?谢谢大家!

你没有给我们提供足够的信息,你问了很多问题,因此这里有一个粗略的大纲,让你开始使用awt/swing按钮,你可以从那里查找其余的按钮,或者问一些更具体的问题,如如何设置自定义绘制等。请参阅下面代码中的注释和解释

创建一个公开可用的数组或列表来存储坐标。以下是我们将用于在事件侦听器和绘制方法之间共享信息的内容:

public static LinkedList<Point> myCoOrdinateList = new LinkedList<>();
//create graphics so we can draw lines
Graphics2D g2d = (Graphics2D) g;
//we need to work with 2 points so we will store one point here:
Point previous = new Point(0, 0);
for (Iterator<Point> iterator = myCoOrdinateList.iterator(); iterator.hasNext();)
{
    //get point
    Point nextPoint = iterator.next();
    //link previous point and next pint in the co-ordinates list:
    g2d.drawLine(previous.x, previous.y, nextPoint.x, nextPoint.y);
    //set new previous point so the next line is ready to be drawn
    previous = nextPoint;
}
如果通过覆盖零部件的绘制方法进行绘制,则可以在绘制方法中添加类似的内容:

public static LinkedList<Point> myCoOrdinateList = new LinkedList<>();
//create graphics so we can draw lines
Graphics2D g2d = (Graphics2D) g;
//we need to work with 2 points so we will store one point here:
Point previous = new Point(0, 0);
for (Iterator<Point> iterator = myCoOrdinateList.iterator(); iterator.hasNext();)
{
    //get point
    Point nextPoint = iterator.next();
    //link previous point and next pint in the co-ordinates list:
    g2d.drawLine(previous.x, previous.y, nextPoint.x, nextPoint.y);
    //set new previous point so the next line is ready to be drawn
    previous = nextPoint;
}
//创建图形以便我们可以绘制线条
Graphics2D g2d=(Graphics2D)g;
//我们需要使用2个点,因此我们将在此处存储一个点:
上一个点=新点(0,0);
for(Iterator Iterator=mycoordinalist.Iterator();Iterator.hasNext();)
{
//明白
Point nextPoint=iterator.next();
//链接坐标列表中的上一点和下一品脱:
g2d.绘制线(previous.x,previous.y,nextPoint.x,nextPoint.y);
//设置新的上一点,以便绘制下一条线
上一个=下一个点;
}
下面是来自官方java教程的更多信息。我建议你在提出更多问题之前先阅读这些链接

定制绘画:

使用图形/绘图工具:

有关使用回转按钮的详细信息:

您能提供更多信息吗?到目前为止你试过什么?问题越好,答案就越好。