操作系统为windows11,在eclipse java ee里面用eclipse画图,拖动水平滚动条图像出出现重影,如果在显示设置里面选择缩放为225%就不会有重影,如果缩放小于225%,则出现重影,无法查明原因,请指教!谢谢!
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.io.IOException;
import org.apache.commons.csv.*;
import java.io.FileReader;
@SuppressWarnings("serial")
public class ReadCsvAndDraw extends JFrame {
public ReadCsvAndDraw() {
super();
MyPanelCsv myPanel = new MyPanelCsv();
myPanel.setPreferredSize(new Dimension(5000, 730));
myPanel.setSize(5000, 700);
//myPanel.updateUI();
myPanel.repaint();
JScrollPane sp = new JScrollPane();
this.setSize(new Dimension(1000, 730));
sp.setViewportView(myPanel);
sp.setSize(1000, 1000);
this.setLayout(new BorderLayout());
this.add(sp, BorderLayout.CENTER);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) throws IOException {
ReadCsvAndDraw draw = new ReadCsvAndDraw();
}
}
@SuppressWarnings("serial")
class MyPanelCsv extends JPanel {
@Override
public void paint(Graphics g) {
try {
File file = new File("C:\\Users\\lihh\\Desktop\\600519.csv");
FileReader reader = new FileReader(file);
CSVFormat format = CSVFormat.newFormat(',').withNullString("").withIgnoreSurroundingSpaces(true);
CSVParser parser = format.parse(reader);
ArrayList<String> listOfDate = new ArrayList<String>();
ArrayList<Double> listOfPrice = new ArrayList<Double>();
int row = 0;
for (CSVRecord record : parser) {
if (row > 0 && Double.valueOf(record.get(3)) > 0) {
listOfDate.add(record.get(0));
listOfPrice.add(Double.valueOf(record.get(3)));
}
row++;
}
Collections.reverse(listOfDate);
Collections.reverse(listOfPrice);
double highestPrice = Collections.max(listOfPrice);
double lowestPrice = Collections.min(listOfPrice);
String dateOfHighestPrice = listOfDate.get(listOfPrice.indexOf(highestPrice));
String dateOfLowestPrice = listOfDate.get(listOfPrice.indexOf(lowestPrice));
ArrayList<Integer> riseOrDescend = new ArrayList<Integer>();
for (int t = 1; t < listOfPrice.size(); t++) {
if (listOfPrice.get(t) - listOfPrice.get(t - 1) > 0)
riseOrDescend.add(1);
if (listOfPrice.get(t) - listOfPrice.get(t - 1) == 0)
riseOrDescend.add(0);
if (listOfPrice.get(t) - listOfPrice.get(t - 1) < 0)
riseOrDescend.add(-1);
}
HashMap<Integer, Integer> numOfRiseOrDescend = new HashMap<Integer, Integer>();
for (Integer k : riseOrDescend) {
if (numOfRiseOrDescend.get(k) == null) {
numOfRiseOrDescend.put(k, 1);
} else {
numOfRiseOrDescend.put(k, numOfRiseOrDescend.get(k) + 1);
}
}
int riseIndex = -1;
int maxRiseDays = 0;
int currentRiseIndex = -1;
int currentMaxRiseDays = 0;
int descendIndex = -1;
int maxDescendDays = 0;
int currentDescendIndex = -1;
int currentMaxDescendDays = 0;
for (int c = 0; c < riseOrDescend.size(); c++) {
if (riseOrDescend.get(c) == 1) {
riseIndex = c;
maxRiseDays = maxRiseDays + 1;
} else {
if (maxRiseDays > currentMaxRiseDays) {
currentRiseIndex = riseIndex;
currentMaxRiseDays = maxRiseDays;
}
riseIndex = -1;
maxRiseDays = 0;
}
if (riseOrDescend.get(c) == -1) {
descendIndex = c;
maxDescendDays = maxDescendDays + 1;
} else {
if (maxDescendDays > currentMaxDescendDays) {
currentDescendIndex = descendIndex;
currentMaxDescendDays = maxDescendDays;
}
descendIndex = -1;
maxDescendDays = 0;
}
}
g.setColor(Color.BLACK);
// 画Y轴
g.drawLine(40, 0, 40, 690);
for (int i = 0; i <= 53; i++) {
// if (i < 53) {
g.drawLine(30, i * 50 / 4 + 15, 40, i * 50 / 4 + 15);
g.drawString(" ".repeat(4 - String.valueOf(2650 - i * 50).length()) + String.valueOf(2650 - i * 50), 3,
i * 50 / 4 + 20);
g.setColor(Color.GRAY);
if (i % 2 == 1) {
g.setColor(Color.GRAY);
g.drawLine(40, i * 50 / 4 + 15, 5000, i * 50 / 4 + 15);
}
g.setColor(Color.BLACK);
}
// 画X轴
g.drawLine(30, 677, 5000, 677);
for (int j = 0; j < listOfDate.size(); j++) {
if (j % 100 == 0) {
g.drawLine(40 + j, 677, 40 + j, 687);
g.drawString(listOfDate.get(j), 10 + j, 697);
}
}
// 画股价曲线图
for (int k = 0; k < listOfPrice.size(); k++) {
int price = (int) (listOfPrice.get(k) / 4);
int xPoint = 678 - price;
if (k == 0) {
g.setColor(Color.BLACK);
g.fillOval(40 + k - 2, xPoint - 2, 4, 4);
} else {
int privousPrice = (int) (listOfPrice.get(k - 1) / 4);
int prevousXPoint = 678 - privousPrice;
if (listOfPrice.get(k) > listOfPrice.get(k - 1)) {
g.setColor(Color.RED);
}
if (listOfPrice.get(k) == listOfPrice.get(k - 1)) {
g.setColor(Color.GRAY);
}
if (listOfPrice.get(k) < listOfPrice.get(k - 1)) {
g.setColor(Color.BLUE);
}
if (k < listOfPrice.size() - 1) {
//Graphics2D g2d = (Graphics2D) g;
//g2d.setStroke(new BasicStroke(2.0f));
//g2d.drawLine(40 + k - 1, prevousXPoint, 40 + k, xPoint);
g.drawLine(40 + k - 1, prevousXPoint, 40 + k, xPoint);
} else if (k == listOfPrice.size() - 1) {
// 结束点化成实心圆
g.setColor(Color.BLACK);
g.fillOval(40 + k - 2, xPoint - 2, 4, 4);
}
}
}
g.setColor(Color.darkGray);
g.setFont(new Font("宋体", Font.BOLD, 15));
g.drawString("最高收盘价格" + highestPrice + "元出现在" + dateOfHighestPrice + ",最底收盘价格" + lowestPrice + "元出现在"
+ dateOfLowestPrice, 50, 120);
g.drawString("有效交易日" + listOfDate.size() + "天,上涨" + numOfRiseOrDescend.get(1) + "天,持平"
+ numOfRiseOrDescend.get(0) + "天,下跌" + numOfRiseOrDescend.get(-1) + "天。", 50, 144);
g.drawString(
"连续上涨最长天数为" + currentMaxRiseDays + "天,从交易日"
+ listOfDate.get(currentRiseIndex - currentMaxRiseDays + 2) + "至交易日"
+ listOfDate.get(currentRiseIndex + 1) + ",从"
+ listOfDate.get(currentRiseIndex - currentMaxRiseDays + 1) + "的"
+ +listOfPrice.get(currentRiseIndex - currentMaxRiseDays + 1) + "元连续上涨至"
+ listOfDate.get(currentRiseIndex + 1) + "的" + listOfPrice.get(currentRiseIndex + 1) + "元",
50, 170);
g.drawString("连续下跌最长天数为" + currentMaxDescendDays + "天,从交易日"
+ listOfDate.get(currentDescendIndex - currentMaxDescendDays + 2) + "至交易日"
+ listOfDate.get(currentDescendIndex + 1) + ",从"
+ listOfDate.get(currentDescendIndex - currentMaxDescendDays + 1) + "的"
+ +listOfPrice.get(currentDescendIndex - currentMaxDescendDays + 1) + "元连续下跌至"
+ listOfDate.get(currentDescendIndex + 1) + "的" + listOfPrice.get(currentDescendIndex + 1) + "元",
50, 194);
} catch (IOException e) {
e.printStackTrace();
}
g.setFont(new Font("宋体", Font.BOLD, 30));
g.setColor(Color.BLACK);
g.drawString("贵州茅台自2001年8月27日至2022年4月7日股票收盘价趋势图", 50, 30);
g.setFont(new Font("宋体", Font.BOLD, 20));
g.setColor(Color.RED);
g.drawString("红色曲线代表股价上涨", 80, 55);
g.setColor(Color.BLUE);
g.drawString("蓝色曲线代表股价下跌", 80, 80);
}
}
当显示设置的缩放选择为200%、175%..等小于225%的值时就会出现重影;如果缩放选择的时225%、250%、300%则不会出现重影
无法查明原因,请站友不吝赐教!谢谢!