记账本,C,Github,util

时间:2023-03-09 18:58:27
记账本,C,Github,util
package util;

import java.awt.Component;
import java.awt.Dimension; import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UnsupportedLookAndFeelException; import gui.panel.WorkingPanel; /**
* 居中面板
*
* @author 于修彦
*
*/
public class CenterPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private double rate; // 比例
private JComponent c; // 面板上将要居中的组件
private boolean stretch;// 是否缩放 public CenterPanel(double rate, boolean stretch) {
this.setLayout(null);
this.rate = rate;
this.stretch = stretch;
} public CenterPanel(double rate) {
this(rate, true);
} /**
* 重写重绘方法,在面板居中显示组件
*/
public void repaint() {
if (this.c != null) {
Dimension containerSize = this.getSize();// 面板大小
Dimension componentSize = this.c.getPreferredSize();// 获取组件调整之后的大小 if (this.stretch) {
// 设置这个组件大小
this.c.setSize((int) (containerSize.width * this.rate), (int) (containerSize.height * this.rate));
} else {
this.c.setSize(componentSize);// 保持原来大小不变
}
// 将组件放到居中位置
this.c.setLocation(containerSize.width / 2 - this.c.getWidth() / 2,
containerSize.height / 2 - this.c.getHeight() / 2); }
super.repaint();
} /**
* 显示组件
*
* @param p
* 组件
*/
public void show(JComponent p) {
this.c = p;
Component[] cs = this.getComponents();
Component[] arrayOfComponent1;
int j = (arrayOfComponent1 = cs).length;
for (int i = 0; i < j; i++) { // 清除面板上原来的组件
Component c = arrayOfComponent1[i];
remove(c);
}
add(p); if ((p instanceof WorkingPanel)) {
((WorkingPanel) p).updateData();
}
updateUI();
}
}

util1

