Where should I start painting? (Java, GUI)

I have points towards the end of the GenerateButton class, but now that I have my public double [] [] matrix with all the points in, where will I start 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]);
                }
            }
        }
    }
}

      

-2


source to share


2 answers


I'm not really sure what you are asking. Typically, you add all the paint functions to the paint () method. However, you usually want to save any long work from the AWT dispatch thread, which is the same thread that the actionPerformed () method is called on. To make your program responsive, you can pass it to the SwingWorker.

If your application is not showing, it is most likely because you did not call pack () and show () (in that order) during the JPanel initialization. Typically, you should do this as the last thing in your init () method. Adding components after, or changing the state of components anywhere, but in the AWT thread afterward, can cause AWT to get angry with you and throw an exception.

If you are asking how to paint the points yourself, you should look into Graphics.drawPolyline () and the associated methods that you should call from your paint () method.



If you are asking how to draw them from a button handler, you are not. You call repaint () and let the object repaint itself in due course. Also, in your case, I would not sublicense the JButton. Just implement actionListener. In your init () method, just create two buttons and register them with them. This also means that you don't need to override their constructors.

Also, you mask submit during your init () method: it is never referenced and is not a bpanel. Also, I would use a separate X and Y array instead of trying to combine them into one matrix. You do not need to declare the matrix public as the inner classes can already see it. In fact, you can (and probably should) declare all fields private. You also have too many magic numbers floating around and wrestling too much with Swing: just let that do the job laying out all your stuff. You should also try to show the smallest possible problem that will allow us to answer your problem: this kind of currents is all over the place and difficult to follow.

Oh, and the real reason why your lines aren't drawn: you are calling the redraw on your JPanel, not the JFrame you want to draw on. If you just call repaint () instead of point.repaint (), JFrame takes care of painting itself and all of its children.

+4


source


You have to create a new component and draw the points there, not in the frame.

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 this object to the frame.

+2


source







All Articles