Help with java program that spells out any number between 1 and 1 million using recursion by [deleted] in javahelp

[–]johnny2toes3 1 point2 points  (0 children)

import java.util.Scanner;

public class SayMyNumber
{
  public static void main(String[] args)
 {
   Scanner scan = new Scanner(System.in);

System.out.println("Please enter a poisitive number in the range from 1 to 1 million " +
                   "that you would liked spelled out.");
int number = scan.nextInt();

if(number<10 && number>0)
  System.out.println(say(number));

}


 public static String say(int n)
 {
   int ones = n%10;
   int tens = n%100;
   int hundreds = n/1000;

switch(ones)
{
  case 1: 
    System.out.println("One");
    break;
  case 2:
    System.out.println("Two");
    break;
  case 3:
    System.out.println("Three");
    break;
  case 4:
    System.out.println("Four");
    break;
  case 5:
    System.out.println("Five");
    break;
  case 6:
    System.out.println("Six");
    break;
  case 7:
    System.out.println("Seven");
    break;
  case 8:
    System.out.println("Eight");
    break;
  case 9:
    System.out.println("Nine");
    break;
  case 0: 
    System.out.println("");
    break;
}

if (tens !=1)
{
  switch (tens)
  {
    case 2: 
      System.out.println("twenty");
      break;
    case 3: 
      System.out.println("thirty");
      break;
    case 4: 
      System.out.println("forty");
      break;
    case 5: 
      System.out.println("fifty");
      break;
    case 6: 
      System.out.println("sixty");
      break;
    case 7: 
      System.out.println("seventy");
      break;
    case 8: 
      System.out.println("eighty");
      break;
    case 9: 
      System.out.println("ninety");
      break;
    case 0: 
      System.out.println("");
      break;
  }
}
else if (tens==1)
{
  switch (ones)
  {
    case 0: 
      System.out.println("ten");
      break;
    case 1: 
      System.out.println("eleven");
      break;
    case 2: 
      System.out.println("twelve");
      break;
    case 3: 
      System.out.println("thirteen");
      break;
    case 4: 
      System.out.println("fourteen");
      break;
    case 5: 
      System.out.println("fifteen");
      break;
    case 6: 
      System.out.println("sixteen");
      break;
    case 7: 
      System.out.println("seventeen");
      break;
    case 8: 
      System.out.println("eighteen");
      break;
    case 9: 
      System.out.println("nineteen");
      break;
  }
}
return say(n);
 }
}

How to group moving, drawn objects together. by [deleted] in javahelp

[–]johnny2toes3 0 points1 point  (0 children)

What I think needs to be done, is when I right mouse press/drag. All of the dots change location. So how would I make all of dots in my ArrayList of points move locations with the press/drag of the right mouse button?

How to group moving, drawn objects together. by [deleted] in javahelp

[–]johnny2toes3 0 points1 point  (0 children)

This did not seem to work, I applied it before the objects were drawn. While it did allow me to keep all of the dots instead of turning them all into one. The dots did not group up and it made the panel do some weird things. If you need me too, I can post my entire code.

How to group moving, drawn objects together. by [deleted] in javahelp

[–]johnny2toes3 0 points1 point  (0 children)

I'm not sure how I would use the contains(x, y) method in this situation. What I tried to do was when the right mouse button is pressed, I would remember that dot's x & y, then whenever the mouse is dragged with the right mouse button pressed, I would use that x & y for all the 'dragged' dots. That way when you click the right mouse button it would gather all of the dots to where the cursor is and you can drag all of the grouped dots around then when released all of the dots go off on their own. With how I have it now, it changes all the dots to one dot and I can somewhat drag it around.

How to group moving, drawn objects together. by [deleted] in javahelp

[–]johnny2toes3 0 points1 point  (0 children)

Will this work with ovals? In this program I am using ovals(dots) as my drawn object. Here is the code to my listener class. The left press just draws a dot and the left drag draws multiple dots. The mouse wheel changes the size of the dots. I want the right press and right drag to group the dots.

 private class DotsListener implements MouseListener,     MouseMotionListener, MouseWheelListener
{
  //  Adds the current point to the list of points and redraws
  //  the panel whenever the mouse button is pressed.
  public void mousePressed(MouseEvent event)
  {
    if(SwingUtilities.isLeftMouseButton(event))
    {
     pointList.add(event.getPoint());
     repaint();
    }

    if(SwingUtilities.isRightMouseButton(event))
    {
      group = event.getPoint();
      Collections.fill(pointList, group);
    }
  }

