Skip to content

Instantly share code, notes, and snippets.

@tbenjis
Last active January 2, 2018 20:02
Show Gist options
  • Save tbenjis/10978987 to your computer and use it in GitHub Desktop.
Save tbenjis/10978987 to your computer and use it in GitHub Desktop.
Simple graph for queuing in java
/**
** Simple Queuing, blocking probability with graph
** Author: Tunde
**/
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Queuing extends JPanel {
private int width = 800;
private int heigth = 400;
private int padding = 25;
private int labelPadding = 25;
private Color lineColor = new Color(44, 102, 230, 180);
private Color pointColor = new Color(100, 100, 100, 180);
private Color gridColor = new Color(200, 200, 200, 200);
private static final Stroke GRAPH_STROKE = new BasicStroke(2f);
private int pointWidth = 4;
private int numberYDivisions = 10;
private List<Double> values;
public Queuing(List<Double> values) {
this.values = values;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
double xScale = ((double) getWidth() - (2 * padding) - labelPadding)
/ (values.size() - 1);
double yScale = ((double) getHeight() - 2 * padding - labelPadding)
/ (getMaxValue() - getMinScore());
List<Point> graphPoints = new ArrayList<Point>();
for (int i = 0; i < values.size(); i++) {
int x1 = (int) (i * xScale + padding + labelPadding);
int y1 = (int) ((getMaxValue() - values.get(i)) * yScale + padding);
graphPoints.add(new Point(x1, y1));
}
// draw white background
g2.setColor(Color.WHITE);
g2.fillRect(padding + labelPadding, padding, getWidth() - (2 * padding)
- labelPadding, getHeight() - 2 * padding - labelPadding);
g2.setColor(Color.BLACK);
// create hatch marks and grid lines for y axis.
for (int i = 0; i < numberYDivisions + 1; i++) {
int x0 = padding + labelPadding;
int x1 = pointWidth + padding + labelPadding;
int y0 = getHeight()
- ((i * (getHeight() - padding * 2 - labelPadding))
/ numberYDivisions + padding + labelPadding);
int y1 = y0;
if (values.size() > 0) {
g2.setColor(gridColor);
g2.drawLine(padding + labelPadding + 1 + pointWidth, y0,
getWidth() - padding, y1);
g2.setColor(Color.BLACK);
String yLabel = ((int) ((getMinScore() + (getMaxValue() - getMinScore())
* ((i * 1.0) / numberYDivisions)) * 100))
/ 100.0 + "";
FontMetrics metrics = g2.getFontMetrics();
int labelWidth = metrics.stringWidth(yLabel);
g2.drawString(yLabel, x0 - labelWidth - 5,
y0 + (metrics.getHeight() / 2) - 3);
}
g2.drawLine(x0, y0, x1, y1);
}
// and for x axis
for (int i = 0; i < values.size(); i++) {
if (values.size() > 1) {
int x0 = i * (getWidth() - padding * 2 - labelPadding)
/ (values.size() - 1) + padding + labelPadding;
int x1 = x0;
int y0 = getHeight() - padding - labelPadding;
int y1 = y0 - pointWidth;
if ((i % ((int) ((values.size() / 20.0)) + 1)) == 0) {
g2.setColor(gridColor);
g2.drawLine(x0, getHeight() - padding - labelPadding - 1
- pointWidth, x1, padding);
g2.setColor(Color.BLACK);
String xLabel = i + "";
FontMetrics metrics = g2.getFontMetrics();
int labelWidth = metrics.stringWidth(xLabel);
g2.drawString(xLabel, x0 - labelWidth / 2,
y0 + metrics.getHeight() + 3);
}
g2.drawLine(x0, y0, x1, y1);
}
}
// create x and y axes
g2.drawLine(padding + labelPadding, getHeight() - padding
- labelPadding, padding + labelPadding, padding);
g2.drawLine(padding + labelPadding, getHeight() - padding
- labelPadding, getWidth() - padding, getHeight() - padding
- labelPadding);
Stroke oldStroke = g2.getStroke();
g2.setColor(lineColor);
g2.setStroke(GRAPH_STROKE);
for (int i = 0; i < graphPoints.size() - 1; i++) {
int x1 = graphPoints.get(i).x;
int y1 = graphPoints.get(i).y;
int x2 = graphPoints.get(i + 1).x;
int y2 = graphPoints.get(i + 1).y;
g2.drawLine(x1, y1, x2, y2);
}
g2.setStroke(oldStroke);
g2.setColor(pointColor);
for (int i = 0; i < graphPoints.size(); i++) {
int x = graphPoints.get(i).x - pointWidth / 2;
int y = graphPoints.get(i).y - pointWidth / 2;
int ovalW = pointWidth;
int ovalH = pointWidth;
g2.fillOval(x, y, ovalW, ovalH);
}
}
// @Override
// public Dimension getPreferredSize() {
// return new Dimension(width, heigth);
// }
private double getMinScore() {
double minScore = Double.MAX_VALUE;
for (Double score : values) {
minScore = Math.min(minScore, score);
}
return minScore;
}
private double getMaxValue() {
double maxValue = Double.MIN_VALUE;
for (Double score : values) {
maxValue = Math.max(maxValue, score);
}
return maxValue;
}
public void setValues(List<Double> values) {
this.values = values;
invalidate();
this.repaint();
}
public List<Double> getValues() {
return values;
}
private static void drawGraph() {
List<Double> rt_values = new ArrayList<Double>();
List<Double> p_values = new ArrayList<Double>();
List<Double> n_values = new ArrayList<Double>();
// the variables
double p, n, rt;
double step = 0.1;
double lamda = 0; // lamda value
// get step
int maxDataPoints = 10;
DecimalFormat df = new DecimalFormat("#.##"); // 2 decimal place
for (int i = 0; i < maxDataPoints; i++) {
// calculate n
p = i * step;
p = Double.parseDouble(df.format(p));
p_values.add(p);
// add n values
n = p / (1 - p);
n = Double.parseDouble(df.format(n));
n_values.add(n);
// calculate lamda
if (i == 0) {
lamda = 0;
// add rt values
rt = 0;
} else {
lamda = p * maxDataPoints;
// add rt values
rt = n / lamda;
}
rt = Double.parseDouble(df.format(rt));
rt_values.add(rt);
}
// display the table
String table = "P\tN\tRT";
for (int j = 0; j < p_values.size(); j++) {
table += "\n" + p_values.get(j) + "\t" + n_values.get(j) + "\t"
+ rt_values.get(j);
}
// show the results
System.out.println("THE RESULTS\n" + table);
// we are using the n constants
Queuing mainPanel = new Queuing(n_values);
mainPanel.setPreferredSize(new Dimension(500, 400));
JFrame frame = new JFrame("N Graph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// we are using the rt constants
Queuing mainPanel1 = new Queuing(rt_values);
mainPanel1.setPreferredSize(new Dimension(500, 400));
JFrame frame1 = new JFrame("RT Graph");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.getContentPane().add(mainPanel1);
frame1.pack();
frame1.setLocation(500, 300);
frame1.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
drawGraph();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment