簡體   English   中英

Java:使用抽象窗口工具箱繪制點

[英]Java: plotting points with the abstract windowing toolkit

我在Java.awt面板中直觀地繪制了100個隨機點(據我所知),但是它工作的不是那么順利。 窗格必須由用戶最大化后才能顯示。 我不確定我缺少哪個命令來使它更流暢

100個x,y坐標是隨機生成的,並發送到此文件中的JFrame。

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

導入的文件“ Perceptron”和“ Point”與此問題范圍無關,但是如果要運行代碼,則可以在此處找到 關於為什么窗格沒有立即顯示所有點的任何想法? 我不確定我的paint方法是如何工作的,以及為什么要用圖形obj調用它,這是在方便的基礎上在Java程序中繪制x,y坐標的最佳方法嗎?

首先,重寫paintComponent並將您的繪畫代碼放在那里。 不要忘記在開始時使用super.paintComponent(g) ,以便在繪畫之前清除面板。 如果使CC_SimplePerceptron擴展JPanel ,則可以將其設置為內容窗格:

frame.setContentPane(new CC_SimplePerceptron)

所以它充滿了框架。 最后,在調用frame.pack()之前,在面板上使用setPreferredSize()

frame.setVisible(true); 建立完所有要顯示的數據后,最后調用它...

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

}

如果要動態更新UI,則在您需要的情況下,在要更新的組件上調用repaint也應該有效。

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

其他注意事項

作為一般建議,在執行任何自定義繪制之前,應重寫paintComponentpaint並應調用super paint方法,以確保繪制鏈保持完整。

您還應該重寫getPreferredSize並返回適當的大小提示,這將在計算窗口大小時為pack提供更好的信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM