swing 画平滑曲线
【摘要】 package zhipu.trial.graph;
import zhipu.main.Entrance;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.GeneralPath;
import java.util.ArrayList;
import java.util.List;
publ...
package zhipu.trial.graph; import zhipu.main.Entrance; import javax.swing.*; import java.awt.*; import java.awt.geom.GeneralPath; import java.util.ArrayList; import java.util.List; public class DrawChartDemo extends JPanel { private Float[] values = new Float[]{0.00476401629000442f , 0.0063234711479510904f, 0.021695992320743099f, 0.0087923714104826394f, 0.016055573626994599f, 0.013452284007208701f, 0.0055611602146519801f, 0.0069146150381687596f, 0.0036230541986219198f, 0.0061674043426179301f, 0.00434440023492552f, 0.035138205238166999f, 0.0143897055961933f, 0.016811934089353098f, 0.0178861993959042f, 0.0087641654315109407f, 0.0102057457561573f, 0.0073624585142180602f, 0.272971583614874f, 0.0548574538534378f, 0.016955049412583498f, 0.0051184965677307202f, 0.0033135295393096199f, 0.0059344633867462002f, 0.0039347395484018001f, 0.00348367923743807f, 0.026377286849230099f, 0.036832070301518f, 0.0056683752431168398f, 0.0047084728862943504f, 0.0050807108798568996f, 0.0105448869266738f, 0.0056977138353234702f, 0.085602792538286607f, 0.0043054151148011997f, 0.0061197850778468496f, 0.0144032602561065f, 0.0098497881084309603f, 0.0065389022201078504f, 0.0079793910312075996f, 0.15023645197019f, 0.0068558337838472202f, 0.00388789967772707f, 0.0019588905560046599f, 0.00813157867268848f, 0.0181800097326743f, 0.0179981531868081f, 0.120687865774082f, 0.031901241417862901f, 0.0059096372433688803f, 0.039816888009691f, 0.0022180602796087298f, 0.0020421521163729701f, 0.0029398192573330999f, 0.0090383331308239393f, 0.069211309213069394f, 0.111332699808443f, 0.044655161421859497f, 0.0057869308391771297f, 0.0041646558052810603f, 0.0019861723474547599f, 0.0038429991850212901f, 0.0064167275578309001f, 0.0583319986899676f, 0.029744027162729499f, 0.0120586137993033f, 0.13488486713846501f, 0.046035790422276901f, 0.015785314062179999f, 0.0039932783490598603f, 0.0081173362329025698f, 0.101646122607811f, 0.27943278350252698f, 0.0150728510109874f, 0.013116721411450901f, 0.0092261367320127692f, 0.026218708859473899f, 0.00282953555265384f, 0.0044117373389137102f, 0.00260404985014934f, 0.0019293731880499201f, 0.00190464973226343f, 0.0038443276152136602f, 0.034056811633866202f, 1f, 0.094481973471835803f, 0.056033417554237797f, 0.018825637176542499f, 0.012730403947311f, 0.0065964488004084604f, 0.0126633133100138f, 0.0080601022249829398f, 0.0089431644768543598f, 0.025988444047573799f, 0.025109531649443f, 0.113067555237683f, 0.026435961558734799f, 0.070132172326724307f, 0.0044758225616537703f, 0.0059336269122534204f, 0.0036456993373522898f, 0.0118958563883495f, 0.0575133588137191f, 0.015350604576100499f, 0.0040318661392891499f, 0.0032193573469880901f, 0.0088850481743988092f, 0.023877330732745498f, 0.011155327017979f, 0.0083402368409935708f, 0.0573569045895874f, 0.69517440147232901f, 0.094371184910862896f, 0.071074104420728201f, 0.036956650045186501f, 0.0173215382293214f, 0.0082548025110114592f, 0.00575279184699701f, 0.0212456696437636f, 0.0090010269608339806f, 0.016338080136423599f, 0.102902209055831f, 0.0078924297946662402f, 0.0074607529655075604f, 0.0091213387724318404f, 0.0021516241375805099f, 0.043235898066084501f, 0.0025994439364933199f, 0.020705025675201501f, 0.0032286088255712199f, 0.0050739272892518896f, 0.0077606155122827997f, 0.24031502163576099f, 0.0361153446322387f, 0.010647914836661301f, 0.0034358292189724002f, 0.0034736207897978801f, 0.00342349287658111f, 0.0027815949551543298f, 0.21694338263132201f, 0.026700254130092999f, 0.0247184156591739f, 0.019277156255502199f, 0.061541706635920297f, 0.014204979551463199f, 0.098758600131271204f, 0.0109810452672438f, 0.011093174362075801f, 0.0035773478807027402f, 0.024341837124427598f, 0.095577421992731795f, 0.068092147724430493f, 0.025021579201069202f, 0.126686929940758f, 0.0274313458782336f, 0.0021874701261044402f, 0.0018053147965141801f, 0.0047045163060877001f, 0.060906978228009202f, 0.013506145600343401f, 0.057883280380810802f, 0.0135282913118019f, 0.133110570121451f, 0.0196209073352617f, 0.0081105463525148799f, 0.0166649644250235f, 0.119949170146833f, 0.35597758971746102f, 0.086355513768921199f, 0.020482876765933199f, 0.014151643764207401f, 0.0080725779012815208f, 0.0038027676021578499f, 0.015995267888023499f, 0.0024399349293019101f, 0.0065235374981799004f, 0.0051175119611385602f, 0.051700471945563202f, 0.0116097754442507f, 0.024850592780722301f, 0.0073334465479829398f, 0.0282795436228921f, 0.0071780000763853003f, 0.0047777663463222002f, 0.0073803493784771702f, 0.0043306186181200097f, 0.0064184788227479601f}; List<GeneralPath> paths =new ArrayList<GeneralPath>(); int maxindex=0; float maxvalue=-1; int heith=100; public DrawChartDemo() { int startY=0; int endY=0; for (int j = 0; j <5; j++) { Float[] points=values.clone(); startY=j*heith; endY=(j+1)*heith; GeneralPath path = new GeneralPath(); // path.lineTo(0,100); // path.lineTo(0, heith-points[0]); int index = 0; int indexnext = 0; for (int i = 0; i < points.length - 1; ++i) { points[i] *= heith; if (points[i] > maxvalue) { maxvalue = points[i]; maxindex = i; } } path.moveTo(0, endY - points[0]); for (int i = 0; i < points.length - 1; ++i) { index = i * 5; indexnext = (i + 1) * 5; float sp = points[i]; float ep = points[i + 1]; // Point c1 = new Point((i + i+1)/2, sp); // Point c2 = new Point((i + i+1)/2, ep); path.curveTo((index + indexnext) / 2, endY - sp, (index + indexnext) / 2, endY - ep, (index + 5), endY - ep); if (maxindex == i) { // path.lineTo((index+indexnext)/2,100); // path.lineTo((index+indexnext)/2, heith-ep); } } paths.add(path); } } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setStroke(new BasicStroke(1)); g2d.translate(40, 40); int gsize = paths.size(); for (int j = 0; j < gsize; j++) { g2d.setColor(Color.blue); g2d.draw(paths.get(j)); g2d.setColor(Color.red); g2d.drawLine(maxindex*5,heith,maxindex*5,0); } // for (int i = 0; i < points.length; ++i) { // g2d.setColor(Color.GRAY); // g2d.fillOval(points[i].x-4, points[i].y-4, 8, 8); // g2d.setColor(Color.BLACK); // g2d.drawOval(points[i].x-4, points[i].y-4, 8, 8); // } } private static void createAndShowGui() { JFrame frame = new JFrame("Smooth Curve"); frame.setContentPane(new DrawChartDemo()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(820, 280); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { Entrance.startTime=System.currentTimeMillis(); //获取开始时间 createAndShowGui(); Entrance.endTime=System.currentTimeMillis(); //获取开始时间 System.out.println("程序启动时间: " + (Entrance.endTime - Entrance.startTime) + "ms"); } }); } }
文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/75212815
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)