Created
April 30, 2015 11:13
-
-
Save nicolaferraro/510a5c36ab1b80f7dd3f to your computer and use it in GitHub Desktop.
Plot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.eng.java.util; | |
import javax.swing.*; | |
import java.awt.*; | |
public class Plot { | |
private double[] matrix; | |
private int columns; | |
public Plot(double[] matrix, int columns) { | |
this.matrix = matrix; | |
this.columns = columns; | |
} | |
private Dimension getDimensions() { | |
return new Dimension(columns, matrix.length / columns); | |
} | |
public void show() { | |
JFrame.setDefaultLookAndFeelDecorated(true); | |
JFrame dia = new JFrame(); | |
dia.setTitle("Plot"); | |
dia.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
Container container = dia.getContentPane(); | |
JPanel cont = new JPanel() { | |
private static final long serialVersionUID = 1L; | |
@Override | |
public void paint(Graphics g) { | |
super.paint(g); | |
for(int i=0; i<matrix.length; i++) { | |
int x = i % columns; | |
int y = i / columns; | |
int col; | |
if(Double.isNaN(matrix[i]) || Double.isInfinite(matrix[i])) { | |
col = 0; | |
} else { | |
col = Math.min(Math.max((int)(255-255*matrix[i]), 0), 255); | |
} | |
g.setColor(new Color(255-col, 255-col, 255-col)); | |
g.drawLine(x, y, x+1, y+1); | |
} | |
} | |
}; | |
Dimension dim = getDimensions(); | |
cont.setSize(dim); | |
cont.setMaximumSize(dim); | |
cont.setMinimumSize(dim); | |
cont.setPreferredSize(dim); | |
container.add(cont); | |
dia.pack(); | |
dia.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment