Menampilkan Undetermined Circle Progressbar di Java
Terkadang, dalam menjalankan perintah yang tidak dapat ditentukan nilai progress presentasenya, dibutuhkan untuk menampilkan progressbar yang berupa circle yang berputar secara terus menerus.
Hal ini seperti memanggil sebuah halaman website, dimana website tersebut tidak dapat dihitung berapa ukuran sebenarnya.
Sehingga tidak dapat membandingkan atau menghitung seberapa persen halaman yang sudah terdownload, dibanding yang belum.
Kode berikut adalah kode class ProgressCircleUI yang telah penulis modifikasi, untuk menampilkan progressbar circle yang berputar secara terus-menerus.
package circleloopprogressbar; import java.awt.BasicStroke; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Insets; import java.awt.RenderingHints; import javax.swing.JComponent; import javax.swing.plaf.basic.BasicProgressBarUI; /** * * @author Edugameapp */ public class ProgressCircleUI extends BasicProgressBarUI { private int startAngle=0; private int reverse=-1; private int lineWidth=2; public Dimension getPreferredSize(JComponent c) { Dimension d = super.getPreferredSize(c); int v = Math.max(d.width, d.height); d.setSize(v, v); return d; } public void paint(Graphics g, JComponent c) { Insets b = progressBar.getInsets(); // area for border int barRectWidth = progressBar.getWidth() - b.right - b.left; int barRectHeight = progressBar.getHeight() - b.top - b.bottom; if (barRectWidth <= 0 || barRectHeight <= 0) { return; } // draw the cells Graphics2D g2 = (Graphics2D) g.create(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(progressBar.getForeground()); double degree = 360 * progressBar.getPercentComplete(); double sz = Math.min(barRectWidth, barRectHeight); g2.setStroke(new BasicStroke(2*lineWidth)); g2.drawArc(lineWidth,lineWidth,(int)(sz-2*lineWidth),(int)(sz-2*lineWidth),startAngle,(int)(reverse*degree)); g2.dispose(); // Deal with possible text painting if (progressBar.isStringPainted()) { paintString(g, b.left, b.top, barRectWidth, barRectHeight, 0, b); } } public int getStartAngle() { return startAngle; } public void setStartAngle(int startAngle) { this.startAngle = startAngle; } public int getReverse() { return reverse; } public void setReverse(int reverse) { this.reverse = reverse; } public void setLineWidth(int lineWidth) { this.lineWidth = lineWidth; } }
Sedangkan kode di bawah ini adalah contoh kode main Java, untuk menggunakan class ProgressCircleUI di atas.
package circleloopprogressbar; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JProgressBar; import javax.swing.Timer; import javax.swing.WindowConstants; /** * * @author Edugameapp */ public class CircleLoopProgressbar extends JFrame { private JProgressBar progressbar = new JProgressBar(); private Timer timer; private int modeProgressbar=1; public void setup() { setTitle("Circle Loop Progressbar"); setLayout(new GridLayout()); timer = new Timer(10, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int iv = progressbar.getValue(); if(modeProgressbar>0){ iv++; }else{ iv--; } iv = Math.max(0,Math.min(100,iv)); progressbar.setValue(iv); if (progressbar.getValue() == 100) { modeProgressbar*=-1; ((ProgressCircleUI)progressbar.getUI()).setReverse(-modeProgressbar); }else if (progressbar.getValue() == 0) { modeProgressbar*=-1; ((ProgressCircleUI)progressbar.getUI()).setReverse(-modeProgressbar); } } }); progressbar = new JProgressBar(); progressbar.setUI(new ProgressCircleUI()); ((ProgressCircleUI)progressbar.getUI()).setStartAngle(90); ((ProgressCircleUI)progressbar.getUI()).setLineWidth(20); progressbar.setBorder(BorderFactory.createEmptyBorder(0,0,0,0)); progressbar.setPreferredSize(new Dimension(250,250)); timer.start(); add(progressbar); pack(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setLocation((int)(0.5*(screenSize.getWidth()-getWidth())),(int)(0.5*(screenSize.getHeight()-getHeight()))); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); getContentPane().requestFocusInWindow(); setVisible(true); } public static void main(String[] args) { CircleLoopProgressbar main=new CircleLoopProgressbar(); main.setup(); } }
Timer digunakan untuk menjalankan animasi, yang dalam implementasi yang sesungguhnya akan diperlukan memanggil timer.stop() saat progress telah benar-benar selesai.
Referensi
- kacianoghelere. 2016. ProgressCircleUI.java. https://github.com/kacianoghelere/G-Tools/blob/master/G-Comps/src/main/java/br/com/gmp/comps/ui/ProgressCircleUI.java. Diakses tanggal 1 Januari 2020.
Komentar
Posting Komentar