[英]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. 我在Java.awt面板中直观地绘制了100个随机点(据我所知),但是它工作的不是那么顺利。 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. 100个x,y坐标是随机生成的,并发送到此文件中的JFrame。
CC_simplePerceptron.Java 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. 导入的文件“ Perceptron”和“ Point”与此问题范围无关,但是如果要运行代码,则可以在此处找到 。 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?
我不确定我的paint方法是如何工作的,以及为什么要用图形obj调用它,这是在方便的基础上在Java程序中绘制x,y坐标的最佳方法吗?
First, override paintComponent
and put your paint code in there. 首先,重写
paintComponent
并将您的绘画代码放在那里。 Don't forget to use super.paintComponent(g)
at the beginning so that it clears the panel before painting. 不要忘记在开始时使用
super.paintComponent(g)
,以便在绘画之前清除面板。 If you make your CC_SimplePerceptron
extend JPanel
, you can set it as the content pane: 如果使
CC_SimplePerceptron
扩展JPanel
,则可以将其设置为内容窗格:
frame.setContentPane(new CC_SimplePerceptron)
so it fills the frame. 所以它充满了框架。 Finally, use
setPreferredSize()
on the panel before you call frame.pack()
最后,在调用
frame.pack()
之前,在面板上使用setPreferredSize()
。
Take frame.setVisible(true);
取
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... 如果要动态更新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();
}
})
}
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. 作为一般建议,在执行任何自定义绘制之前,应重写
paintComponent
和paint
并应调用super
paint方法,以确保绘制链保持完整。
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 您还应该重写
getPreferredSize
并返回适当的大小提示,这将在计算窗口大小时为pack
提供更好的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.