仅作个人学习用
在GUI界面上演示一个移动的曲线,曲线在移动过程中,同时也显示当前曲线对应的y值。
至少4种曲线。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static java.lang.Math.*;
public class Main {
public static class DynamicCurveSystem extends JFrame {
private static final int WIDTH = 960;
private static final int HEIGHT = 768;
private static final int MARGIN = 50;
private static final int NUM_POINTS = 1000000;
private static final int POINT_DELAY = 10;
private static final int MAX_POINTS = 1000000;
private final List<Double> data = new ArrayList<>();
private final List<Point> points = new ArrayList<>();
private double x = 0;
private int currentPoint = 0;
private int lineOffset = 0;
private final JLabel label;
public static class TryJButton extends JPanel implements ActionListener {
protected JButton b1, b2, b3, b4;
public TryJButton() {
b1 = new JButton("y=sinx");
b2 = new JButton("y=cosx");
b3 = new JButton("y=x");
b4 = new JButton("y=2x");
add(b1);add(b2);add(b3);add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
// Create and show DynamicCurveSystem window
DynamicCurveSystem dc = new DynamicCurveSystem();
dc.setVisible(true);
}
if (e.getSource() == b2) {
// Create and show DynamicCurveSystem window
DynamicCurveSystemcos dc = new DynamicCurveSystemcos();
dc.setVisible(true);
}
if (e.getSource() == b3) {
// Create and show DynamicCurveSystem window
DynamicCurveSystemx dc = new DynamicCurveSystemx();
dc.setVisible(true);
}
if (e.getSource() == b4) {
// Create and show DynamicCurveSystem window
DynamicCurveSystem2x dc = new DynamicCurveSystem2x();
dc.setVisible(true);
}
}
}
public DynamicCurveSystem() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("Dynamic Curve System");
setSize(WIDTH, HEIGHT);
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Stroke stroke = new BasicStroke(2.0f);
g2d.setStroke(stroke);
// Draw x and y axis
g2d.drawLine(0, 300, 800, 300);
g2d.drawLine(50, 0, 50, 600);
// Draw previous lines
if (points.size() > 1) {
for (int i = 1; i < points.size(); i++) {
Point p1 = points.get(i-1);
Point p2 = points.get(i);
g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
}
}
// Draw current line
int x1 = 800 - lineOffset;
int y1 = (int) (300 - 100 * Math.sin(0));
int x2 = (int) (x + 800 - lineOffset);
int y2 = (int) (300 - 100 * sin(x));
//g2d.drawLine(x1, y1, x2, y2);
// Draw x and y values
String xStr = "x: " + String.format("%.2f", x);
String yStr = "y: " + String.format("%.2f", Math.sin(x));
Font font = new Font("Arial", Font.BOLD, 20);
g2d.setFont(font);
FontMetrics metrics = g2d.getFontMetrics(font);
int xStrWidth = metrics.stringWidth(xStr);
int yStrWidth = metrics.stringWidth(yStr);
int xStrHeight = metrics.getHeight();
int yStrHeight = metrics.getHeight();
g2d.drawString(xStr, 800- xStrWidth - MARGIN, 600- yStrHeight - MARGIN);
g2d.drawString(yStr, 800- yStrWidth - MARGIN, 600- MARGIN);
// Draw points and curve
points.add(new Point(x2, y2));
if (points.size() > MAX_POINTS) {
points.remove(0);
}
if (points.size() > 1) {
g2d.setColor(Color.BLACK);
for (int i = 1; i < points.size(); i++) {
Point p1 = points.get(i - 1);
Point p2 = points.get(i);
g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
}
g2d.setColor(Color.BLACK);
}
}
};
// Add label to panel
label = new JLabel();
panel.add(label);
label.setBounds(10, 10, 100, 20);
add(panel);
// Generate initial data
Random random = new Random();
for (int i = 0; i < NUM_POINTS; i++) {
double value = random.nextDouble();
data.add(value);
}
// Start timer to update curve
Timer timer = new Timer(POINT_DELAY, e -> {
// Update data
for (int i = 0; i < currentPoint; i++) {
data.set(i, data.get(i + 1));
}
data.set(currentPoint, Math.random());
// Increment x and currentPoint, and move line to left
x += 0.1;
currentPoint++;
lineOffset += 1;
// Redraw panel
panel.repaint();
});
timer.start();
//this.setVisible(true);
// Center the frame on the screen
setLocationRelativeTo(null);
}
}
public static void main(String[] args) {
DynamicCurveSystem dc = new DynamicCurveSystem();
JFrame frame=new JFrame("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DynamicCurveSystem.TryJButton panel= new DynamicCurveSystem.TryJButton() {
@Override
public void actionPerformed(ActionEvent e) {
}
};
frame.getContentPane().add(panel);
frame.setSize(300,80);
DynamicCurveSystem.TryJButton tryJButton = new DynamicCurveSystem.TryJButton();
frame.add(tryJButton);
frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static java.lang.Math.sin;
public class DynamicCurveSystemcos extends JFrame {
private static final int WIDTH = 960;
private static final int HEIGHT = 768;
private static final int MARGIN = 50;
private static final int NUM_POINTS = 1000000;
private static final int POINT_DELAY = 10;
private static final int MAX_POINTS = 1000000;
private final List<Double> data = new ArrayList<>();
private final List<Point> points = new ArrayList<>();
private double x = 0;
private int currentPoint = 0;
private int lineOffset = 0;
private final JLabel label;
public static class TryJButton extends JPanel implements ActionListener {
protected JButton b1, b2, b3, b4;
public TryJButton() {
b1 = new JButton("y=sinx");
b2 = new JButton("y=cosx");
b3 = new JButton("y=x");
b4 = new JButton("y=2x");
add(b1);add(b2);add(b3);add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
// Create and show DynamicCurveSystem window
Main.DynamicCurveSystem dc = new Main.DynamicCurveSystem();
dc.setVisible(true);
}
if (e.getSource() == b2) {
// Create and show DynamicCurveSystem window
DynamicCurveSystemcos dc = new DynamicCurveSystemcos();
dc.setVisible(true);
}
if (e.getSource() == b3) {
// Create and show DynamicCurveSystem window
Main.DynamicCurveSystem dc = new Main.DynamicCurveSystem();
dc.setVisible(true);
}
if (e.getSource() == b4) {
// Create and show DynamicCurveSystem window
Main.DynamicCurveSystem dc = new Main.DynamicCurveSystem();
dc.setVisible(true);
}
}
}
public DynamicCurveSystemcos() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("Dynamic Curve System");
setSize(WIDTH, HEIGHT);
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Stroke stroke = new BasicStroke(2.0f);
g2d.setStroke(stroke);
// Draw x and y axis
g2d.drawLine(0, 300, 800, 300);
g2d.drawLine(50, 0, 50, 600);
// Draw previous lines
if (points.size() > 1) {
for (int i = 1; i < points.size(); i++) {
Point p1 = points.get(i-1);
Point p2 = points.get(i);
g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
}
}
// Draw current line
int x1 = 800 - lineOffset;
int y1 = (int) (300 - 100 * Math.cos(0));
int x2 = (int) (x + 800 - lineOffset);
int y2 = (int) (300 - 100 * Math.cos(x));
//g2d.drawLine(x1, y1, x2, y2);
// Draw x and y values
String xStr = "x: " + String.format("%.2f", x);
String yStr = "y: " + String.format("%.2f", Math.cos(x));
Font font = new Font("Arial", Font.BOLD, 20);
g2d.setFont(font);
FontMetrics metrics = g2d.getFontMetrics(font);
int xStrWidth = metrics.stringWidth(xStr);
int yStrWidth = metrics.stringWidth(yStr);
int xStrHeight = metrics.getHeight();
int yStrHeight = metrics.getHeight();
g2d.drawString(xStr, 800- xStrWidth - MARGIN, 600- yStrHeight - MARGIN);
g2d.drawString(yStr, 800- yStrWidth - MARGIN, 600- MARGIN);
// Draw points and curve
points.add(new Point(x2, y2));
if (points.size() > MAX_POINTS) {
points.remove(0);
}
if (points.size() > 1) {
g2d.setColor(Color.BLACK);
for (int i = 1; i < points.size(); i++) {
Point p1 = points.get(i - 1);
Point p2 = points.get(i);
g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
}
g2d.setColor(Color.BLACK);
}
}
};
// Add label to panel
label = new JLabel();
panel.add(label);
label.setBounds(10, 10, 100, 20);
add(panel);
// Generate initial data
Random random = new Random();
for (int i = 0; i < NUM_POINTS; i++) {
double value = random.nextDouble();
data.add(value);
}
// Start timer to update curve
Timer timer = new Timer(POINT_DELAY, e -> {
// Update data
for (int i = 0; i < currentPoint; i++) {
data.set(i, data.get(i + 1));
}
data.set(currentPoint, Math.random());
// Increment x and currentPoint, and move line to left
x += 0.1;
currentPoint++;
lineOffset += 1;
// Redraw panel
panel.repaint();
});
timer.start();
//this.setVisible(true);
// Center the frame on the screen
setLocationRelativeTo(null);
}
}
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class DynamicCurveSystemx extends JFrame {
private static final int WIDTH = 960;
private static final int HEIGHT = 768;
private static final int MARGIN = 50;
private static final int NUM_POINTS = 100000;
private static final int POINT_DELAY = 10;
private static final int LINE_LENGTH = 300;
private List<Double> data = new ArrayList<>();
private double x = 0;
private int currentPoint = 0;
private int lineOffset = 0;
private JLabel label;
public DynamicCurveSystemx() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("Dynamic Curve System");
setSize(WIDTH, HEIGHT);
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Stroke stroke = new BasicStroke(2.0f);
g2d.setStroke(stroke);
// Draw x and y axis
g2d.drawLine(0, 300, 800, 300);
g2d.drawLine(50, 0, 50, 600);
// Draw current line
int x1 = 800 - lineOffset;
int y1 = 300;
int x2 = (int) (x + 800 - lineOffset);
int y2 = (int) (300 - x);
g2d.drawLine(x1, y1, x2, y2);
// Draw x and y values
String xStr = "x: " + String.format("%.2f", x);
String yStr = "y: " + String.format("%.2f", x);
Font font = new Font("Arial", Font.BOLD, 20);
g2d.setFont(font);
FontMetrics metrics = g2d.getFontMetrics(font);
int xStrWidth = metrics.stringWidth(xStr);
int yStrWidth = metrics.stringWidth(yStr);
int xStrHeight = metrics.getHeight();
int yStrHeight = metrics.getHeight();
g2d.drawString(xStr, 800 - xStrWidth - MARGIN, 600 - yStrHeight - MARGIN);
g2d.drawString(yStr, 800 - yStrWidth - MARGIN, 600 - MARGIN);
}
};
// Add label to panel
label = new JLabel();
panel.add(label);
label.setBounds(10, 10, 100, 20);
add(panel);
// Generate initial data
Random random = new Random();
for (int i = 0; i < NUM_POINTS; i++) {
double value = random.nextDouble();
data.add(value);
}
// Start timer to update curve
Timer timer = new Timer(POINT_DELAY, e -> {
// Update data
for (int i = 0; i < currentPoint; i++) {
data.set(i, data.get(i + 1));
}
data.set(currentPoint, Math.random());
// Increment x and currentPoint, and move line to left
x += 1;
currentPoint++;
lineOffset += 1;
// Redraw panel
panel.repaint();
});
timer.start();
this.setVisible(true);
// Center the frame on the screen
setLocationRelativeTo(null);
}
}
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class DynamicCurveSystem2x extends JFrame {
private static final int WIDTH = 960;
private static final int HEIGHT = 768;
private static final int MARGIN = 50;
private static final int NUM_POINTS = 100000;
private static final int POINT_DELAY = 10;
private static final int LINE_LENGTH = 300;
private List<Double> data = new ArrayList<>();
private double x = 0;
private int currentPoint = 0;
private int lineOffset = 0;
private JLabel label;
public DynamicCurveSystem2x() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("Dynamic Curve System");
setSize(WIDTH, HEIGHT);
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Stroke stroke = new BasicStroke(2.0f);
g2d.setStroke(stroke);
// Draw x and y axis
g2d.drawLine(0, 300, 800, 300);
g2d.drawLine(50, 0, 50, 600);
// Draw current line
int x1 = 800 - lineOffset;
int y1 = 300;
int x2 = (int) (x + 800 - lineOffset);
int y2 = (int) (300 - 2*x);
g2d.drawLine(x1, y1, x2, y2);
// Draw x and y values
String xStr = "x: " + String.format("%.2f", x);
String yStr = "y: " + String.format("%.2f", 2*x);
Font font = new Font("Arial", Font.BOLD, 20);
g2d.setFont(font);
FontMetrics metrics = g2d.getFontMetrics(font);
int xStrWidth = metrics.stringWidth(xStr);
int yStrWidth = metrics.stringWidth(yStr);
int xStrHeight = metrics.getHeight();
int yStrHeight = metrics.getHeight();
g2d.drawString(xStr, 800 - xStrWidth - MARGIN, 600 - yStrHeight - MARGIN);
g2d.drawString(yStr, 800 - yStrWidth - MARGIN, 600 - MARGIN);
}
};
// Add label to panel
label = new JLabel();
panel.add(label);
label.setBounds(10, 10, 100, 20);
add(panel);
// Generate initial data
Random random = new Random();
for (int i = 0; i < NUM_POINTS; i++) {
double value = random.nextDouble();
data.add(value);
}
// Start timer to update curve
Timer timer = new Timer(POINT_DELAY, e -> {
// Update data
for (int i = 0; i < currentPoint; i++) {
data.set(i, data.get(i + 1));
}
data.set(currentPoint, Math.random());
// Increment x and currentPoint, and move line to left
x += 1;
currentPoint++;
lineOffset += 1;
// Redraw panel
panel.repaint();
});
timer.start();
this.setVisible(true);
// Center the frame on the screen
setLocationRelativeTo(null);
}
}