简体   繁体   中英

Java: plotting points with the abstract windowing toolkit

I am visually plotting 100 random points in a Java.awt panel (as far as i know) but it is not working so smoothly. The pane has to be maximized by the user before they show up. Im not sure which command I am missing to make this more fluid

The 100 x,y coordinates are generated randomly and sent to a JFrame in this file.

CC_simplePerceptron.Java

import java.awt.*;       // Using AWT's Graphics and Color          abstract window toolkit
import java.awt.event.*; // Using AWT event classes and listener interfaces
import javax.swing.*;    // Using Swing's components and containers
import javax.swing.*;
import java.awt.geom.Ellipse2D;

import Components.Perceptron;
import Components.Point;

public class CC_SimplePerceptron extends JComponent { 
    public static final int maxD = 800;
    public static Perceptron p = new Perceptron();
    public static Point[] points = new Point[100];          //100 element array of type Point to hold data


    public static void main(String[] args){
        JFrame frame = new JFrame("Draw Ellipse Demo");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().add(new CC_SimplePerceptron());
        frame.pack();
        frame.setSize(new Dimension(maxD, maxD));
        frame.setVisible(true);


        System.out.println("Point initialiations");
        //initializing 100 random points
        for(int i = 0; i < points.length; i++){
            points[i] = new Point();                //random point
            System.out.println("Point " + i + " =" + points[i].getX() + ", " + points[i].getY());
        }

        float[] inputs = {-1f,0.5f};                //0.5f to indicate its float not double
        int guess = p.guess(inputs);

        System.out.println(guess);
        return;
    }

   // Constructor to set up the GUI components and event handlers
   public CC_SimplePerceptron() {
    System.out.println("Def constructor");
   }

   @Override
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        g2.setPaint(Color.RED);
        g2.setStroke(new BasicStroke(5.0f));
        for(int i = 0; i < points.length; i++){
            g2.fill(new Ellipse2D.Double(points[i].getX(), points[i].getY(), 8, 8));
        }
    }
}

The imported files "Perceptron" & "Point" are not relevant for this question scope, but can be found here if one wants to run the code. Any thoughts on why the pane doesnt display all points right away? Im not exactly sure how my paint method works, and why it is called with a graphics obj, is this the best method to plot my x,y coordinates in a java program on the basis of convience?

First, override paintComponent and put your paint code in there. Don't forget to use super.paintComponent(g) at the beginning so that it clears the panel before painting. If you make your CC_SimplePerceptron extend JPanel , you can set it as the content pane:

frame.setContentPane(new CC_SimplePerceptron)

so it fills the frame. Finally, use setPreferredSize() on the panel before you call frame.pack()

Take frame.setVisible(true); and call it last, after you've built all the data you want to display...

public static void main(String[] args){
    JFrame frame = new JFrame("Draw Ellipse Demo");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new CC_SimplePerceptron());
    frame.pack();
    frame.setSize(new Dimension(maxD, maxD));


    System.out.println("Point initialiations");
    //initializing 100 random points
    for(int i = 0; i < points.length; i++){
        points[i] = new Point();                //random point
        System.out.println("Point " + i + " =" + points[i].getX() + ", " + points[i].getY());
    }

    float[] inputs = {-1f,0.5f};                //0.5f to indicate its float not double
    int guess = p.guess(inputs);

    System.out.println(guess);
    frame.setVisible(true);

}

If you want to dynamically update the UI, then, in your case, calling repaint on the component you want updated should also work...

public static void main(String[] args){
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame frame = new JFrame("Draw Ellipse Demo");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            CC_SimplePerceptron component = new CC_SimplePerceptron();
            frame.getContentPane().add(component);
            frame.pack();
            frame.setSize(new Dimension(maxD, maxD));
            frame.setVisible(true);

            System.out.println("Point initialiations");
            //initializing 100 random points
            for(int i = 0; i < points.length; i++){
                points[i] = new Point();                //random point
                System.out.println("Point " + i + " =" + points[i].getX() + ", " + points[i].getY());
            }

            float[] inputs = {-1f,0.5f};                //0.5f to indicate its float not double
            int guess = p.guess(inputs);

            System.out.println(guess);
            component.repaint();
        }
    })
}

Other considerations

As general recommendation, you should be overriding paintComponent and paint and you should be calling the super paint method before performing any custom painting to ensure that the paint chain remains intact.

You should also override getPreferredSize and return an appropriate size hint, this will provide pack with better information when it calculates the size of the window

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