Results 1 to 6 of 6
  1. #1
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default FEEDBACK: Simple Game

    Hi, I read Graphics2D in java, and try to create a simple game. I have no problem in logical (so far), but I am not sure if I did it correct and I want someone to see or give feedback about this. What I am worrying is the program in paint method
    Java Code:
     * fill the whole JPanel with color gray
     * Loop while integer is less than array list
           draw circle
           draw string
    this is what happen for 1 minute. I was thinking if I can update only the part that change (the circle only).

    Here is the code:
    Java Code:
    [B]PnlBombLevel.java[/B]
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.geom.GeneralPath;
    import java.util.ArrayList;
    import javax.swing.AbstractAction;
    import javax.swing.ActionMap;
    import javax.swing.InputMap;
    import javax.swing.JComponent;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
    import javax.swing.Timer;
    
    public class PnlBombLevel extends JPanel implements KeyListener
    {
        ArrayList<Bomb> listOfBomb = new ArrayList<Bomb>();
        Timer refresh, tmrNewBomb, tmrStopper;
        InputMap im;
        //ActionMap am;
        String latestKey = "";
    
        private final int MAX_BOMB = 5;
        private int numberOfBomb = 0;
        private int totalNumberOfBomb = 0;
        private int secondsLeft = 60;
        private int correctHit = 0;
        GeneralPath p = new GeneralPath();
        public PnlBombLevel()
        {
            setPreferredSize(new Dimension(200,200));
            addActionMap();
            addKeyListener(this);
            setFocusable(true);
            requestFocusInWindow();
            refresh = new Timer(0, new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    repaint();
                }
            });
            tmrNewBomb = new Timer(2000, new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    if(numberOfBomb < MAX_BOMB)
                    {
                        addBomb();
                    }
                }
            });
            tmrStopper = new Timer(1000, new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    System.out.println(secondsLeft);
                    secondsLeft--;
                    if(secondsLeft < 1)
                    {
                        refresh.stop();
                        tmrNewBomb.stop();
                        tmrStopper.stop();
                        stopBombs();
    
                        System.out.println(correctHit + " out of " + totalNumberOfBomb);
                        System.out.println(totalNumberOfBomb/correctHit + "%");
                    }
                }
            });
            refresh.start();
            tmrNewBomb.start();
            tmrStopper.start();
        }
    
        @Override
        public void paint(Graphics g)
        {
            Graphics2D g2d = (Graphics2D)g.create();
            
            int w = getWidth();
            int h = getHeight();
    
            g2d.setColor(Color.GRAY);
            g2d.fillRect(0, 0, w, h);
            for(int i = 0; i < listOfBomb.size(); i++)
            {
                g2d.setColor(Color.BLACK);
                g2d.drawOval(listOfBomb.get(i).getXaxis(),
                        listOfBomb.get(i).getYaxis(),
                        listOfBomb.get(i).getWidht(), 
                        listOfBomb.get(i).getHeight());
    
                g2d.fillOval(listOfBomb.get(i).getXaxis(),
                        listOfBomb.get(i).getYaxis(),
                        listOfBomb.get(i).getWidht(),
                        listOfBomb.get(i).getHeight());
                g2d.setColor(Color.WHITE);
                g2d.drawString(listOfBomb.get(i).getLetter(),
                        listOfBomb.get(i).getXaxis() + 6,
                        listOfBomb.get(i).getYaxis() + 15);
            }
        }
    
        private void addBomb()
        {
            listOfBomb.add(numberOfBomb, new Bomb(getWidth()));
            addKey(listOfBomb.get(numberOfBomb).getLetter());
            listOfBomb.get(numberOfBomb).start();
            numberOfBomb++;
            totalNumberOfBomb++;
        }
    
        private void stopBombs()
        {
            for(int i = 0; i < listOfBomb.size(); i++)
            {
                listOfBomb.remove(i).stop();
            }
        }
    
        private void showResult()
        {
            //Compute
        }
    
        private void addKey(String key)
        {
            im.put(KeyStroke.getKeyStroke(key), "REMOVE_BOMB");
        }
    
        private void addActionMap()
        {
            ActionMap am = this.getActionMap();
            am.put("REMOVE_BOMB", new AbstractAction()
            {
                public void actionPerformed(ActionEvent e)
                {
                    for(int i = 0; i < listOfBomb.size(); i++)
                    {
                        if(listOfBomb.get(i).getLetter().equalsIgnoreCase(latestKey))
                        {
                            listOfBomb.remove(i).stop();
                            numberOfBomb--;
                            correctHit++;
                        }
                    }
                }
            });
            im = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        }
    
        public void keyPressed(KeyEvent ke)
        {
            latestKey = String.valueOf(ke.getKeyChar());
            System.out.println("PRESSED: " + latestKey);
        }
    
        public void keyTyped(KeyEvent ke){}
        public void keyReleased(KeyEvent ke){}
        
    }
    
    
    [B]Bomb.java[/B]
    import typingtest.Utilities.Letters;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import javax.swing.Timer;
    
    public class Bomb
    {
        private int lastX, lastY;
        private final int MY_WIDTH = 20;
        private final int MY_HEIGHT = 20;
        private String letter;
        Timer bombTimer;
        boolean pressedKey = false;
        Random random = new Random();
        public Bomb(int maxX)
        {
            Letters aLetter = new Letters();
            letter = aLetter.getNext();
            lastY = -20;
            lastX = random.nextInt(maxX);
            bombTimer = new Timer(80, null);
            bombTimer.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    lastY += 5;
                }
            });
        }
    
        public void start()
        {
            bombTimer.start();
        }
    
        public void restart()
        {
            bombTimer.restart();
        }
    
        public void stop()
        {
            bombTimer.stop();
        }
    
        public int getWidht()
        {
            return MY_WIDTH;
        }
    
        public int getHeight()
        {
            return MY_HEIGHT;
        }
    
        public int getXaxis()
        {
            return lastX;
        }
    
        public void setXaxis(int x)
        {
            lastX = x;
        }
    
        public int getYaxis()
        {
            return lastY;
        }
    
        public void setYaxis(int y)
        {
            lastY = y;
        }
    
        public String getLetter()
        {
            return letter;
        }
    Thanks in advance.
    Gail
    Last edited by mine0926; 07-23-2011 at 06:20 AM.

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

    Default

    Quote Originally Posted by mine0926 View Post
    ...this is what happen for 1 minute. I was thinking if I can update only the part that change (the circle only).
    The repaint method has some overloads, some that limit the region requested to be repainted, and you may wish to look into this.


    Here is the code:
    Java Code:
        @Override
        public void paint(Graphics g)
        {
            //...
        }
    Why are you painting in the paint override and not the paintComponent override?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    A couple of comments on that override:
    Java Code:
        // in the constructor
        setBackground(Color.GRAY);
        //
    
        @Override
        // public void paint(Graphics g)
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D)g.create();
            
            // int w = getWidth();
            // int h = getHeight();
    
            // g2d.setColor(Color.GRAY);
            // g2d.fillRect(0, 0, w, h);
            for(int i = 0; i < listOfBomb.size(); i++)
            {
                /* g2d.setColor(Color.BLACK);
                g2d.drawOval(listOfBomb.get(i).getXaxis(),
                        listOfBomb.get(i).getYaxis(),
                        listOfBomb.get(i).getWidht(), 
                        listOfBomb.get(i).getHeight());
    
                g2d.fillOval(listOfBomb.get(i).getXaxis(),
                        listOfBomb.get(i).getYaxis(),
                        listOfBomb.get(i).getWidht(),
                        listOfBomb.get(i).getHeight());
                g2d.setColor(Color.WHITE);
                g2d.drawString(listOfBomb.get(i).getLetter(),
                        listOfBomb.get(i).getXaxis() + 6,
                        listOfBomb.get(i).getYaxis() + 15); */
                listOfBomb.get(i).paintBomb(g2d);
            }
        }
    
    
    // in Bomb.java
        public void paintBomb(Graphics2D g2d)
        {
            g2d.setColor(Color.BLACK);
            g2d.drawOval(lastX,
                    lastY,
                    MY_WIDTH, 
                    MY_HEIGHT);
    
            g2d.fillOval(lastX,
                    lastY,
                    MY_WIDTH,
                    MY_HEIGHT);
    
            g2d.setColor(Color.WHITE);
            g2d.drawString(getLetter(),
                    lastX + 6,
                    lastY + 15);
        }
    The Bomb class should know how to paint itself. Also IMO the constants for width and height are final, so you don't need the two getters. If they are needed to be accessed from outside the class, they could be public.

    Then again, unless you have a static field involved in the Letters class (i.e. bad design -- providing an instance method that performs a utility task), constructing a new Letters() in the constructor of each bomb seems both superfluous and likely to return the same latter from getNext(). There should probably be just one static Letters field in Bomb, and its getNext() should be called in the constructor. Here too, as the letter is set only in the constructor and cannot be changed, that could be a public final variable, doing away with the need for a getLetter() method.

    db
    Last edited by DarrylBurke; 07-23-2011 at 03:17 PM.

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Quote Originally Posted by Fubarable View Post
    The repaint method has some overloads, some that limit the region requested to be repainted, and you may wish to look into this.
    That's why I am not confident on what I did and so post here.

    Quote Originally Posted by Fubarable View Post
    Why are you painting in the paint override and not the paintComponent override?
    My mistake, I remember that I was corrected before about this. :p

  5. #5
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Quote Originally Posted by DarrylBurke View Post
    A couple of comments on that override:
    Java Code:
        // in the constructor
        setBackground(Color.GRAY);
        //
    
        @Override
        // public void paint(Graphics g)
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D)g.create();
            
            // int w = getWidth();
            // int h = getHeight();
    
            // g2d.setColor(Color.GRAY);
            // g2d.fillRect(0, 0, w, h);
            for(int i = 0; i < listOfBomb.size(); i++)
            {
                /* g2d.setColor(Color.BLACK);
                g2d.drawOval(listOfBomb.get(i).getXaxis(),
                        listOfBomb.get(i).getYaxis(),
                        listOfBomb.get(i).getWidht(), 
                        listOfBomb.get(i).getHeight());
    
                g2d.fillOval(listOfBomb.get(i).getXaxis(),
                        listOfBomb.get(i).getYaxis(),
                        listOfBomb.get(i).getWidht(),
                        listOfBomb.get(i).getHeight());
                g2d.setColor(Color.WHITE);
                g2d.drawString(listOfBomb.get(i).getLetter(),
                        listOfBomb.get(i).getXaxis() + 6,
                        listOfBomb.get(i).getYaxis() + 15); */
                listOfBomb.get(i).paintBomb(g2d);
            }
        }
    
    
    // in Bomb.java
        public void paintBomb(Graphics2D g2d)
        {
            g2d.setColor(Color.BLACK);
            g2d.drawOval(lastX,
                    lastY,
                    MY_WIDTH, 
                    MY_HEIGHT);
    
            g2d.fillOval(lastX,
                    lastY,
                    MY_WIDTH,
                    MY_HEIGHT);
    
            g2d.setColor(Color.WHITE);
            g2d.drawString(getLetter(),
                    lastX + 6,
                    lastY + 15);
        }
    I like it more... :)
    Thanks

    Quote Originally Posted by DarrylBurke View Post
    The Bomb class should know how to paint itself.
    that was what I trying to do but I just dont know how to paint it from other class so I just did it in PnlBombLevel class. Maybe because I read the trail of Graphics2D for 1 day only. I change it already base on your example. :)

    Quote Originally Posted by DarrylBurke View Post
    Also IMO the constants for width and height are final, so you don't need the two getters. If they are needed to be accessed from outside the class, they could be public.

    Then again, unless you have a static field involved in the Letters class (i.e. bad design -- providing an instance method that performs a utility task), constructing a new Letters() in the constructor of each bomb seems both superfluous and likely to return the same latter from getNext(). There should probably be just one static Letters field in Bomb, and its getNext() should be called in the constructor. Here too, as the letter is set only in the constructor and cannot be changed, that could be a public final variable, doing away with the need for a getLetter() method.
    What is IMO?
    I will check Letters class and post it here too.

    Thanks.

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

Similar Threads

  1. need a simple java game
    By Shashwat in forum New To Java
    Replies: 1
    Last Post: 01-03-2011, 02:42 PM
  2. simple game programming help
    By b.m in forum New To Java
    Replies: 0
    Last Post: 11-25-2010, 05:16 PM
  3. Replies: 8
    Last Post: 09-23-2010, 12:48 AM
  4. I need feedback on my TicTacToe game
    By kiregad in forum New To Java
    Replies: 4
    Last Post: 03-21-2010, 10:09 PM
  5. Simple Game example
    By mayuresh34 in forum CLDC and MIDP
    Replies: 1
    Last Post: 10-08-2009, 01:21 PM

Posting Permissions

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