框架和paintComponent

时间:2015-01-13 18:58:30

标签: java frame paintcomponent

如何根据此文本文件打印多个帧? 我需要同时绘制这条线FS和每次迭代; 该行将在所有帧中打印。 我怎么能这样做?

请遵循以下代码:

有人想知道这个问题吗?


文本文件是:

FS:1,3,5,7 迭代:1
F1:11,13,15,17
F2:21,23,25
F3:1,4,6 迭代:2
F1:2,14,15,18
F2:22,24,26
F3:4,8,10

这是代码的第一部分:

package visualizationBasic;


import java.awt.Color;
import java.awt.Graphics;


public class graphPaper extends javax.swing.JPanel {


  /**
 * 
 */
private static final long serialVersionUID = 1L;

public graphPaper() {
    setBackground(Color.white);
    //System.out.println("HH");
  }

  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    //this.setBackground(Color.WHITE);
    // this method is now somewhat superfluous 
    //unless you're doing other painting here
  }
}

这是代码的第二部分:

package visualBF;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.awt.*;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import visualizationBasic.*;

public class Interface extends graphPaper {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private int numberOfNodes = 100;

/**
 * A main routine lets this class be run as an application.
 */
public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            String file_name = "./output";
            String[ ] aryLines = null;

                    // 
                    try {
                        readData file = new readData(file_name );
                        aryLines = file.OpenFile( );
                        System.out.println(Arrays.toString(aryLines));
                    }
                    catch ( IOException e ) {
                        System.out.println( e.getMessage() );
                    }    

                    int w = 1;
                    for(int i = 0;i<aryLines.length;i++){
                        String vectorBF = aryLines[i];  

                        System.out.println("vectorBF: " + vectorBF);

                        System.out.println(vectorBF.substring(0, 1));
                        if (vectorBF.substring(0, 1).contentEquals("I")) {
                            createAndShowGUI(w);
                            w++;

                        }

                    }
         }
     });

}




private static void createAndShowGUI(int i) {
    System.out.println("Created GUI on EDT? "+
            SwingUtilities.isEventDispatchThread());
    JFrame window;
    window = new JFrame("Iteration " + i);
    Interface content = new Interface();
    window.setContentPane(content);
    //window.getContentPane().add(new Interface());
    window.pack(); // Size the window to the preferred size of its content.
    window.setLocation(50,50);
    window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    window.setResizable(false);  // User can't change the window's size.
    window.setVisible(true);

}


//-------------------------------------------------------------------

int selectedRow; // Row and column of selected square.  If no
int selectedCol; //      square is selected, selectedRow is -1.
int R, G, B, flag;

/**
 * Constructor.  Set selectedRow to -1 to indicate that
 * no square is selected.  And set the board object
 * to listen for mouse events on itself.
 */
public Interface() {
    selectedRow = -1;     
    setPreferredSize( new Dimension(500,500) );
}



@Override
public void paintComponent(Graphics g) {

//System.out.println("Hello!!");

super.paintComponent(g);    

int row;   // Row number, from 0 to 7
int col;   // Column number, from 0 to 7
int x,y;   // Top-left corner of square     


double numberOfRowsDecimal = Math.sqrt(numberOfNodes); 
int numberOfRows = (int) Math.ceil(numberOfRowsDecimal);

//ArrayList<Integer> numbers = new ArrayList<>();
//ArrayList<String> texts = new ArrayList<>();

for ( row = 0;  row < numberOfRows;  row++ ) {
    for ( col = 0;  col < numberOfRows;  col++) {
        x = col * 50;
        y = row * 50;
        g.setColor(Color.black);

        g.fillRect(x, y, 50, 1);
        g.fillRect(x, y, 1, 50);

    }
} // end for row        

String file_name = "./output";
String[ ] aryLines = null;
int iterationCount = 0;
int fiPlusSolutionCount = 0;
int m = 0;
int f = 0;
        // 
        try {
            readData file = new readData(file_name );
            aryLines = file.OpenFile( );
            //System.out.println(Arrays.toString(aryLines));
        }
        catch ( IOException e ) {
            System.out.println( e.getMessage() );
        }    

        for(int i = 0;i<aryLines.length;i++){

                String vectorBF = aryLines[i];

                ArrayList<String> vectorName =  new ArrayList<>();
                ArrayList<Integer> vectorNumber = new ArrayList<>();
                String str = null;



            for (String line : vectorBF.split("\\W+")) {

                if (!line.matches("[0-9]+")) {
                    str = String.valueOf(line);
                    //System.out.println("str: " + str);
                    vectorName.add(str);
                } else {
                    Integer num = Integer.valueOf(line);
                    vectorNumber.add(num);
                    //System.out.println("num: " + num);            
                }
            }


            if (str.equals("Iteration")){
                iterationCount++;
            }

            if(iterationCount < 2) fireflyPlusSolutionCount ++;


        }
        fiPlusSolutionCount = fiPlusSolutionCount - 1;
        System.out.println(fiPlusSolutionCount);

        for(int i = 0;i<aryLines.length;i++){

            String vectorBF = aryLines[i];

            ArrayList<String> vectorName =  new ArrayList<>();
            ArrayList<Integer> vectorNumber = new ArrayList<>();
            String str = null;




            for (String line : vectorBF.split("\\W+")) {

                if (!line.matches("[0-9]+")) {
                    str = String.valueOf(line);
                    //System.out.println("str: " + str);
                    vectorName.add(str);
                } else {
                    Integer num = Integer.valueOf(line);
                    vectorNumber.add(num);
                    //System.out.println("num: " + num);            
                }
            }
            //System.out.println(str);
            if(str.equals("FS")){
                R = 255;
                G = 0;
                B = 0;
                m = 1;
            }

            else if(str.equals("Iteration")){
                R = 255;
                B = 255;
                G = 255;

                m = 1;



            }

            else {
                R = 0;
                G = 255;
                B = 0;
                f++;
                m++;
            }






            for(int w = 0; w<vectorNumber.size();w++){

                int faultyNode = (int) vectorNumber.get(w);
                int rowNumber = 1;
                int columnNumber = 1;
                //System.out.println(faultyNode);  
                while(faultyNode > numberOfRows){
                    rowNumber ++;
                    faultyNode = faultyNode - numberOfRows;
                }
                columnNumber = faultyNode;


                if(faultyNode == 0){
                    rowNumber = 0;
                    columnNumber = 1;
                }

                if(faultyNode%numberOfRows == 0){
                    rowNumber = rowNumber + 1;
                    columnNumber = 0;
                }

                 x = (columnNumber) * 50 + (m-1)*50/(fiPlusSolutionCount);
                 y = (rowNumber -1) * 50 ;

                 // Creating Random Colors
                 if(!str.equals("Iteration")){
                     Color randomColor;
                    // System.out.println(vectorName);
                     randomColor = new Color(R, G, B);
                     g.setColor(randomColor);
                     g.fillRect(x, y, 50/fiPlusSolutionCount, 50);

                 }

            }

        }    
        g.dispose();


}

}

0 个答案:

没有答案