Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 8th, 2006, 06:45 PM
Authorized User
 
Join Date: Sep 2006
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem with animation

why is the line with the code
Code:
if(ball.intersects(r) && ball.y_speed > 0)
throwing a NullPointerException?
Code:
/*program: deleting hardrive
 *description: a mock hardrive deletion
 */
import javax.swing.border.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;

public class Main extends JFrame implements ActionListener{
    public static void main(String []args){
        new Main();
        new otherwindow();
        new animationWindow();
    }

    private JRadioButton yesbutton;
    private JRadioButton nobutton;
    private JRadioButton laterbutton;
    private JButton enter;
    private JTextField othertext;
    private JLabel deleting;
    private JLabel other;
    private JLabel mb;
    private JScrollPane scroll;
    private ButtonGroup option;
    private JSlider slider;
    private JComboBox combo;


    public Main(){
        this.setSize(500, 800);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Deleting Hardrive.");
        this.setLocation(750, 0);
        this.setBackground(Color.WHITE);

        JPanel mainpanel = new JPanel();
        JPanel panel = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        JPanel panel4 = new JPanel();

        yesbutton = new JRadioButton("yes");
        nobutton = new JRadioButton("no");
        laterbutton = new JRadioButton("Not right now.");
        enter = new JButton("Enter text");


        slider = new JSlider(0, 1000000, 1000000);
        slider.setMajorTickSpacing(500000);
        slider.setMinorTickSpacing(0);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);

        combo = new JComboBox();

        combo.addItem("0");
        combo.addItem("500000");
        combo.addItem("1000000");

        yesbutton.addActionListener(this);
        nobutton.addActionListener(this);
        laterbutton.addActionListener(this);
        enter.addActionListener(this);

        option = new ButtonGroup();

        option.add(yesbutton);
        option.add(nobutton);
        option.add(laterbutton);

        othertext = new JTextField(15);

        scroll = new JScrollPane(
                othertext,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED

        );

        mb = new JLabel("How many MBs to delete...");

        deleting = new JLabel("Deleting Hardrive.");
        other = new JLabel("other: ");

        Border mainb = BorderFactory.createTitledBorder("Deleting Hardrive");
        mainpanel.setBorder(mainb);

        Border b = BorderFactory.createTitledBorder("Deleting");
        panel.setBorder(b);

        Border b2 = BorderFactory.createTitledBorder("Yes, no, or later.");
        panel2.setBorder(b2);

        Border b3 = BorderFactory.createTitledBorder("Other");
        panel3.setBorder(b3);

        Border b4 = BorderFactory.createTitledBorder("MBs");
        panel4.setBorder(b4);

        mainpanel.setLayout(new GridLayout(4, 1));

        panel.add(deleting);

        panel2.add(yesbutton);
        panel2.add(nobutton);
        panel2.add(laterbutton);

        panel3.add(other);
        panel3.add(scroll);
        panel3.add(enter);

        panel4.add(mb);
        panel4.add(slider);
        panel4.add(combo);

        mainpanel.add(panel);
        mainpanel.add(panel2);
        mainpanel.add(panel3);
        mainpanel.add(panel4);

        this.add(mainpanel);


        this.setVisible(true);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == nobutton || e.getSource() == laterbutton){
            nobutton.setText("HAHA! Clicking the button doesn't do anything!");
            laterbutton.setText("HAHA! Clicking the button doesn't do anything!");
            JOptionPane.showMessageDialog(
                    null,
                    "Deleting Hardrive...",
                    "Deleting Hardrive...",
                    JOptionPane.DEFAULT_OPTION
            );
        }
        else if(e.getSource() == yesbutton){
            String selected = (String)combo.getSelectedItem();
            JOptionPane.showMessageDialog(
                    null,
                    "Finished deleting " + slider.getValue() + "MBs + " + selected + "MBs, please reboot.",
                    "Finished, please reboot.",
                    JOptionPane.DEFAULT_OPTION
            );
        }
        else if(e.getSource() == enter){
            other.setText("you want to do: " + othertext.getText());
        }
    }
}
class PaintSurface extends JComponent{
    ArrayList<Shape> shapes = new ArrayList<Shape>();
    Point startDrag, endDrag;

