如何编辑两个不同类的面板以使用图形?

时间:2018-04-08 16:06:08

标签: java graphics jpanel frame netbeans-8

实际上我正在通过第一个,最差的,最合适的方式创建内存管理。我创建了框架。一切都在编译时没有任何错误但在其他类中没有输出

    import java.util.stream.*;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;

    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;

       public class bar extends javax.swing.JFrame {

        public int[]  blocksize=new int[100];
        public int i=0,l=0;
        public int[] prosize=new int[100];

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

        /*
         * regenerated by the Form Editor.
         */

        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {

            jMenu1 = new javax.swing.JMenu();
            jFrame1 = new javax.swing.JFrame();
            jFrame2 = new javax.swing.JFrame();
            jFrame3 = new javax.swing.JFrame();
            jLabel1 = new javax.swing.JLabel();
            jComboBox1 = new javax.swing.JComboBox<>();
            jButton1 = new javax.swing.JButton();
            jTextField1 = new javax.swing.JTextField();
            checkbox1 = new java.awt.Checkbox();
            checkbox2 = new java.awt.Checkbox();
            jPanel1 = new javax.swing.JPanel();
            jButton2 = new javax.swing.JButton();

            jMenu1.setText("jMenu1");

            javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
            jFrame1.getContentPane().setLayout(jFrame1Layout);
            jFrame1Layout.setHorizontalGroup(

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

        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
           jTextField1.setText(jComboBox1.getSelectedItem().toString()); // TODO add your handling code here:
        }                                        

        private void checkbox1MouseClicked(java.awt.event.MouseEvent evt) {                                       
            // TODO add your handling code here:

        }                                      

        private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {                                      
       JFrame f=new JFrame();
            if(checkbox1.getState())
      {  

          int m=IntStream.of(blocksize).sum();
          int j=Integer.parseInt(JOptionPane.showInputDialog(f,"Enter Block size"));  
          blocksize[i]=j;


          Graphics gfx=jPanel1.getGraphics();
          if(i==0)
          {gfx.drawRect(0,0,blocksize[i]/2,100);}
           else
           {
            gfx.drawRect(m/2,0,blocksize[i]/2,100);

           }
          i++;
      }       
      if(checkbox2.getState())
      {
         int k=Integer.parseInt(JOptionPane.showInputDialog(f,"Enter Process size"));  
          prosize[l]=k;
        l++;
      }
     // TOD{O add your handling code here:
        }                                     

        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
        }                                        

        private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      

            if(jComboBox1.getSelectedItem().toString()=="First Fit")
      {
       Firstfit obj=new Firstfit();
       obj.firstFit(blocksize,i,prosize,l);


      }
      else if(jComboBox1.getSelectedItem().toString()=="Best Fit")
      {
          Bestfit obj=new Bestfit();
       obj.bestFit(blocksize,i,prosize,l);

      }
      else{
          Worstfit obj=new Worstfit();
       obj.worstFit(blocksize,i,prosize,l);

      }              // TODO add your handling code here:
        }                                     

           /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new bar().setVisible(true);
                }
            });
        }

        // Variables declaration - do not modify                     
        private java.awt.Checkbox checkbox1;
        private java.awt.Checkbox checkbox2;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JComboBox<String> jComboBox1;
        private javax.swing.JFrame jFrame1;
        private javax.swing.JFrame jFrame2;
        private javax.swing.JFrame jFrame3;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JMenu jMenu1;
        public static javax.swing.JPanel jPanel1;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration                   


        private void jCheckbox1ActionPerformed(java.awt.event.ActionEvent evt) {    


    }
      }

我想在此代码中使用图形编辑面板。此代码用于实现首次匹配 我需要填充矩形,因为我想在块中插入内存

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import static mm.bar.jPanel1;

public class Firstfit extends javax.swing.JFrame
{
    bar b=new bar();

      // Method to allocate memory to blocks as per First fit
    // algorithm
    static void firstFit(int blockSize[], int m, int processSize[], 
                                                     int n)
    { 

        // Stores block id of the block allocated to a
        // process
        int allocation[] = new int[n];

        // Initially no block is assigned to any process
        for (int i = 0; i < allocation.length; i++)
            allocation[i] = -1;

        // pick each process and find suitable blocks
        // according to its size ad assign to it
        for (int i=0; i<n; i++)
        {
            for (int j=0; j<m; j++)
            {
                if (blockSize[j] >= processSize[i])
                {
                    // allocate block j to p[i] process
                    allocation[i] = j;
                   Graphics gfx=jPanel1.getGraphics();

                    gfx.drawRect(0,0,200,200);
                    gfx.setColor(Color.RED);
                    gfx.fillRect(0,0,200,200);
                    System.out.println("Hi");

                    // Reduce available memory in this block.
                    blockSize[j] -= processSize[i];

                    break;
                }
            }
        }         
    }      
}

0 个答案:

没有答案