views:

1189

answers:

2

I have the points by the end of the GenerateButton class but now that I got my public double[][] matrix with all the points in, where do I begin drawing them???

my Main.java:

import java.awt.*;
import javax.swing.*;

public class Main {
     public static Display display = new Display();

     public static void main(String[] args) {
         display.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        display.setVisible(true);
     }
}

my Display.java:

import java.awt.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.util.Vector;

import javax.swing.*;

public class Display extends JFrame {
    final int FRAME_WIDTH = 910;
    final int FRAME_HEIGHT = 660;
    final int X_OFFSET = 40;
    final int Y_OFFSET = 40;

    final int GRAPH_OFFSETX = 15;
    final int GRAPH_OFFSETY = 40;
    final int GRAPH_WIDTH = 500;
    final int GRAPH_HEIGHT = 500;
    final int GRAPH_INTERVAL = 20;

    JButton submit;
    JTextField numPoint;
    JPanel bpanel;
    JPanel points;
    Vector<JTextField> pointsA = new Vector<JTextField>();
    int maxPoints;
    public double[][] matrix;

    public Display() {
        init();
    }

    public void init() {
        setBackground(Color.WHITE);
        setLocation(X_OFFSET, Y_OFFSET);
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setTitle("Geometric Transformations");
        getContentPane().setLayout(null);
        setDefaultLookAndFeelDecorated(true);

        numPoint = new JTextField();
        numPoint.setText("# Points?");
        numPoint.setBounds(530,200,120+20,25);

        SubmitButton submit = new SubmitButton("Submit");
        submit.setBounds(530+150, 200, 100, 25);

        GenerateButton submitC = new GenerateButton("Generate");
        submitC.setBounds(530-5, 200+130, 100, 25);

        points = new JPanel(new GridLayout(2,2));
        points.setBounds(530, 200+40,100+270,80);

        this.add(numPoint);
        this.add(submit);
        this.add(submitC);
        this.add(points, BorderLayout.LINE_START);

        repaint();
    }

    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.WHITE);
        g.fillRect(100, 100, 20, 30);
        g.setColor(Color.BLACK);
        genGraph(g, GRAPH_OFFSETX, GRAPH_OFFSETY,
                 GRAPH_WIDTH, GRAPH_HEIGHT, GRAPH_INTERVAL);
    }

    public void genGraph (Graphics g, int x, int y,
                          int width, int height, int interval) {
        // draw background
        int border = 5;
        g.setColor(Color.BLACK);
        width = width - (width % interval);
        height = height - (height % interval);
        for (int col=x; col <= x+width; col+=interval) {
            g.drawLine(col, y, col, y+height);
        }
        for (int row=y; row <= y+height; row+=interval) {
            g.drawLine(x, row, x+width, row);
        }
    }
    class SubmitButton extends JButton implements ActionListener {

        public SubmitButton(String title){
            super(title);
            addActionListener(this);
        }
        public void actionPerformed (ActionEvent e) {
            maxPoints = Integer.parseInt(numPoint.getText()) * 2;

            points.removeAll();        // clear JPanel so results from last aren't appended to
                                // delete this line and first enter 2 then 10 for # points
            for (int i=0; i<maxPoints; i++) {
                JTextField textField = new JTextField();
                points.add(textField);        // add to JPanel that gets displayed
                pointsA.add(textField);        // for getting values from later
            }

            matrix = new double[2][pointsA.size()/2];    // setting up dimension of matrix double[][]

            points.validate();
            points.repaint();

            // What to Check:
            // Things between commas are either spaces (which will be stripped later)
            // or numbers!

            // Pairs must match up!
        }
    }
    class GenerateButton extends JButton implements ActionListener {
        public GenerateButton (String title) {
            super(title);
            addActionListener(this);
        }
        public void actionPerformed (ActionEvent e) {
            int c=0;
            for (int i=0; i<pointsA.size()/2; i++) {
                JTextField pointTF = pointsA.get(i);
                Double point = Double.parseDouble(pointTF.getText());
                matrix[0][c] = point;
                c++;
            }
            c=0;
            for (int i=pointsA.size()/2; i<pointsA.size(); i++) {
                JTextField pointTF = pointsA.get(i);
                Double point = Double.parseDouble(pointTF.getText());
                matrix[1][c] = point;
                c++;
            }
            for (int i=0; i<matrix.length; i++) {
                for (int j=0; j<matrix[0].length; j++) {
                    System.out.println("i:"+i+"\t"+"j:"+j);
                    System.out.println("      "+matrix[i][j]);
                }
            }
        }
    }
}
+4  A: 

I'm not quite exactly sure what you are asking. Normally, you put all of the drawing functionality in the paint() method. However, you generally want to keep any long running work off of the AWT dispatch thread which is the same thread that your buttons' actionPerformed() method is invoked on. To keep your program responsive, you may want to delegate it to a SwingWorker.

If your app is not being shown, it is likely because you have not called pack() and show() (in that order) during your JPanel's initialization. Generally, you should do this as the last thing in your init() method. Adding components afterward or changing your components state anywhere but in the AWT thread after this may cause AWT to get mad at you and throw an exception.

If you are asking how to draw the points themselves, you should look into Graphics.drawPolyline() and the related methods, which you should call from within your paint() method.

If you're asking how to draw them from within the button handler, you don't. You call repaint() and let the object repaint itself on its own time. Also, in your case, I wouldn't bother subclasing JButton. Just implement actionListener. In you init() method, just create two buttons and register your listeners with them. This also means that you don't have to override their constructors.

Also, you mask submit during your init() method: it is never referenced and neither is bpanel. Also, I would use a separate X and Y array rather than trying to combine them into a single matrix. You don't need to declare matrix to be public since the inner classes can already see it anyway. In fact, you can (and probably should) declare all of the fields to be private. You also have way too many magic numbers floating around and you are fighting Swing way too much: just let it do it's job laying all your stuff out. You should also try show the smallest possible problem that will allow us to answer your problem: this one sort of flows all over the place and is hard to follow.

Oh, and the real reason why your lines are not being drawn: you call repaint on your JPanel and not the JFrame that you want to draw on. If you just call repaint() instead of points.repaint() the JFrame will take care of drawing itself and all of its children.

James
+2  A: 

You should create a new component and draw the dots there, not in the Frame it self.

Read this:

http://java.sun.com/products/jfc/tsc/articles/painting

And then you can use this:

http://java.sun.com/javase/6/docs/api/java/awt/Canvas.html

And add that object to the frame.

OscarRyz