    public PaintSurface(){
        this.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                    startDrag = new Point(e.getX(), e.getY());
                    endDrag = startDrag;
                    repaint();
                }
                public void mouseReleased(MouseEvent e){
                    Shape r = makeEllipse(
                            startDrag.x, startDrag.y,
                            e.getX(), e.getY()
                    );
                    shapes.add(r);
                    startDrag = null;
                    endDrag = null;
                    repaint();
                }
        });
        this.addMouseMotionListener(new MouseMotionAdapter(){
           public void mouseDragged(MouseEvent e){
               endDrag = new Point(e.getX(), e.getY());
               repaint();
           } 
        });
    }
    public void paint(Graphics g){
        Graphics2D g2 = (Graphics2D)g;

        //turn on antialiasing
        g2.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON
        );

        g2.setPaint(Color.LIGHT_GRAY);
        for(int i = 0; i < getSize().width; i += 10){
            Shape line = new Line2D.Float(i, 0, i, getSize().height);
            g2.draw(line);
        }
        for(int i = 0; i < getSize().height; i += 10){
            Shape line = new Line2D.Float(0, i, getSize().width, i);
            g2.draw(line);
        }

        Color []colors = {
            Color.RED, Color.BLUE, Color.YELLOW, Color.PINK,
            Color.MAGENTA, Color.ORANGE, Color.GREEN
        };

        int colorIndex = 0;

        g2.setStroke(new BasicStroke(2));
        g2.setComposite(AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, 0.50f));

        for(Shape s : shapes){
            g2.setPaint(Color.BLACK);
            g2.draw(s);
            g2.setPaint(colors[(colorIndex++) % 6]);
            g2.fill(s);
        }

        if(startDrag != null && endDrag != null){
            g2.setPaint(Color.LIGHT_GRAY);
            Shape r = makeEllipse(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
            g2.draw(r);
        }
    }
    private Ellipse2D.Float makeEllipse(int x1, int y1, int x2, int y2){
        int x = Math.min(x1, x2);
        int y = Math.min(y1, y2);

        int width = Math.abs(x1 - x2);
        int height = Math.abs(y1 - y2);


        return new Ellipse2D.Float(x, y, width, height);
    }
}
class otherwindow extends JFrame{
    public otherwindow(){
        this.setSize(500, 500);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setLocation(0, 0);

        this.setBackground(Color.WHITE);

        this.setTitle("Draw an ellipse Here!");


        this.add(new PaintSurface());


        this.setVisible(true);
    }
}
class animationWindow extends JFrame{
    public animationWindow(){
        this.setSize(250, 250);
        this.setLocation(500, 0);
        this.setBackground(Color.WHITE);


        Thread t = new AnimationThread(this);
        t.start();

        this.add(new AnimationSurface());

        this.setVisible(true);
    }
}
class AnimationThread extends Thread{
    JFrame c;

    public AnimationThread(JFrame c){
        this.c = c;
    }

    public void run(){
        while(true){
            c.repaint();
            try{
                Thread.sleep(20);
            }
            catch(InterruptedException ex){
                //swallow the exception
            }
        }
    }
}
class AnimationSurface extends JComponent{
    int paddle_x = 125;

    int paddle_y = 10;

    int score = 0;

    float english = 1.0f;

    Ball ball;

    private int colorIndex = 0;

    Color []colors = {
        Color.RED, Color.ORANGE, Color.BLUE, 
                Color.MAGENTA, Color.PINK, Color.YELLOW
    };

    public void AnimationSurface(){
        addMouseMotionListener(new MouseMotionAdapter(){
            public void mouseMoved(MouseEvent e){
                if(e.getX() - 30 - paddle_x > 5){
                    english = 1.5f;
                }
                else if(e.getX() - 30 - paddle_x < -5){
                    english = -1.5f;
                }
                else{
                    english = 1.0f;

                }
                paddle_x = e.getX() - 30;
                paddle_y = 20;
            }
        });
        ball = new Ball(20);
    }
    public void paint(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON
        );

        Shape paddle = new Rectangle2D.Float(paddle_x, paddle_y, 50, 10);

        g2.setColor(colors[colorIndex % 6]);

        try{
            Rectangle2D r = new Rectangle2D.Float(paddle_x, paddle_y, 50, 10);
            if(ball.intersects(r) && ball.y_speed > 0){
                ball.y_speed = -ball.y_speed;
                ball.x_speed = (int)(ball.x_speed * english);
                if(english != 1.0f){
                    score += Math.abs(ball.x_speed * 10);
                }
                if(ball.getY() + ball.getHeight() >= 250){
                    ball = new Ball(20);
                    score -= 1000;
                    colorIndex = 0;
                }
                ball.move();
                g2.fill(ball);
                g2.fill(paddle);

                g2.drawString("Score: " + score, 0, 0);
            }
        }
        catch(NullPointerException e){
            g2.setPaint(Color.BLUE);

            g2.drawString(e.toString(), 50, 50);

            g2.setColor(colors[colorIndex % 6]);
        }
    }
}
class Ball extends Ellipse2D.Float{
  public int x_speed, y_speed;
  private int d;
  private int width = 250;
  private int height = 250;

  public Ball(int diameter){
      super((int)(Math.random() * (250 - 20) + 1),
              0, diameter, diameter);
      this.d = diameter;
      this.x_speed = (int)(Math.random() * 5 + 5);
      this.y_speed = (int)(Math.random() * 5 + 5);
  }
  public void move(){
      if(super.x < 0 || super.x > width - d){
          x_speed = -x_speed;
      }
      if(super.y < 0 || super.y > height - d){
          y_speed = -y_speed;
      }
      super.x += x_speed;
      super.y += y_speed;
  }
}
"Judge a man by his questions, not by his answers."
-Voltaire
__________________
\"Judge a man by his questions, not by his answers.\"
-Voltaire





Similar Threads
Thread Thread Starter Forum Replies Last Post
Image Animation Andraw Java GUI 0 October 9th, 2008 08:51 PM
Text Animation flash2004 Flash (all versions) 2 May 18th, 2005 12:41 AM
Animation Control JelfMaria VB How-To 2 March 14th, 2005 12:11 AM
Animation pallone Javascript How-To 9 February 2nd, 2005 03:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.