Total members 11893 |It is currently Tue Nov 05, 2024 9:08 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Hello, I am working on a project comprised of 3 classes; (see below). Unfortunately I can't get them to work and I'm uncertain as to why. Can someone guide me?
cloud - contains an ArrayList of Points and methods: add(Point point) and draw(Graphics g).
/////////
Code:
package Lab10;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Random;



public class Cloud implements Serializable
{

   private static final long serialVersionUID = 1L;

   //ArrayList of Points
   static ArrayList<Point> cloud;
   Shape circle;
   static Random randomizer = new Random();


   /**
    * Default Constructor
    */
   public Cloud()
   {
      //initializes vars
      cloud = new ArrayList<Point>(100);
   }
   
   
   //adds the Point to the array list
   /**
    * @param point
    */
   public static void add(Point point)
   {
      cloud.add(point);
   }

   //fills a small circle on the graphics context at each point in the arraylist
   public static void draw(Graphics g)
   {
     
      Graphics2D ga = (Graphics2D)g;
      for (Point point: cloud)
      {
         CloudPanel cp = new CloudPanel();
         int height = cp.getHeight();
         int width = cp.getWidth();
         int x = randomizer.nextInt(width/2);
         int y = randomizer.nextInt(height/2);

         Shape circle = new Ellipse2D.Double(x, y, width, height);
         cp.paint((Graphics) circle);
         
         
      }

   }


   /**
    * @return the pointList
    */
   public static ArrayList<Point> getPointList()
   {
      return cloud;
   }

}
/////////////////
cloudApp - displays a Cloud containing 100 random points centered on the panel.
//////////////////
package Lab10;

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;

//import Lesson9.OtherApp.HousePanel;

public class CloudPanel extends JPanel

{
   private static final long serialVersionUID = 1;
   static Random randomizer = new Random();
 
   CloudPanel()
   {
      Cloud myCloud = new Cloud();
      repaint();
      //paintComponent();
   }
   
   public boolean saveCloud()
   {
      try {
             BufferedImage bi = ImageIO.read(new File("C:\\Libraries\\Documents\\lab10.bmp"));
             BufferedImage biFiltered;
             int w = bi.getWidth(null);
             int h = bi.getHeight(null);
             if (bi.getType() != BufferedImage.TYPE_INT_RGB)
             {
                 BufferedImage bi2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
                 Graphics big = bi2.getGraphics();
                 big.drawImage(bi, 0, 0, null);
                 biFiltered = bi = bi2;
             }
             return true;
         }
         catch (IOException e)
         {
             System.out.println("Image could not be read");
             System.exit(1);
             return false;
         }
   }
   
   public void loadCloud()
   {
      try
         {               
            JFileChooser chooser = new JFileChooser("C:\\Libraries\\Documents\\lab10.bmp");
            int returnVal= chooser.showOpenDialog(null);
            File inputfile = chooser.getSelectedFile();
            BufferedImage image = ImageIO.read(inputfile);   
            if (image != null)
               {
                  CloudPanel.setBackgroundImage(image);
               }
         }
         catch (IOException e1)
         {
            e1.printStackTrace();
         }
   }
   
   public void newCloud()
   {
      int h = this.getHeight();
      int w = this.getWidth();
      int x = randomizer.nextInt(w/2);
      int y = randomizer.nextInt(h/2);
     
      Point point;
      clear();
      CloudPanel cloudpanel = new CloudPanel();

      for(int k=0; k <100; k++)
      {
         point = new Point(x,y);
         Cloud.add(point);
      }
      repaint();
    }
   
   public void clear()
   {
      Graphics g = getGraphics();
      Dimension d = getSize();
      Color c = getBackground();
      g.setColor(c);
      g.fillRect(0,0,d.width,d.height);
      repaint();
   }
   
   public void paintComponent(Graphics g)
   {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setColor(Color.BLACK);
   
       Cloud c = new Cloud();
       Random generator = new Random();
       int x = 0;
       int y = 0;
 
       for (int i = 0; i < 100; i++)
       {
          x = getWidth()/2 * generator.nextInt();
          y = getHeight()/2 * generator.nextInt();
          Cloud.add(new Point());
       }
 
       Cloud.draw(g2);
   
   }

