简体   繁体   English

分别在列表中的每两个点之间画一条线

[英]drawing a line between each two points in a list respectively

Hi I work with netbeans. 嗨,我和netbeans一起工作。 I have written a code which has two classes (1) Demo (2) mainFrame which extends javax.swing.JFrame 我编写了一个具有两个类的代码(1) Demo (2) mainFrame which extends javax.swing.JFrame

at first, my mainFrame will be run and it will show a panel that you can put some points on it and then when you click a button ; 首先,将运行我的mainFrame ,它将显示一个面板,您可以在其上放置一些点,然后单击按钮时; the dimension of all points will be store in a list , then I will send this list to Demo class because I have to put the points in an order that you can draw a line between each two points respectively. 所有点的维将存储在一个列表中,然后将这个列表发送到Demo class因为我必须按顺序放置点,以便可以分别在每两个点之间绘制一条线。 I have two problems here : 我在这里有两个问题:

  1. I have that list <listOfPoints> ,how can I send this list to mainFrame class for drawing lines without making a new object of the mainFrame class? 我有该list <listOfPoints> ,如何在不创建mainFrame类的新对象的情况下将该列表发送到mainFrame类以绘制线条?

  2. I should work with paint method? 我应该用paint方法吗? please help me by some code example in java (with netbeans) 请通过java中的一些代码示例帮助我(使用netbeans)

    Totally : 总计:

     I have a lot of points' dimension in my list ,I want to traverse my list in the ie, paint method and draw line between each pi and pi+1.how can I do this? 

thanks 谢谢

Google: netbeans java drawline 谷歌:netbeans java drawline

first answer 第一个答案

search 搜索

Graphics gg = buffImg.createGraphics();
        gg.setColor(Color.MAGENTA);
        gg.drawLine(10,10,buffImg.getWidth()-buffImg.getWidth()/10,

http://wws2.uncc.edu/tpw/tpwJavaNtebeansTutorial/index.html http://wws2.uncc.edu/tpw/tpwJavaNtebeansTutorial/index.html

regarding your question about drawing a list of points 关于你绘制点列表的问题

void drawList(Graphics gg, List points){
   MyPointClass prev p = null;
   for(MyPointClass p : points){
       if(prevp != null){
           gg.drawLine(prevp.x, prevp.y, p.x, p.y);
       }
       prev = p;
    }
}

And if you don't feel like passing object from one frame to the other male drawList static and then you can do MyClassContainingDrawLineMethod.drawList(gg, points); 如果你不想将对象从一个帧传递到另一个男性drawList静态然后你可以做MyClassContainingDrawLineMethod.drawList(gg,points);

Does it even matter that you're using Netbeans? 您使用Netbeans甚至是否重要?

Also, to answer: 另外,回答:

I think with Swing you generally override the paintComponent(Graphics gg) method to describe how you want your panel/frame drawn. 我认为使用Swing,您通常会覆盖paintComponent(Graphics gg)方法来描述您希望如何绘制面板/框架。 And call repaint() when you want your program to actually redraw itself. 并希望您的程序实际重绘时调用repaint()。

You may want to read this tutorial here: http://download.oracle.com/javase/tutorial/uiswing/ Especially the "Performing custom painting" section. 您可能需要阅读本教程: http//download.oracle.com/javase/tutorial/uiswing/特别是“执行自定义绘画”部分。

Also, have a look through the appropriate Swing and AWT parts of the API. 另外,请查看API的相应Swing和AWT部分。

You can draw lines using the drawLine() method of the graphics class. 您可以使用图形类的drawLine()方法绘制线条。 Or by using the Line class (Line2D.Double ,etc) to represent your lines and then going, graphicsObject.draw(line) , etc. 或者使用Line类(Line2D.Double等)来表示你的行然后去,graphicsObject.draw(line)等。

What you could do is add the points to a list, as you add then to the graphics panel (repainting whenever you add one), then when the button is pressed, you could have run the method to process your list in the button's actionListener. 您可以做的就是将点添加到列表中,然后再添加到图形面板中(每次添加时都重新绘制),然后在按下按钮时,您可以运行该方法以在按钮的actionListener中处理列表。 So something like: list = demo.processList(list). 所以类似:list = demo.processList(list)。 Where processList has a header like: public List processList(List toProcess). 其中processList具有标题,例如:public List processList(List toProcess)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM