knock_plugin_clean - clean changes #6989

only:encapsulation of swing
This commit is contained in:
Andrey 2024-10-21 09:39:33 -04:00
parent aa99badad0
commit f453e9ad03
2 changed files with 150 additions and 162 deletions

View File

@ -7,10 +7,100 @@ import java.awt.image.BufferedImage;
import static org.apache.commons.math3.util.Precision.round;
public class KnockCanvas extends JComponent implements ComponentListener {
public class KnockCanvas {
JComponent dd = this;
//--------------------------------------
private JComponent component = new JComponent() {
@Override
public void paint(Graphics g) {
super.paint(g);
Dimension size = component.getSize();
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
float bx = (float)width / (float)SPECTROGRAM_X_AXIS_SIZE;
int offset = (int)(currentIndexXAxis * bx);
// flip buffers
g.drawImage(bufferedImage,
0, 0, size.width - offset, size.height,
offset, 0, size.width, size.height,
null);
g.drawImage(bufferedImage, size.width - offset, 0, size.width, size.height,null);
g.setColor(Color.RED);
int line = (int)(currentIndexXAxis * bx);
g.drawLine(line, 0, line, height);
Font f = g.getFont();
g.setFont(new Font(f.getName(), Font.CENTER_BASELINE, g.getFont().getSize() - 4));
g.setColor(Color.YELLOW);
for(int i = 0; i < yAxisHz.length; ++i) {
var y = hzToYScreen(yAxisHz[i], height);
g.setColor(Color.orange);
g.fillRect(0, y, 30, 1);
var hz = yAxisHz[i];
g.drawString(Double.valueOf(round(hz, 1)).toString(), 35, y);
}
mouseFrequency = (float)YScreenToHz(mouse_y, height);
int mouseSpecX = canvasXToSpectrogramSpace(mouse_x);
int mouseSpecY = canvasYToSpectrogramSpace(mouse_y);
mouseAmplitude = specrtogram[mouseSpecX][mouseSpecY];
//Font f = g.getFont();
g.setFont(new Font(f.getName(), Font.BOLD, g.getFont().getSize()));
g.setColor(Color.YELLOW);
var currentX = width / 4;
g.drawString("[current]", currentX, 10);
g.drawString(Float.valueOf(currentFrequency).toString() + " Hz", currentX, 30);
g.drawString(Float.valueOf(currentAmplitude).toString() + " Amp", currentX, 50);
g.setColor(Color.RED);
g.drawString("[peak]", currentX * 2, 10);
g.drawString(Float.valueOf(peakFrequency).toString() + " Hz", currentX * 2, 30);
g.drawString(Float.valueOf(peakAmplitude).toString() + " Amp", currentX * 2, 50);
g.setColor(Color.ORANGE);
g.drawString("[mouse]", currentX * 3, 10);
g.drawString(Float.valueOf(mouseFrequency).toString() + " Hz", currentX * 3, 30);
g.drawString(Float.valueOf(mouseAmplitude).toString() + " Amp", currentX * 3, 50);
g.setFont(f);
g.setColor(Color.green);
g.fillOval(spectrogramSpaceToCanvasX(peakX)-5, spectrogramSpaceToCanvasY(peakY)-5, 10, 10);
g.setColor(Color.WHITE);
var yy = hzToYScreen(currentFrequency, height);
g.fillRect(0, yy, width, 1);
g.setColor(Color.DARK_GRAY);
g.fillRect(0, mouse_y, width, 1);
//for test
//var yy2 = hzToYScreen(8117.68, height);
//g.fillRect(0, yy2, width, 1);
}
};
private ComponentListener componentListener = new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
bufferedImage = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB);
bufferedGraphics = bufferedImage.createGraphics();
}
};
private BufferedImage bufferedImage;
private Graphics2D bufferedGraphics;
@ -51,15 +141,11 @@ public class KnockCanvas extends JComponent implements ComponentListener {
public KnockCanvas() {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
dd.repaint();
}
});
SwingUtilities.invokeLater(() -> component.repaint());
bufferedImage = new BufferedImage(640,480, BufferedImage.TYPE_INT_RGB);
bufferedGraphics = bufferedImage.createGraphics();
this.addComponentListener(this);
component.addComponentListener(componentListener);
//linear-gradient(to right, #000000, #290d1a, #490b32, #670353, #81007b, #a60085, #ca008b, #ef008f, #ff356b, #ff6947, #ff9a22, #ffc700);
colorspace = new Color[] {
@ -371,104 +457,4 @@ public class KnockCanvas extends JComponent implements ComponentListener {
int spectrogramSpaceX = this.canvasXToSpectrogramSpace(this.mouse_x);
return specrtogram[spectrogramSpaceX];
}
@Override
public void paint(Graphics g) {
super.paint(g);
Dimension size = getSize();
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
float bx = (float)width / (float)SPECTROGRAM_X_AXIS_SIZE;
int offset = (int)(currentIndexXAxis * bx);
// flip buffers
g.drawImage(bufferedImage,
0, 0, size.width - offset, size.height,
offset, 0, size.width, size.height,
null);
g.drawImage(bufferedImage, size.width - offset, 0, size.width, size.height,null);
g.setColor(Color.RED);
int line = (int)(currentIndexXAxis * bx);
g.drawLine(line, 0, line, height);
Font f = g.getFont();
g.setFont(new Font(f.getName(), Font.CENTER_BASELINE, g.getFont().getSize() - 4));
g.setColor(Color.YELLOW);
for(int i = 0; i < yAxisHz.length; ++i) {
var y = hzToYScreen(yAxisHz[i], height);
g.setColor(Color.orange);
g.fillRect(0, y, 30, 1);
var hz = this.yAxisHz[i];
g.drawString(Double.valueOf(round(hz, 1)).toString(), 35, y);
}
mouseFrequency = (float)YScreenToHz(mouse_y, height);
int mouseSpecX = canvasXToSpectrogramSpace(mouse_x);
int mouseSpecY = canvasYToSpectrogramSpace(mouse_y);
mouseAmplitude = specrtogram[mouseSpecX][mouseSpecY];
//Font f = g.getFont();
g.setFont(new Font(f.getName(), Font.BOLD, g.getFont().getSize()));
g.setColor(Color.YELLOW);
var currentX = width / 4;
g.drawString("[current]", currentX, 10);
g.drawString(Float.valueOf(currentFrequency).toString() + " Hz", currentX, 30);
g.drawString(Float.valueOf(currentAmplitude).toString() + " Amp", currentX, 50);
g.setColor(Color.RED);
g.drawString("[peak]", currentX * 2, 10);
g.drawString(Float.valueOf(peakFrequency).toString() + " Hz", currentX * 2, 30);
g.drawString(Float.valueOf(peakAmplitude).toString() + " Amp", currentX * 2, 50);
g.setColor(Color.ORANGE);
g.drawString("[mouse]", currentX * 3, 10);
g.drawString(Float.valueOf(mouseFrequency).toString() + " Hz", currentX * 3, 30);
g.drawString(Float.valueOf(mouseAmplitude).toString() + " Amp", currentX * 3, 50);
g.setFont(f);
g.setColor(Color.green);
g.fillOval(spectrogramSpaceToCanvasX(peakX)-5, spectrogramSpaceToCanvasY(peakY)-5, 10, 10);
g.setColor(Color.WHITE);
var yy = hzToYScreen(currentFrequency, height);
g.fillRect(0, yy, width, 1);
g.setColor(Color.DARK_GRAY);
g.fillRect(0, mouse_y, width, 1);
//for test
//var yy2 = hzToYScreen(8117.68, height);
//g.fillRect(0, yy2, width, 1);
}
@Override
public void componentHidden(ComponentEvent e) {
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentResized(ComponentEvent e) {
bufferedImage = new BufferedImage(getWidth(),getHeight(), BufferedImage.TYPE_INT_RGB);
bufferedGraphics = bufferedImage.createGraphics();
}
@Override
public void componentShown(ComponentEvent e) {
}
}

View File

@ -5,18 +5,48 @@ import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import static org.apache.commons.math3.util.Precision.round;
public class KnockMagnitudeCanvas extends JComponent implements ComponentListener {
public class KnockMagnitudeCanvas {
JComponent dd = this;
//--------------------------------------
private JComponent component = new JComponent() {
@Override
public void paint(Graphics g) {
super.paint(g);
Dimension size = getSize();
// flip buffers
g.drawImage(bufferedImage, 0, 0, size.width, size.height, null);
}
};
private ComponentListener componentListener = new ComponentListener() {
@Override
public void componentHidden(ComponentEvent e) {
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentResized(ComponentEvent e) {
bufferedImage = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB);
bufferedGraphics = bufferedImage.createGraphics();
}
@Override
public void componentShown(ComponentEvent e) {
}
};
private BufferedImage bufferedImage;
private Graphics2D bufferedGraphics;
public double yAxisHz[];
public double[] yAxisHz;
int yAxisFequencyStart = -1;
float yAxisFequencyStep = -1;
@ -26,15 +56,13 @@ public class KnockMagnitudeCanvas extends JComponent implements ComponentListene
public KnockMagnitudeCanvas() {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
dd.repaint();
}
SwingUtilities.invokeLater(() -> {
component.repaint();
});
bufferedImage = new BufferedImage(640,480, BufferedImage.TYPE_INT_RGB);
bufferedImage = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB);
bufferedGraphics = bufferedImage.createGraphics();
this.addComponentListener(this);
component.addComponentListener(componentListener);
yAxisHz = new double[64]; // protocol size
}
@ -44,7 +72,7 @@ public class KnockMagnitudeCanvas extends JComponent implements ComponentListene
this.yAxisFequencyStart = start;
if(needSetup) {
if (needSetup) {
setupFrequencyyAxis();
}
}
@ -55,19 +83,19 @@ public class KnockMagnitudeCanvas extends JComponent implements ComponentListene
this.yAxisFequencyStep = step;
if(needSetup) {
if (needSetup) {
setupFrequencyyAxis();
}
}
public void setupFrequencyyAxis() {
if(this.yAxisFequencyStep < 0 || this.yAxisFequencyStart < 0) {
if (this.yAxisFequencyStep < 0 || this.yAxisFequencyStart < 0) {
return;
}
for(var i = 0; i < 64; ++i) {
float y = (float)this.yAxisFequencyStart + (this.yAxisFequencyStep * (float)i);
for (var i = 0; i < 64; ++i) {
float y = (float) this.yAxisFequencyStart + (this.yAxisFequencyStep * (float) i);
this.yAxisHz[i] = y;
}
}
@ -77,10 +105,10 @@ public class KnockMagnitudeCanvas extends JComponent implements ComponentListene
int max = 256;
int index = -1;
for(int i = 0; i < 64; ++i) {
for (int i = 0; i < 64; ++i) {
int value = yPoints[i];
if(value < max) {
if (value < max) {
max = value;
index = i;
}
@ -94,18 +122,18 @@ public class KnockMagnitudeCanvas extends JComponent implements ComponentListene
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
bufferedGraphics.clearRect(0,0, width, height);
bufferedGraphics.clearRect(0, 0, width, height);
float bx = (float)width / (float)64;
float bx = (float) width / (float) 64;
for(int i = 0; i < 64; ++i) {
for (int i = 0; i < 64; ++i) {
xPoints[i] = (int)(bx * i);
xPoints[i] = (int) (bx * i);
float normalized = magnitudes[i]/255.f;
float normalized = magnitudes[i] / 255.f;
float y = height * normalized;
yPoints[i] = (int)(height - y);
yPoints[i] = (int) (height - y);
}
//last point 2 always in corners
xPoints[64] = xPoints[63];
@ -125,7 +153,7 @@ public class KnockMagnitudeCanvas extends JComponent implements ComponentListene
bufferedGraphics.drawLine(xLine, 0, xLine, height);
double hz = this.yAxisHz[index];
bufferedGraphics.drawString(Double.valueOf(round(hz, 1)).toString(), xLine, 10);
bufferedGraphics.drawString(Double.valueOf(round(hz, 1)).toString(), xLine, 10);
Font defaultFont = bufferedGraphics.getFont();
Font font = new Font(null, Font.PLAIN, 14);
@ -134,42 +162,16 @@ public class KnockMagnitudeCanvas extends JComponent implements ComponentListene
Font rotatedFont = font.deriveFont(rotate90);
bufferedGraphics.setFont(rotatedFont);
for(int i = 0; i < 64; ++i) {
for (int i = 0; i < 64; ++i) {
int x = (int) (bx * i);
double hzZxis = this.yAxisHz[i];
bufferedGraphics.setColor(Color.white);
bufferedGraphics.drawLine(x, height, x, height - 10);
bufferedGraphics.drawString(Double.valueOf(round(hzZxis, 1)).toString(), x, height - 20);
bufferedGraphics.drawString(Double.valueOf(round(hzZxis, 1)).toString(), x, height - 20);
}
bufferedGraphics.setFont(defaultFont);
this.repaint();
component.repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
Dimension size = getSize();
// flip buffers
g.drawImage(bufferedImage, 0, 0, size.width, size.height,null);
}
@Override
public void componentHidden(ComponentEvent e) {
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentResized(ComponentEvent e) {
bufferedImage = new BufferedImage(getWidth(),getHeight(), BufferedImage.TYPE_INT_RGB);
bufferedGraphics = bufferedImage.createGraphics();
}
@Override
public void componentShown(ComponentEvent e) {
}
}