   public static void setBackgroundImage(Image img)
   {
      // TODO Auto-generated method stub
     
   }
}
CoudFrame:
package Lab10;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//import java.awt.BorderLayout;

public class CloudFrame extends JFrame
{
   private static final long serialVersionUID = 1;
   private CloudPanel panel;
   private JButton b_save;
   private JButton b_new;
   private JButton b_clear;
   private JButton b_load;
   
   public CloudFrame()
   {
     setTitle("Cloud");
     setSize(600,350);
     
     panel = new CloudPanel();
     panel.setBackground(Color.BLUE);
     b_new = new JButton("New Cloud");
     b_save = new JButton("Save Cloud");
     b_load = new JButton("Load Cloud");
     b_clear = new JButton("Clear");
     
     JPanel controlPanel = new JPanel();
     controlPanel.setLayout(new GridLayout(1,1));
     
     JPanel controlPanel2 = new JPanel();
     controlPanel2.setLayout(new GridLayout(1,4));

     controlPanel.add(panel);
     controlPanel2.add(b_new);
     controlPanel2.add(b_save);
     controlPanel2.add(b_load);
     controlPanel2.add(b_clear);
     
     add(controlPanel,BorderLayout.CENTER);
     add(controlPanel2,BorderLayout.SOUTH);
     
    //o  instantiate the class that implements the ActionListener interface
     ActionListener NewListener = new NewButtonListener();
     b_new.addActionListener(NewListener);
     
     //o  instantiate the class that implements the ActionListener interface
     ActionListener SaveListener = new SaveButtonListener();
     b_save.addActionListener(SaveListener);
     
     //o  instantiate the class that implements the ActionListener interface
     ActionListener LoadListener = new LoadButtonListener();
     b_load.addActionListener(LoadListener);
     
     //o  instantiate the class that implements the ActionListener interface
     ActionListener ClearListener = new ClearButtonListener();
     b_clear.addActionListener(ClearListener);

   }
   
   public static void main(String[] args)
   {
      JFrame myFrame = new CloudFrame();
      myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      myFrame.setVisible(true);
   }
   
   private class NewButtonListener implements ActionListener
   {
     //actionPerformed() method
      public void actionPerformed(ActionEvent e)
       {
         if (e.getSource() == "New Cloud")
         {
            CloudPanel cp = new CloudPanel();
            cp.newCloud();
         }
         else
         {
            return;
         }
       }
   }
   
 
   private class LoadButtonListener implements ActionListener
   {
      //actionPerformed() method
      public void actionPerformed(ActionEvent e)
       {
         if (e.getSource() == "Load Cloud")
         {
            CloudPanel cp = new CloudPanel();
            cp.loadCloud();
         }
         else
         {
            return;
         }
       }
      }
   
   private class ClearButtonListener implements ActionListener
   {
     //actionPerformed() method
      public void actionPerformed(ActionEvent e)
       {
         if(e.getSource()=="Clear")
         {
            CloudPanel c = new CloudPanel();
            c.clear();
         }
         else
         {
            return;
         }

       }
   }

   private class SaveButtonListener implements ActionListener
   {
     //actionPerformed() method
      public void actionPerformed(ActionEvent e)
       {
         if(e.getSource()=="Save Cloud")
         {
            CloudPanel c = new CloudPanel();
            c.saveCloud();
         }
         else
         {
            return;
         }
       }
   }
}





Author:

Can you please add the exception ?

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Draw Cloud which Contains a list of points
 Draw Cardinal Spline With Mouse Clicks Points     -  
 Draw Bezier Curve and selecting the points with mouse click     -  
 tag cloud     -  
 cloud animation     -  
 Method to Get distance between two points     -  
 How to draw a gun     -  
 list and explode     -  
 Sort a list     -  
 display list     -  
 List interface     -  









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com