package util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.util.List; import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel; import com.objectplanet.chart.BarChart;
import com.objectplanet.chart.Chart; import entity.Record; /**
* 生成柱状图
*
* @author 于修彦
*
*/
public class ChartUtil {
/**
* 横轴下面的文字
*
* @param rs
* 数据集
* @return 依据数据集生成的文字数组
*/
private static String[] sampleLabels(List<Record> rs) {
String[] sampleLabels = new String[rs.size()];
for (int i = 0; i < sampleLabels.length; i++) {
if (0 == i % 5)
sampleLabels[i] = String.valueOf(i + 1 + "日");
} return sampleLabels; } /**
* 柱状图的柱的数据高度
*
* @param rs
* 数据集
* @return 依据数据集生成的数据数组
*/
public static double[] sampleValues(List<Record> rs) {
double[] sampleValues = new double[rs.size()];
for (int i = 0; i < sampleValues.length; i++) {
sampleValues[i] = rs.get(i).getSpend();
} return sampleValues;
} /**
* 获取柱状图的图片
*
* @param rs
* 数据集
* @param width
* 宽度
* @param height
* 高度
* @return 结果图
*/
public static Image getImage(List<Record> rs, int width, int height) {
// 根据消费记录得到的样本数据
double[] sampleValues = sampleValues(rs);
// 根据消费记录得到的下方日期文本
String[] sampleLabels = sampleLabels(rs);
// 样本中的最大值
int max = max(sampleValues); // 数据颜色
Color[] sampleColors = new Color[] { ColorUtil.blueColor }; // 柱状图
BarChart chart = new BarChart(); // 设置样本个数
chart.setSampleCount(sampleValues.length);
// 设置样本数据
chart.setSampleValues(0, sampleValues);
// 设置文字
chart.setSampleLabels(sampleLabels);
// 设置样本颜色
chart.setSampleColors(sampleColors);
// 设置取值范围
chart.setRange(0, max * 1.2);
// 显示背景横线
chart.setValueLinesOn(true);
// 显示文字
chart.setSampleLabelsOn(true);
// 把文字显示在下方
chart.setSampleLabelStyle(Chart.BELOW); // 样本值的字体
chart.setFont("rangeLabelFont", new Font("Arial", Font.BOLD, 12));
// 显示图例说明
chart.setLegendOn(true);
// 把图例说明放在左侧
chart.setLegendPosition(Chart.LEFT);
// 图例说明中的文字
chart.setLegendLabels(new String[] { "月消费报表" });
// 图例说明的字体
chart.setFont("legendFont", new Font("Dialog", Font.BOLD, 13));
// 下方文字的字体
chart.setFont("sampleLabelFont", new Font("Dialog", Font.BOLD, 13));
// 图表中间背景颜色
chart.setChartBackground(Color.white);
// 图表整体背景颜色
chart.setBackground(ColorUtil.backgroundColor);
// 把图表转换为Image类型
Image im = chart.getImage(width, height);
return im;
} public static int max(double[] sampleValues) {
int max = 0;
for (double v : sampleValues) {
if (v > max)
max = (int) v;
}
return max; } private static String[] sampleLabels() {
String[] sampleLabels = new String[30]; for (int i = 0; i < sampleLabels.length; i++) {
if (0 == i % 5)
sampleLabels[i] = String.valueOf(i + 1 + "日");
}
return sampleLabels;
} public static Image getImage(int width, int height) {
// 模拟样本数据
double[] sampleValues = sampleValues();
// 下方显示的文字
String[] sampleLabels = sampleLabels();
// 样本中的最大值
int max = max(sampleValues); // 数据颜色
Color[] sampleColors = new Color[] { ColorUtil.blueColor }; // 柱状图
BarChart chart = new BarChart(); // 设置样本个数
chart.setSampleCount(sampleValues.length);
// 设置样本数据
chart.setSampleValues(0, sampleValues);
// 设置文字
chart.setSampleLabels(sampleLabels);
// 设置样本颜色
chart.setSampleColors(sampleColors);
// 设置取值范围
chart.setRange(0, max * 1.2);
// 显示背景横线
chart.setValueLinesOn(true);
// 显示文字
chart.setSampleLabelsOn(true);
// 把文字显示在下方
chart.setSampleLabelStyle(Chart.BELOW); // 样本值的字体
chart.setFont("rangeLabelFont", new Font("Arial", Font.BOLD, 12));
// 显示图例说明
chart.setLegendOn(true);
// 把图例说明放在左侧
chart.setLegendPosition(Chart.LEFT);
// 图例说明中的文字
chart.setLegendLabels(new String[] { "月消费报表" });
// 图例说明的字体
chart.setFont("legendFont", new Font("Dialog", Font.BOLD, 13));
// 下方文字的字体
chart.setFont("sampleLabelFont", new Font("Dialog", Font.BOLD, 13));
// 图表中间背景颜色
chart.setChartBackground(Color.white);
// 图表整体背景颜色
chart.setBackground(ColorUtil.backgroundColor);
// 把图表转换为Image类型
Image im = chart.getImage(width, height);
return im;
} private static double[] sampleValues() { double[] result = new double[30];
for (int i = 0; i < result.length; i++) {
result[i] = (int) (Math.random() * 300);
}
return result; } // public static void main(String[] args) {
// JPanel p = new JPanel();
// JLabel l = new JLabel();
// Image img = ChartUtil.getImage(400, 300);
// Icon icon = new ImageIcon(img);
// l.setIcon(icon);
// p.add(l);
// GUIUtil.showPanel(p);
// } }

util2

package util;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingWorker; public class CircleProgressBar extends JPanel { private int minimumProgress; private int maximumProgress; private int progress; private String progressText; private Color backgroundColor; private Color foregroundColor; public CircleProgressBar() {
minimumProgress = 0;
maximumProgress = 100;
progressText = "0%";
} public void paint(Graphics g) {
super.paint(g);
Graphics2D graphics2d = (Graphics2D) g;
// 开启抗锯齿
graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int x = 0;
int y = 0;
int width = 0;
int height = 0;
int fontSize = 0;
if (getWidth() >= getHeight()) {
x = (getWidth() - getHeight()) / 2 + 25;
y = 25;
width = getHeight() - 50;
height = getHeight() - 50;
fontSize = getWidth() / 8;
} else {
x = 25;
y = (getHeight() - getWidth()) / 2 + 25;
width = getWidth() - 50;
height = getWidth() - 50;
fontSize = getHeight() / 8;
}
graphics2d.setStroke(new BasicStroke(20.0f));
graphics2d.setColor(backgroundColor);
graphics2d.drawArc(x, y, width, height, 0, 360);
graphics2d.setColor(foregroundColor);
graphics2d.drawArc(x, y, width, height, 90,
-(int) (360 * ((progress * 1.0) / (maximumProgress - minimumProgress))));
graphics2d.setFont(new Font("黑体", Font.BOLD, fontSize));
FontMetrics fontMetrics = graphics2d.getFontMetrics();
int digitalWidth = fontMetrics.stringWidth(progressText);
int digitalAscent = fontMetrics.getAscent();
graphics2d.setColor(foregroundColor);
graphics2d.drawString(progressText, getWidth() / 2 - digitalWidth / 2, getHeight() / 2 + digitalAscent / 2);
} public int getProgress() {
return progress;
} public void setProgress(int progress) {
if (progress >= minimumProgress && progress <= maximumProgress)
this.progress = progress;
if (progress > maximumProgress)
this.progress = maximumProgress; this.progressText = String.valueOf(progress + "%"); this.repaint();
} public Color getBackgroundColor() {
return backgroundColor;
} public void setBackgroundColor(Color backgroundColor) {
this.backgroundColor = backgroundColor;
this.repaint();
} public Color getForegroundColor() {
return foregroundColor;
} public void setForegroundColor(Color foregroundColor) {
this.foregroundColor = foregroundColor;
this.repaint();
} }

util3