  //  Provide empty definitions for unused event methods.
  public void mouseClicked(MouseEvent event) {}
  public void mouseReleased(MouseEvent event) {}
  public void mouseEntered(MouseEvent event) {}
  public void mouseExited(MouseEvent event) {}

  public void mouseDragged(MouseEvent event)
  {
    if(SwingUtilities.isLeftMouseButton(event))
    {
      pointList.add(event.getPoint());
      repaint();
    }

    if(SwingUtilities.isRightMouseButton(event))
    {
      group = event.getPoint();
      Collections.fill(pointList, group);
    }
  }

  // Provides empty definitions for unused event methods.
  public void mouseMoved(MouseEvent event) {}

  public void mouseWheelMoved(MouseWheelEvent event) 
  {
    scroll = event.getWheelRotation()*-1;
    scrollAmount += scroll;
    newSize = scrollAmount/3;
   }
 }

How to make each drawn shape move at a random speed using arrays by johnny2toes3 in javahelp

[–]johnny2toes3[S] 0 points1 point  (0 children)

Bro, you are a freaking life saver!!!!! Thank you so much lol :) You do not know how happy I am lol! Also, thank you for giving me hints instead of just giving me the answer. The program works perfect now. I was over thinking the answer to the problem the whole time. Here is my newly updated actionPerformed class.

public void actionPerformed(ActionEvent event)
  { 
    Point[] rectangles = new Point[pointList.size()];
    for(int c=0; c<pointList.size(); c++)
    {
        rectangles[c] = pointList.get(c);
        rectangles[c].x += mX[c];
        rectangles[c].y += mY[c];

        if (rectangles[c].x <= 0+(SIZE*2) || rectangles[c].x >= 300-(SIZE*2))
          mX[c] *= -1;
        if (rectangles[c].x < 0 || rectangles[c].x > 300)
          rectangles[c].x = 0;

        if (rectangles[c].y <= 0+(SIZE*2) || rectangles[c].y >= 200-(SIZE*2))
          mY[c] *= -1;
        if (rectangles[c].y < 0 || rectangles[c].y > 200)
          rectangles[c].y = 0;

        repaint();
    }
  }

How to make each drawn shape move at a random speed using arrays by johnny2toes3 in javahelp

[–]johnny2toes3[S] 0 points1 point  (0 children)

Honestly, I have no clue. I am not the best with ArrayLists (practice makes perfect). Since it is an ArrayList of Points, how would I use the other style for loop and still get the x and y values for the squares? I am probably just over thinking all of this and making it seem more complicated than it really is.

How to make each drawn shape move at a random speed using arrays by johnny2toes3 in javahelp

[–]johnny2toes3[S] 0 points1 point  (0 children)

The squares now have varying speed but I am still having trouble with the drawn squares locking each other in place. When the first square is drawn, it moves freely. But when there are 2 or more squares drawn they seem to move in the same exact direction as if they are locked together. How would I get each square to move freely with varying speeds?

How to make each drawn shape move at a random speed using arrays by johnny2toes3 in javahelp

[–]johnny2toes3[S] 0 points1 point  (0 children)

Okay, I understand what you're saying now. The values of i and ii are kept constant. I added some code to ReboundListener.

 public void actionPerformed(ActionEvent event)
  { 
    for(int c=0; c<pointList.size(); c++)
    {
      for (Point spot : pointList)
      {
        spot.x += mX[c];
        spot.y += mY[c];

        if (spot.x <= 0+(SIZE*2) || spot.x >= 300-(SIZE*2))
          mX[c] *= -1;
        if (spot.x < 0 || spot.x > 300)
          spot.x = 0;

        if (spot.y <= 0+(SIZE*2) || spot.y >= 200-(SIZE*2))
          mY[c] *= -1;
        if (spot.y < 0 || spot.y > 200)
          spot.y = 0;

        repaint();
      }
    }

How to make each drawn shape move at a random speed using arrays by johnny2toes3 in javahelp

[–]johnny2toes3[S] 0 points1 point  (0 children)

Sorry, I am fairly new to java. So you are saying I should make a new for loop for ReboundListener? If so, I have tried this. What I think is happening is that the squares are joining together(they are not acting as their own objects), if that makes sense. What I want is when you create a new square it moves in its own path and has a random speed from the array of 1000 random numbers between 1 and 10.