Results 1 to 4 of 4

Thread: Class help?

  1. #1
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Class help?

    For some reason all of the code compiles, there's just a problem with the SueFace class that won't let me make an object of Sue class. o.o

    Sue: (I'm adding the other face.setImage and face.setMessage stuff later)


    Java Code:
    public class Sue
    {
        SueFace face;
        int happiness = 30;
        int cheeriosCount = 20;
        int jobSecurity = 30;
        int teamSkill;
        int gleeCallCount;
        int totalCallCount;
        int gleeDestroy;
        double ratio;
        
        // constructor
        public Sue()
        {
            face = new SueFace();
            face.setImage("normal");
            face.setMessage("Your face makes me want to puke.");
        }
        
        public String seeInstructions()
        {
            return ("Game Goal: Win Nationals. You will begin by training and hating on other people. Various actions will raise or lower your happiness, Cheerios count, team skill, and job security. You have a limit of 50 moves before going to Nationals is required, but you can challenge whenever you feel you're ready. You may only challenge once. If you lose at nationals, get fired, or get depressed, you automatically lose the game.");
        }
    
        public String checkStats()
        {
            return ("Happiness = " + happiness +", Cheerios count = " + cheeriosCount + ", Job security = " + jobSecurity + ", Team skill = " + teamSkill + ", Ratio of Glee-ruining calls to total calls = " + ratio);
        }
        
        public void holdTryouts(int audits)
        {
            if (audits > 5) {
            cheeriosCount = cheeriosCount + 2;
            }
            totalCallCount = totalCallCount + 1;
            jobSecurity = jobSecurity + audits;
            checkDeath();
            checkRatio();
                }
        
        public void hurtStudentsPhysically(int victims)
        {
            happiness = happiness + 3*victims;
            gleeCallCount = gleeCallCount + 1;
            totalCallCount = totalCallCount + 1;
            jobSecurity = jobSecurity - 5;
            gleeDestroy = gleeDestroy + 10;
            checkDeath();
            checkRatio();
        }
        
        public void makeFunOfPeople(int victims)
        {
            happiness = happiness + 2*victims;
            gleeCallCount = gleeCallCount + 1;
            totalCallCount = totalCallCount + 1;
            jobSecurity = jobSecurity - 5;
            gleeDestroy = gleeDestroy + 5;
            checkDeath();
            checkRatio();
        }
        
        public void petitionArts()
        {
            happiness = happiness + 10;
            gleeCallCount = gleeCallCount + 1;
            totalCallCount = totalCallCount + 1;
            jobSecurity = jobSecurity - 10;
            gleeDestroy = gleeDestroy + 10;
            checkDeath();
            checkRatio();
        }
        
        public void havePractice (int hours)
        {
            if ((hours > 20) || (hours < 0)) {
                System.out.println("Invalid number of hours.");
            }
             else {
                 if (hours > 7) {
                  cheeriosCount = cheeriosCount - hours/2;
                 }        
            teamSkill = teamSkill + 3*hours;
            happiness = happiness + 2;
            totalCallCount = totalCallCount + 1;
            jobSecurity = jobSecurity + 10;
            checkRatio();
            checkDeath();
           }
        }
        
        public void drinkProteinShake() {
            happiness = happiness + 3;
            totalCallCount = totalCallCount + 1;
            checkRatio();
            checkDeath();
        }
        
        public void visitSister() {
            happiness = happiness + 20;
            totalCallCount = totalCallCount + 1;
            checkRatio();
            checkDeath();
        }
        
        private void checkGlee()
        {
            if (gleeDestroy >= 50) {
                System.out.println("Congratulations! Glee Club has been destroyed.");
                happiness = happiness + 25;
            }
        }
        
        private void checkRatio()
        {
            ratio = 1.0* gleeCallCount/totalCallCount;
            if (ratio <= .2) {
                System.out.println("You suck. Glee Club has succeeded in having fun and winning regionals.");
                happiness = happiness - 30;
            }
        }
        
        private void checkDeath()
        {
            if (totalCallCount >= 50) {
                System.out.println("You have reached competition season, and have no moves left. You are now going to Nationals.");
                goToNationals();
            }
    
            else if (happiness < 30) {
                getDepressed();
            }
            else if (jobSecurity < 10) 
            {
                jobWarning();
            }
            else if (jobSecurity < 0)
            {
                getFired();
            }
            
        }
       
        private String getDepressed () 
        {
            return ("Game over. Sue has become depressed.");
        }
        
        private String jobWarning ()
        {
            return ("Figgins is giving you a warning. Next time you're fired.");
        }
        
        private String getFired ()
        {
            return ("Game over. Figgins has fired Sue.");
        }
        
        public void goToNationals()
        {
            if ((cheeriosCount >= 20) && (teamSkill >= 30))
            {
                winNationals();
            }
            else
            {
                loseNationals();
            }        
        }
        
        private String loseNationals()
        {
            return ("Game over. You lose at nationals.");
        }
        
        private String winNationals()
        {
            return ("Congratulations! You win nationals!");
        }
    }
    
    SueFace:
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Image;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FilenameFilter;
    import java.util.ArrayList;
    
    
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.Timer;
    import javax.swing.border.Border;
    
    
    public class SueFace extends JFrame implements ActionListener{
    
        private final int WIDTH = 400;
        private final int HEIGHT = 400;
        private ImagePanel imagePanel;
        private JTextPane textArea;
        private String base;
        private int loopslot = 0;
        private String[] files;
        private Image[] allPics;
        private ArrayList<Image> pics;
        private Timer timer;
        
    
        private static final String imageBase = "./pet_images/";
        
        public static void main(String args[]) {
            Sue newPet = new Sue();   
        }
        
        public SueFace() {
            try {
                javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        createGUI();
                    }
                });
            } catch (Exception e) {
                System.err.println("createGUI didn't successfully complete");
            }
            
            init();
        }
        
        public void init() {
            String curDir = System.getProperty("user.dir");
            
            base = curDir + "/" + imageBase;
            pics = new ArrayList<Image>();
            timer = new Timer(400, this);
            //timer.setInitialDelay(1000);
    
            getAllImages();
            
            setBackground();
            //setImage("angel");      
            //setMessage("Hello, and Welcome!");
        }
        
        public void createGUI() {
            setMaximumSize(new Dimension(WIDTH, HEIGHT));
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(new Dimension(WIDTH, HEIGHT));
            Container contentPane = this.getContentPane();
            contentPane.setLayout(new GridBagLayout());
            contentPane.setBackground(Color.white);
           
            
            imagePanel = new ImagePanel();
            int width = 200;
            int height = 200;
            imagePanel.setPreferredSize(new Dimension(width, height));
            imagePanel.setMinimumSize(new Dimension(width, height));
            imagePanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
            GridBagConstraints c = new GridBagConstraints();
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 1;
            contentPane.add(imagePanel,c);
            
            textArea = new JTextPane();
            textArea.setEditable(false);
            JScrollPane scroll = new JScrollPane(textArea);
            scroll.setPreferredSize(new Dimension(width, height/2));
            scroll.setSize(new Dimension(width, height/2));
            textArea.setPreferredSize(new Dimension(width, height/2));
            textArea.setSize(new Dimension(width, height/2));
    
    
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridwidth = 2;
            c.gridx = 0;
            c.gridy = 2;
            c.ipady = 20;
            contentPane.add(scroll, c);
        
            setLocationRelativeTo(null);
            setVisible(true);
            //toFront();
            setAlwaysOnTop(true);
            setAlwaysOnTop(false);
        }
        
        private void setBackground() {
            Image backImage = createImage(base+"background.png", "");
            Border bkgrnd = new CentredBackgroundBorder(backImage);
            ((JComponent) getContentPane()).setBorder(bkgrnd);
        }
        
        protected Image createImage(String path, String description) {
               return new ImageIcon(path, description).getImage();
        }
    
        public void setImage(String mood) {
            timer.stop();
            pics.clear();
            getImages(mood);
            timer.start(); 
        }
    
         public void actionPerformed(ActionEvent e) {
                loopslot++;
    
                if (loopslot >= pics.size()) {
                    loopslot = 0;
                }
    
                imagePanel.repaint();
    
                if (loopslot == pics.size()) {
                    timer.restart();
                }
            }
    
        public void getAllImages() {
            File dir = new File(base);   
            files  = dir.list();
            allPics = new Image[files.length];
            for (int i = 0; i < files.length; i++) {
                //System.err.println(files[i]);
                allPics[i]=createImage(base + files[i],"");
                
            }
            //System.err.println(pics.size());
        }       
            
            
        public void getImages(final String mood) {
        
            for (int i = 0; i < files.length; i++) {
                if (files[i].contains(mood)) {
                    pics.add(allPics[i]);
                }
            }
            //System.err.println(pics.size());
        }
        
        public void setMessage(String message) {
            String current = textArea.getText();
            textArea.setText(current + "\n" + message);
            textArea.select(current.length(), (current.length() + message.length() + 1));
        }
        
        
        public class ImagePanel extends JPanel {
            public ImagePanel( ) {
                super();
            }
    
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (pics.size() > 0) {
                    g.drawImage(pics.get(loopslot), 0, 0, this.getWidth(), this.getHeight(), null);
                }
            }
    
        }
    
        public class CentredBackgroundBorder implements Border {
            private final Image image;
         
            public CentredBackgroundBorder(Image image) {
                this.image = image;
            }
         
            public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
                g. drawImage(image, 0, 0, SueFace.this.getWidth(), SueFace.this.getHeight(),null);
            }
         
            public Insets getBorderInsets(Component c) {
                return new Insets(0,0,0,0);
            }
         
            public boolean isBorderOpaque() {
                return true;
            }
        }
    }
    If anyone can make sense of this, then can that one help me? Thanks xP

    -Ray
    Last edited by sunde887; 10-03-2011 at 12:15 AM. Reason: Changed quote tags to code, [code]...[/code]

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Class help?

    Quote Originally Posted by ShatteringBlue View Post
    For some reason all of the code compiles, there's just a problem with the SueFace class that won't let me make an object of Sue class. o.o

    Sue: (I'm adding the other face.setImage and face.setMessage stuff later)
    ....

    If anyone can make sense of this, then can that one help me?
    I'm sorry, but I can't. Can you:
    • First of all, edit your original post and change all [quote] and [/quote] tags to [code] and [/code] tags so that your code is actually readable.
    • Please spend a little time and create a few sentences that describe in greater detail just what your problem is.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Class help?

    Hey ray, I changed your quote tags to code tags, in the future you can use code tags by typing [code]YOUR CODE HERE[/code]

    Also, I (and I'm sure others here) are lazy, and can't be bothered to look through so much code. You will get more help by trying to give us a small piece of code which demonstrates your problem(and is compilable). See Fubars signature--all of the links are good. SSCCE is the one which details what we would prefer to look at as opposed to 3298578763 lines of code.

  4. #4
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: Class help?

    Hm.. Please close this thread for now mods, I'm just going to fidget around with the code to figure out the problem. Thanks anyhoo~

Similar Threads

  1. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  2. Replies: 5
    Last Post: 01-26-2011, 06:38 PM
  3. Replies: 8
    Last Post: 04-14-2010, 05:49 AM
  4. Replies: 7
    Last Post: 05-06-2009, 12:42 PM
  5. Replies: 8
    Last Post: 03-21-2009, 09:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •