简体   繁体   中英

drawing a line between each two points in a list respectively

Hi I work with netbeans. I have written a code which has two classes (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 ; 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. 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?

  2. I should work with paint method? please help me by some code example in java (with 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

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

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);

Does it even matter that you're using 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. And call repaint() when you want your program to actually redraw itself.

You may want to read this tutorial here: http://download.oracle.com/javase/tutorial/uiswing/ Especially the "Performing custom painting" section.

Also, have a look through the appropriate Swing and AWT parts of the API.

You can draw lines using the drawLine() method of the graphics class. Or by using the Line class (Line2D.Double ,etc) to represent your lines and then going, graphicsObject.draw(line) , etc.

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. So something like: list = demo.processList(list). Where processList has a header like: public List processList(List toProcess).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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