📄 PastaGrafik.java .java dosyası
⬇️ İndir
import javax.swing.*;
import java.awt.*;
import java.text.DecimalFormat;
public class PastaGrafik {
private JFrame frame;
public PastaGrafik() {
frame = new JFrame("Pasta Grafiği Çizimi | Java Blackboard [156]");
frame.setLayout(null);
JPanel anaPanel = new JPanel();
anaPanel.setLayout(null);
anaPanel.setBackground(new Color(74, 103, 140));
anaPanel.setBounds(30, 30, 940, 540);
anaPanel.add(pastaGrafik(33, 33, 33)); // !!!
frame.add(anaPanel);
frame.setSize(1000, 622);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setVisible(true);
}
public JPanel pastaGrafik(double a, double b, double c) {
JPanel panelPasta = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
double aGecici = a;
double bGecici = b;
double cGecici = c;
double total = aGecici + bGecici + cGecici;
if (total != 360.0) {
// Dilim genişliklerini orantılı olarak ayarla
double scale = 360.0 / total;
aGecici *= scale;
bGecici *= scale;
cGecici *= scale;
}
int width = getWidth();
int height = getHeight();
int diameter = Math.min(width, height) - 10;
int x = (width - diameter) / 2;
int y = (height - diameter) / 2;
int startAngle = 0;
// Sektor A Kirmizi
int extent = (int) ((a / total) * 360);
g.setColor(new Color(204, 20, 62));
g.fillArc(x, y, diameter, diameter, startAngle, extent);
startAngle += extent;
// Sektor B Mavi
extent = (int) ((b / total) * 360);
g.setColor(new Color (67, 117, 252));
g.fillArc(x, y, diameter, diameter, startAngle, extent);
startAngle += extent;
// Sektor C Sari
extent = (int) ((c / total) * 360);
g.setColor(new Color(242, 226, 5));
g.fillArc(x, y, diameter, diameter, startAngle, extent);
}
};
panelPasta.setBackground(new Color(203, 231, 248));
panelPasta.setLayout(null);
panelPasta.setBounds(0, 0, 440, 440);
JPanel panelKareKirmizi = new JPanel();
panelKareKirmizi.setLayout(null);
panelKareKirmizi.setBackground(new Color(204, 20, 62));
panelKareKirmizi.setBounds(480, 100, 50, 50);
JPanel panelKareMavi = new JPanel();
panelKareMavi.setLayout(null);
panelKareMavi.setBackground(new Color (67, 117, 252));
panelKareMavi.setBounds(480, 180, 50, 50);
JPanel panelKareSari = new JPanel();
panelKareSari.setLayout(null);
panelKareSari.setBackground(new Color(242, 226, 5));
panelKareSari.setBounds(480, 260, 50, 50);
Font font = new Font("Ubuntu", Font.PLAIN, 30);
double toplam = a+b+c;
double oran = 0.0;
String oranMetni = "";
// Kesirli sayı formatını belirleyelim
DecimalFormat df = new DecimalFormat("#0.00");
oran = (100.0 * a) / toplam;
oranMetni = df.format(oran);
JLabel labelKirmizi = new JLabel();
labelKirmizi.setFont(font);
labelKirmizi.setText(a + " -> %" + oranMetni);
labelKirmizi.setBounds(540, 100, 300, 50);
oran = (100.0 * b) / toplam;
oranMetni = df.format(oran);
JLabel labelMavi = new JLabel();
labelMavi.setFont(font);
labelMavi.setText(b + " -> %" + oranMetni);
labelMavi.setBounds(540, 180, 300, 50);
oran = (100.0 * c) / toplam;
oranMetni = df.format(oran);
JLabel labelSari = new JLabel();
labelSari.setFont(font);
labelSari.setText(c + " -> %" + oranMetni);
labelSari.setBounds(540, 260, 300, 50);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBackground(new Color(203, 231, 248));
panel.add(panelPasta);
panel.add(panelKareKirmizi);
panel.add(panelKareMavi);
panel.add(panelKareSari);
panel.add(labelKirmizi);
panel.add(labelMavi);
panel.add(labelSari);
panel.setBounds(50, 50, 840, 440);
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new PastaGrafik();
});
}
}