我使用JPanel在时域中绘制(co)正弦信号并覆盖其paintComponent(Graphics g)。 当我调整大小时,我的应用程序开始闪烁并显示黑色背景,直到调整大小没有完成。 有没有办法防止这种闪烁? 提前致谢。
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Dimension;
public class AnotherSignalGraph extends JPanel {
private double offset = 0.0;
private double scaleX = 0.0;
private double scaleY = 0.0;
protected double maxX = 0.0;
private double minX = 0.0;
private double maxY = 0.0;
private double minY = 0.0;
private Point[] points = null;
private Signal s = null;
private void initPoints(int pointsToGenerate) {
this.points = new Point[pointsToGenerate];
double t = 0;
double dt = this.maxX / pointsToGenerate;
for (int i=0; i<pointsToGenerate; i++, t += dt)
points[i] = new Point(t, this.s.v(t));
}
public AnotherSignalGraph(int pointsToGenerate, int width, int height) {
super.setPreferredSize(new Dimension(width, height));
this.s = new Sinusoid(1000, 10, 0);
this.maxX = (1 / this.s.frequency) * 2;
this.maxY = this.s.amplitude;
this.minY = -this.maxY;
this.offset = super.getHeight() / 2.0;
this.initPoints(pointsToGenerate);
this.updateScale();
}
private void updateScale() {
this.scaleX = super.getWidth() / (this.maxX - this.minX);
this.scaleY = super.getHeight() / (this.maxY - this.minY);
this.offset = super.getHeight() / 2.0;
}
private void drawSignal(Graphics2D g2D) {
int x1, x2, y1, y2;
g2D.setColor(Color.blue);
g2D.setClip(0, (int)(this.offset - (this.s.amplitude * this.scaleY)), (int)(super.getWidth()), (int)((this.s.amplitude * 2 * this.scaleY) + 2));
for (int i=0; i<this.points.length - 1; i++) {
x1 = (int)(this.scaleX * this.points[i].getX());
y1 = (int)(this.offset - (this.scaleY * this.points[i].getY()));
x2 = (int)(scaleX * this.points[i + 1].getX());
y2 = (int)(this.offset - (this.scaleY * this.points[i + 1].getY()));
g2D.drawLine(x1, y1, x2, y2);
}
}
private void graphIt(Graphics2D g2D) {
this.updateScale();
this.drawSignal(g2D);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
this.graphIt(g2D);
}
private class Point {
private double x;
private double y;
public Point(double x, double y){
this.x = x;
this.y = y;
}
public double getX(){
return this.x;
}
public double getY(){
return this.y;
}
}
private abstract class Signal {
double frequency; //Signal frequency in Hz
double amplitude; //Signal amplitude in Volt
double initPhase; //Signal initial phase in radians
public Signal(double frequency, double amplitude, double degInitPhase) throws IllegalArgumentException{ //degInitPhase parameter specifies the initial angle phase in degrees
this.frequency = frequency;
this.amplitude = amplitude;
this.initPhase = degInitPhase;
}
public abstract double v(double t);
}
private class Sinusoid extends Signal {
public Sinusoid(double frequency, double amplitude, double degInitPhase){ //degInitPhase parameter specifies the initial angle phase in degrees
super(frequency, amplitude, degInitPhase);
}
@Override
public double v(double t){ //t parameter indicates the specific time frame in seconds
return super.amplitude * Math.sin((super.frequency * Math.PI * t) + super.initPhase);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new AnotherSignalGraph(50000, 600, 600));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}