Loading ...
Sorry, an error occurred while loading the content.
 

Java COde.....

Expand Messages
  • didit4botenk
    aloow smua.... mohon bantuan doonk.... saya dapat tugas untuk membuat bola setiap button di click. tapi yg terjadi setelah bola2 tersebut ber hasil di
    Message 1 of 5 , Oct 31, 2005
      aloow smua....
      mohon bantuan doonk....
      saya dapat tugas untuk membuat bola setiap button di click.
      tapi yg terjadi setelah bola2 tersebut ber hasil di tampilkan di
      window, bola2 tersebut akan menhilang apabila kita menggeser window
      outside the screen!!.

      begini qra2 code yg telah saya buat.

      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      class RandomBallsEx2
      {
      RandomBallsEx2 randomballsEx2;
      Listener listen = new Listener();
      JFrame frame = new JFrame(" RandomBallsEx2");
      JPanel panel = new JPanel();
      JButton button = new JButton("new ball");
      JPanel panel2 = new JPanel();


      public static void main(String[] args)
      {
      new RandomBallsEx2();
      }

      RandomBallsEx2()
      {

      frame.setVisible(true);


      panel.setPreferredSize(new Dimension(640, 440));
      frame.add(panel);


      panel2.add(button);
      frame.add(panel2, BorderLayout.SOUTH);

      frame.pack();

      frame.addWindowListener(new WindowAdapter()
      {
      public void windowClosing(WindowEvent e)
      {
      System.exit(0);
      }
      });

      button.addActionListener(listen);
      }


      class Listener implements ActionListener
      {
      public void actionPerformed(ActionEvent e)
      {

      Graphics g = panel.getGraphics();
      int rd;
      int gr;
      int bl;
      while(true)
      {
      double d4 = Math.random()*1000;

      if (d4<256)
      {
      rd = (int)d4;
      break;
      }
      }

      while(true)
      {
      double d5 = Math.random()*1000;
      if(d5<256)
      {
      gr = (int)d5;
      break;
      }

      }

      while(true)
      {
      double d6 = Math.random()*1000;
      if (d6<256)
      {
      bl = (int)d6;
      break;
      }
      }

      g.setColor(new Color(rd,gr,bl));

      double d1 = Math.random()*500;
      int a = (int)d1;

      double d2 = Math.random()*500;
      int b = (int)d2;

      double d3 = Math.random()*200;
      int c = (int)d3;
      g.fillOval(a, b, c, c);


      }
      }

      }

      mohon bantuan..........
    • cherry pratama
      saya punya usul sbb, tapi sebelumnya mohon maaf kalo ada error karena nggak ada compiler /* begin file : DrawBalls.java */ import java.util.*; import
      Message 2 of 5 , Nov 7, 2005
        saya punya usul sbb, tapi sebelumnya mohon
        maaf kalo ada error karena nggak ada
        compiler

        /* begin file : DrawBalls.java */
        import java.util.*;
        import java.awt.*;
        import java.awt.event.*;
        import javax.swing.*;

        class PicBox extends JLabel {
        private int numBalls;
        private int [] xPos;
        private int [] yPos;

        public PicBox () {
        numBalls = 0;
        // max 100 bola
        xPos = new int[100];
        yPos = new int[100];
        setPreferredSize(new Dimension(800, 600));
        }

        public void paintComponent (Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.red);
        for (int i = 0; i < numBalls; ++i) {
        g.fillOval(x[i], y[i], 15, 15);
        }
        }

        public void addBall (int x, int y) {
        if (numBalls < xPos.length) {
        xPos[numBalls] = x;
        yPos[numBalls] = y;
        ++numBalls;
        }
        repaint();
        }

        public void clearBalls () {
        numBalls = 0;
        repaint();
        }
        }

        public class DrawBalls extends JPanel {
        private Random rand;
        private PicBox drawArea;
        private JButton drawBtn, clrBtn;
        private GridBagConstraints
        gbc = new GridBagConstraints();

        private void addGBLCom (Component c, int x,
        int y, int w) {
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = w;
        this.add(c, gbc);
        }

        public DrawBalls () {
        super(new GridBagLayout());

        rand = new Random(1L << 40);
        drawArea = new PicBox();
        drawBtn = new JButton("New Ball");
        clrBtn = new JButton("Clear Screen");

        addGBLCom(drawArea, 0, 0, 2);
        addGBLCom(drawBtn, 0, 1, 1);
        addGBLCom(clrBtn, 1, 1, 1);

        drawBtn.addActionListener(new ActionListener() {
        public void actionPerformed (ActionEvent ae) {
        int x = rand.nextInt(700);
        int y = rand.nextInt(500);
        drawArea.addBall(x, y);
        }
        });
        clrBtn.addActionListener(new ActionListener() {
        public void actionPerformed (ActionEvent ae) {
        drawArea.clearBalls();
        }
        });
        }

        public static void showGUI () {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame win = new JFrame("Draw Balls");
        win.getContentPane().add(new DrawBalls(),
        BorderLayout.CENTER);
        win.setDefaultCloseOperation(win.EXIT_ON_CLOSE);
        win.pack();
        win.setResizable(false);
        win.setVisible(true);
        }

        public static void main (String [] args) {
        SwingUtilities.invokeLater(new Runnable() {
        public void run () {
        showGUI();
        }
        });
        }
        }

        /* end */
        demikian usulan saya semoga bisa membantu

        --- didit4botenk <diditbotenk@...> wrote:

        > aloow smua....
        > mohon bantuan doonk....
        > saya dapat tugas untuk membuat bola setiap button di
        > click.
        > tapi yg terjadi setelah bola2 tersebut ber hasil di
        > tampilkan di
        > window, bola2 tersebut akan menhilang apabila kita
        > menggeser window
        > outside the screen!!.
        >
        > begini qra2 code yg telah saya buat.
        >
        > import javax.swing.*;
        > import java.awt.*;
        > import java.awt.event.*;
        > class RandomBallsEx2
        > {
        > RandomBallsEx2 randomballsEx2;
        > Listener listen = new Listener();
        > JFrame frame = new JFrame(" RandomBallsEx2");
        > JPanel panel = new JPanel();
        > JButton button = new JButton("new ball");
        > JPanel panel2 = new JPanel();
        >
        >
        > public static void main(String[] args)
        > {
        > new RandomBallsEx2();
        > }
        >
        > RandomBallsEx2()
        > {
        >
        > frame.setVisible(true);
        >
        >
        > panel.setPreferredSize(new Dimension(640, 440));
        > frame.add(panel);
        >
        >
        > panel2.add(button);
        > frame.add(panel2, BorderLayout.SOUTH);
        >
        > frame.pack();
        >
        > frame.addWindowListener(new WindowAdapter()
        > {
        > public void windowClosing(WindowEvent e)
        > {
        > System.exit(0);
        > }
        > });
        >
        > button.addActionListener(listen);
        > }
        >
        >
        > class Listener implements ActionListener
        > {
        > public void actionPerformed(ActionEvent e)
        > {
        >
        > Graphics g = panel.getGraphics();
        > int rd;
        > int gr;
        > int bl;
        > while(true)
        > {
        > double d4 = Math.random()*1000;
        >
        > if (d4<256)
        > {
        > rd = (int)d4;
        > break;
        > }
        > }
        >
        > while(true)
        > {
        > double d5 = Math.random()*1000;
        > if(d5<256)
        > {
        > gr = (int)d5;
        > break;
        > }
        >
        > }
        >
        > while(true)
        > {
        > double d6 = Math.random()*1000;
        > if (d6<256)
        > {
        > bl = (int)d6;
        > break;
        > }
        > }
        >
        > g.setColor(new Color(rd,gr,bl));
        >
        > double d1 = Math.random()*500;
        > int a = (int)d1;
        >
        > double d2 = Math.random()*500;
        > int b = (int)d2;
        >
        > double d3 = Math.random()*200;
        > int c = (int)d3;
        > g.fillOval(a, b, c, c);
        >
        >
        > }
        > }
        >
        > }
        >
        > mohon bantuan..........
        >
        >
        >
        >
        >
        >




        __________________________________
        Start your day with Yahoo! - Make it your home page!
        http://www.yahoo.com/r/hs
      • cherry pratama
        ehem, ada yg salah (tepatnya pada class PicBox), ini koreksinya: class PicBox extends JLabel { private int numBalls; private int [] xPos; private int [] yPos;
        Message 3 of 5 , Nov 9, 2005
          ehem, ada yg salah (tepatnya pada class PicBox),
          ini koreksinya:

          class PicBox extends JLabel {
          private int numBalls;
          private int [] xPos;
          private int [] yPos;

          public PicBox () {
          numBalls = 0;
          // max 100 bola
          xPos = new int[100];
          yPos = new int[100];
          setPreferredSize(new Dimension(800, 600));
          }

          public void paintComponent (Graphics g) {
          super.paintComponent(g);
          g.setColor(Color.red);
          for (int i = 0; i < numBalls; ++i) {
          g.fillOval(xPos[i], yPos[i], 15, 15);
          }
          }

          public void addBall (int x, int y) {
          if (numBalls < xPos.length) {
          xPos[numBalls] = x;
          yPos[numBalls] = y;
          ++numBalls;
          }
          repaint();
          }

          public void clearBalls () {
          numBalls = 0;
          repaint();
          }
          }

          demikian ralat dari saya terima kasih...





          __________________________________
          Yahoo! Mail - PC Magazine Editors' Choice 2005
          http://mail.yahoo.com
        • didit4botenk
          Wah...wahh... sebelumnya makasih bnyk atas sarannya... sangat bermanfaat sekali... tapi dari yg mas saranin ini, maksimum bola yg bisa di tampilkan hanya 100.
          Message 4 of 5 , Nov 10, 2005
            Wah...wahh... sebelumnya makasih bnyk atas sarannya... sangat
            bermanfaat sekali...
            tapi dari yg mas saranin ini, maksimum bola yg bisa di tampilkan
            hanya 100. dan dalam penggunaan array ini memory address yg dipakai
            berjumlah 100 dan tentu saja memakan bnyk memory.
            kalo ga keberatan ada ga cara lain untuk membuat jumlah bola2nya itu
            ga terbaras dan jumlah memory address yg akan terpakai sesuai dgn
            seberapa kali button di klik.
            sekali lagi maaf merepotkan...
            tapi saran penggunaan array yg telah diberikan sangat bermanfaat.
            terimakasih...

            --- In ilmukomputer-programming@yahoogroups.com, cherry pratama
            <cph_wpb@y...> wrote:
            >
            > ehem, ada yg salah (tepatnya pada class PicBox),
            > ini koreksinya:
            >
            > class PicBox extends JLabel {
            > private int numBalls;
            > private int [] xPos;
            > private int [] yPos;
            >
            > public PicBox () {
            > numBalls = 0;
            > // max 100 bola
            > xPos = new int[100];
            > yPos = new int[100];
            > setPreferredSize(new Dimension(800, 600));
            > }
            >
            > public void paintComponent (Graphics g) {
            > super.paintComponent(g);
            > g.setColor(Color.red);
            > for (int i = 0; i < numBalls; ++i) {
            > g.fillOval(xPos[i], yPos[i], 15, 15);
            > }
            > }
            >
            > public void addBall (int x, int y) {
            > if (numBalls < xPos.length) {
            > xPos[numBalls] = x;
            > yPos[numBalls] = y;
            > ++numBalls;
            > }
            > repaint();
            > }
            >
            > public void clearBalls () {
            > numBalls = 0;
            > repaint();
            > }
            > }
            >
            > demikian ralat dari saya terima kasih...
            >
            >
            >
            >
            >
            > __________________________________
            > Yahoo! Mail - PC Magazine Editors' Choice 2005
            > http://mail.yahoo.com
            >
          • Wiro Sableng
            mungkin saya bisa membantu dgn permasalahan memory, setelah melihat source code yg diusulkan sdr cherry, sebenarnya yg dialokasikan adalah array dgn tipe data
            Message 5 of 5 , Nov 15, 2005
              mungkin saya bisa membantu dgn permasalahan
              memory, setelah melihat source code yg diusulkan
              sdr cherry, sebenarnya yg dialokasikan adalah
              array dgn tipe data primitif yaitu integer
              dan kemungkinan utk kehabisan memory sangat kecil.
              lagipula yg disimpan hanyalah koordinat dari
              bola2x yg akan ditampilkan (bukan objek)
              jadi saya rasa hal tsb cukup aman
              (mungkin kira2x sampai 1 juta bola!).

              mungkin kalo anda ingin menentukan sendiri banyaknya
              bola yg ingin ditampilkan class PicBox mungkin bisa
              diganti seperti ini

              public class PicBox extends JLabel {
              private int maxBall;
              private int numBall;
              private int [] xPos;
              private int [] yPos;

              public PicBox (int max) {
              maxBall = max;
              numBall = 0;
              xPos = new int[max];
              yPos = new int[max];
              }

              public void paintComponent (Graphics g) {
              super.paintComponent(g);
              g.setColor(Color.red);
              for (int i = 0; i < numBall; ++i) {
              g.drawOval(xPos[i], yPos[i], 16, 16);
              }
              }

              public void addBall (int x, int y) {
              if (numBall < max) {
              xPos[numBall] = x;
              yPos[numBall] = y;
              ++numBall;
              }
              repaint();
              }

              public void clearBalls () {
              numBall = 0;
              repaint();
              }
              }

              demikian usulan dari saya

              --- didit4botenk <diditbotenk@...> wrote:

              > Wah...wahh... sebelumnya makasih bnyk atas
              > sarannya... sangat
              > bermanfaat sekali...
              > tapi dari yg mas saranin ini, maksimum bola yg bisa
              > di tampilkan
              > hanya 100. dan dalam penggunaan array ini memory
              > address yg dipakai
              > berjumlah 100 dan tentu saja memakan bnyk memory.
              > kalo ga keberatan ada ga cara lain untuk membuat
              > jumlah bola2nya itu
              > ga terbaras dan jumlah memory address yg akan
              > terpakai sesuai dgn
              > seberapa kali button di klik.
              > sekali lagi maaf merepotkan...
              > tapi saran penggunaan array yg telah diberikan
              > sangat bermanfaat.
              > terimakasih...
              >
              > --- In ilmukomputer-programming@yahoogroups.com,
              > cherry pratama
              > <cph_wpb@y...> wrote:
              > >
              > > ehem, ada yg salah (tepatnya pada class PicBox),
              > > ini koreksinya:
              > >
              > > class PicBox extends JLabel {
              > > private int numBalls;
              > > private int [] xPos;
              > > private int [] yPos;
              > >
              > > public PicBox () {
              > > numBalls = 0;
              > > // max 100 bola
              > > xPos = new int[100];
              > > yPos = new int[100];
              > > setPreferredSize(new Dimension(800, 600));
              > > }
              > >
              > > public void paintComponent (Graphics g) {
              > > super.paintComponent(g);
              > > g.setColor(Color.red);
              > > for (int i = 0; i < numBalls; ++i) {
              > > g.fillOval(xPos[i], yPos[i], 15, 15);
              > > }
              > > }
              > >
              > > public void addBall (int x, int y) {
              > > if (numBalls < xPos.length) {
              > > xPos[numBalls] = x;
              > > yPos[numBalls] = y;
              > > ++numBalls;
              > > }
              > > repaint();
              > > }
              > >
              > > public void clearBalls () {
              > > numBalls = 0;
              > > repaint();
              > > }
              > > }
              > >
              > > demikian ralat dari saya terima kasih...
              > >
              > >
              > >
              > >
              > >
              > > __________________________________
              > > Yahoo! Mail - PC Magazine Editors' Choice 2005
              > > http://mail.yahoo.com
              > >
              >
              >
              >
              >
              >
              >
              >


              __________________________________________________
              Do You Yahoo!?
              Tired of spam? Yahoo! Mail has the best spam protection around
              http://mail.yahoo.com
            Your message has been successfully submitted and would be delivered to recipients shortly.