• Terbaru

    Java Graphics , Program Nilai Mahasiswa

    Program Java Grafik yang sebenernya tugas kuliah waktu taun 2013, kodenya sedikit ngaco karena waktu yang di beri dosen singkat, jadi bikin programnya sejadi-jadinya.






    .
    nah itu, logo di gantinya dari JLabel, di modifikasi saja dari JLabel > properties dan ganti icon JLabel.,
    dan kode programnya :

    Library :
    import java.awt.*;
    import java.awt.Color;
    import static java.awt.Color.blue;
    import static java.awt.Color.green;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.font.TextAttribute;
    import java.awt.geom.GeneralPath;
    import java.text.AttributedString;
    .


    kode program :


       
        //membuat attribut untuk menampung variabel input text field
    private int hdir,tgs,uts,uas;
    double hasil;



        /**
         * Creates new form FORMINPUT
         */
        public FORMINPUT() {
            initComponents();
        }

        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                        
        private void initComponents() {

            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jLabel5 = new javax.swing.JLabel();
            jLabel6 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            fkehadiran = new javax.swing.JTextField();
            ftugas = new javax.swing.JTextField();
            fuas = new javax.swing.JTextField();
            futs = new javax.swing.JTextField();
            nakhir = new javax.swing.JLabel();
            hitungBtn = new javax.swing.JButton();
            form = new javax.swing.JLabel();

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

            jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
            jLabel1.setText("Data nilai mahasiswa");

            jLabel2.setText("Tugas");

            jLabel3.setText("UTS");

            jLabel5.setText("Kehadiran");

            jLabel6.setText("UAS");

            jLabel4.setText("Nilai akhir");

            hitungBtn.setText("Hitung dan Tampilkan");
            hitungBtn.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    hitungBtnActionPerformed(evt);
                }
            });

            form.setIcon(new javax.swing.ImageIcon(getClass().getResource("/logounpam.png"))); // NOI18N

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(22, 22, 22)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(form, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(hitungBtn)
                        .addComponent(jLabel1)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel5)
                                .addComponent(jLabel2)
                                .addComponent(jLabel3)
                                .addComponent(jLabel6)
                                .addComponent(jLabel4))
                            .addGap(29, 29, 29)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(nakhir)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(fkehadiran, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                                    .addComponent(ftugas)
                                    .addComponent(futs)
                                    .addComponent(fuas)))))
                    .addContainerGap(503, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(28, 28, 28)
                    .addComponent(form)
                    .addGap(34, 34, 34)
                    .addComponent(jLabel1)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(fkehadiran, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(ftugas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(futs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(fuas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(28, 28, 28)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(nakhir))
                    .addGap(31, 31, 31)
                    .addComponent(hitungBtn)
                    .addContainerGap(81, Short.MAX_VALUE))
            );

            pack();
        }// </editor-fold>                      

        private void hitungBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        
            // TODO add your handling code here:
            //kode ketika tombol hitung di klik
            //menyimpan variabel input dan menyimpan sekaligus menampilkan grafik
            //dari fungsi paintcomponents
          
            hdir = Integer.parseInt(fkehadiran.getText());
            tgs = Integer.parseInt(ftugas.getText());
            uts = Integer.parseInt(futs.getText());
            uas = Integer.parseInt(fuas.getText());
          
            hasil = (hdir*0.1)+(tgs*0.2)+(uts*0.3)+(uas*0.4); //perhitungan nilai
            nakhir.setText(Double.toString(hasil));
          
            paintComponents(getGraphics());
        }                                       

        public void paint(Graphics g){
            super.paintComponents(g); //ini yang bikin tampil bareng sama jtextfield
        Graphics2D g2d = (Graphics2D)g;
        g2d.setColor(Color.red);
        GeneralPath gp = new GeneralPath(GeneralPath.WIND_NON_ZERO);
      
        //garis horizontal 10 kali dengan looping
                for(int i=0;i<200;i=i+20){
                    int no = i;
                    g.drawLine(250, 350+i, 250, 380+i);
                    gp.moveTo(250, 350+i);
                    gp.lineTo(600, 350+i);
                    g2d.draw(gp);
                }
              
                //judul
                g.drawString("Mahasiswa", 360, 300);
                g2d.fillRect(340, 300, 10, 10);
              
              
                g.drawLine(250, 550, 600, 550);  //garis
                g.drawString("0", 230, 560);
                g.drawString("10", 230, 540);
                g.drawString("20", 230, 520);
                g.drawString("30", 230, 500);
                g.drawString("40", 230, 480);
                g.drawString("50", 230, 460);
                g.drawString("60", 230, 440);
                g.drawString("70", 230, 420);
                g.drawString("80", 230, 400);
                g.drawString("90", 230, 380);
                g.drawString("100", 230, 360);
              
                g.drawString("Kehadiran", 265, 565);
                g.drawString("Tugas", 335, 565);
                g.drawString("UTS",  385, 565);
                g.drawString("UAS", 420, 565);
              
                //tulisan judul
                String Words = "UNIVERSITAS PAMULANG";
             Font font = new Font("Arial",Font.PLAIN, 30);
             AttributedString as1 = new AttributedString(Words);
             as1.addAttribute(TextAttribute.FONT, font);
            // as1.addAttribute(TextAttribute.FOREGROUND, Color.blue, 0, 6);
           
             //gradient
             GradientPaint gdp;
        gdp = new GradientPaint(110,10,Color.blue,10,90,Color.red,true);
        g2d.setPaint(gdp);
        g2d.drawString(as1.getIterator(),300,250);
      
        String nama = "Muhammad Iskandar Zulkarnain";
        Font hurufnama = new Font("Arial",Font.PLAIN,20);
        AttributedString nama1 = new AttributedString(nama);
        nama1.addAttribute(TextAttribute.FONT, hurufnama);
      
        g2d.drawString(nama1.getIterator(),300,620);
      
        }
      
        /* karena jika di klik maka attribut paint sebelumnya akan di timpa dengan
        paint yang sesudh di klik, maka di tuliskan lagi semua komponen paint sebelumnya di
        fungsi paint setelah di klik
         *
      
        */
        public void paintComponents(Graphics b){
            super.paintComponents(b);
            Graphics2D g3d = (Graphics2D)b;
            g3d.setColor(Color.red);
            //button
            hdir = Integer.parseInt(fkehadiran.getText());
            tgs = Integer.parseInt(ftugas.getText());
            uts = Integer.parseInt(futs.getText());
            uas = Integer.parseInt(fuas.getText());
          
            GeneralPath gp = new GeneralPath();
            b.drawString("Mahasiswa", 360, 300);
                g3d.fillRect(340, 300, 10, 10);
          
          
          
            //hadir
            gp.moveTo(270, 550);
            gp.lineTo(270, 550-(hdir*2));
            gp.lineTo(300, 550-(hdir*2));
            gp.lineTo(300, 550);
            b.drawString("Kehadiran", 260, 565);
            //hadir
          
            //tugas
            gp.moveTo(330, 550);
            gp.lineTo(330, 550-(tgs*2));
            gp.lineTo(360, 550-(tgs*2));
            gp.lineTo(360, 550);
            b.drawString("Tugas",  330, 565);
            //tugas
          
            //uts
            gp.moveTo(390, 550);
            gp.lineTo(390, 550-(uts*2));
            gp.lineTo(420, 550-(uts*2));
            gp.lineTo(420, 550);
            b.drawString("UTS", 390, 565);
            //uts
          
            //uas
            gp.moveTo(450, 550);
            gp.lineTo(450, 550-(uas*2));
            gp.lineTo(480, 550-(uas*2));
            gp.lineTo(480, 550);
            b.drawString("UAS", 450, 565);
            //uas
          
            g3d.draw(gp);
            g3d.fill(gp);
          
            //gp.lineTo(hasil, hasil);
          
          
            for(int i=0;i<200;i=i+20){
                    int no = i;
                    b.drawLine(250, 350+i, 250, 380+i);
                    gp.moveTo(250, 350+i);
                    gp.lineTo(600, 350+i);
                    g3d.draw(gp);
                }
            b.drawLine(250, 550, 600, 550);  //garis
                b.drawString("0", 230, 560);
                b.drawString("10", 230, 540);
                b.drawString("20", 230, 520);
                b.drawString("30", 230, 500);
                b.drawString("40", 230, 480);
                b.drawString("50", 230, 460);
                b.drawString("60", 230, 440);
                b.drawString("70", 230, 420);
                b.drawString("80", 230, 400);
                b.drawString("90", 230, 380);
                b.drawString("100", 230, 360);
              
              
                String Words = "UNIVERSITAS PAMULANG";
             Font font = new Font("Arial",Font.PLAIN, 30);
             AttributedString as1 = new AttributedString(Words);
             as1.addAttribute(TextAttribute.FONT, font);
            // as1.addAttribute(TextAttribute.FOREGROUND, Color.blue, 0, 6);
           
             GradientPaint gdp;
        gdp = new GradientPaint(110,10,Color.blue,10,90,Color.red,true);
        g3d.setPaint(gdp);
        g3d.drawString(as1.getIterator(),300,250);
      
        String nama = "Muhammad Iskandar Zulkarnain";
        Font hurufnama = new Font("Arial",Font.PLAIN,20);
        AttributedString nama1 = new AttributedString(nama);
        nama1.addAttribute(TextAttribute.FONT, hurufnama);
      
        g3d.drawString(nama1.getIterator(),300,620);
          
          
        }
    .

    Sekian penjelasan yang sedikit tapi panjang.

    No comments

    Saya sangat berterimakasih apabila sahabat sekalian bersedia untuk tidak meninggalkan spam dan meninggalkan komentar yang berhubungan dengan artikel di atas.