Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Can anyone explain this code?
I am currently taking a computer class and need help understanding this code and it would help if you could explain what is going in detail in these three classes.
/**
* Implements a falling letter cube for the Ramblecs game
*/
import java.awt.*;
public class FallingCube
{
private int xLeft; // Left margin for cubes
private final int cubeSize;
private int cubeX, cubeY; // Cube coordinates (upper left corner)
private int yStep; // Distance (in pixels) to move down
// in one timer cycle
private int direction= 1;
private static final String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private char randomLetter; // Cube letter
public FallingCube(int size, int horzPos)
{
cubeSize = size;
xLeft = horzPos;
yStep = cubeSize / 8;
cubeX = -cubeSize; // off the board --
cubeY = -cubeSize; // not displayed
}
public void setHorizontalPos (int x)
{
x=xLeft;
}
public void start()
{
int i = (int)(Math.random() * letters.length());
randomLetter = letters.charAt(i);
cubeX = 0;
cubeY = yStep; //-cubeSize; // above the board for smooth entry
}
public boolean moveDown()
{
if (cubeY >= 6 * cubeSize)
{
direction=-direction;
}
if (cubeY <= yStep)// land this cube:
{
direction=-direction;
}
cubeY += yStep * direction;
return true;
}
public void draw(Graphics g)
{
int x = xLeft + cubeX;
int y = cubeY;
int j = (int)(Math.random() * 12);
//if (j==0)
//{
//g.setColor(Color.red);
//return true;
//}
g.setColor(Color.red);
g.fill3DRect(x, y, cubeSize-1, cubeSize-1, true);
g.setColor(Color. white);
g.fillRoundRect(x + 5, y + 5, cubeSize - 10, cubeSize - 10,
cubeSize/2 - 5, cubeSize/2 - 5);
g.setColor(Color.darkGray);
String s = String.valueOf(randomLetter);
g.drawString(s, x + cubeSize/2 - 6, y + cubeSize/2 + 5);
}
}
/**
* Implements a panel on which "letter cubes" fall
* in the Ramblecs applet
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LetterPanel extends JPanel
implements ActionListener
{
private FallingCube cube; //cube2, cube3;
private final int CUBESIZE = 40;
private final int delay = 30;
private Timer t;
public LetterPanel()
{
cube = new FallingCube(CUBESIZE, 2*CUBESIZE);
t = new Timer(delay, this);
//cube2 = new FallingCube(CUBESIZE, 3*CUBESIZE);
//cube3 = new FallingCube(CUBESIZE, 4*CUBESIZE);
}
public void dropCube()
{
cube.start();
//cube2.start();
//cube3.start();
t.start();
}
/**
* Processes timer events
*/
public void actionPerformed(ActionEvent e)
{
boolean moved = cube.moveDown();
//boolean moved2 = cube2.moveDown();
//boolean moved3 = cube3.moveDown();
if (!moved) // "If not moved... "
{
t.stop();
}
/**if (!moved2) // "If not moved... "
{
t.stop();
}
if (!moved3) // "If not moved... "
{
t.stop();
}*/
cube.setHorizontalPos(getWidth()/2-CUBESIZE/2);
//cube2.setHorizontalPos(getWidth()*2-CUBESIZE*2);
//cube3.setHorizontalPos(getWidth()*2-CUBESIZE*2);
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g); // call JPanel's paintComponent
cube.draw(g);
//cube2.draw(g);
//cube3.draw(g);
}
}
/**
* This is the applet class for the Ramblecs game.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Ramblecs extends JApplet
//implements ActionListener
{
private LetterPanel whiteboard;//, yellowboard;
private JButton go;
private ControlPanel controlpanel;
public void init()
{
whiteboard = new LetterPanel();
//yellowboard = new LetterPanel();
//yellowboard.setBackground(Color.green);
whiteboard.setBackground(Color.red);
whiteboard.setSize(600, 200);
//Jbutton go = new JButton("Submit");
controlpanel = new ControlPanel(whiteboard);
//go.addActionListener(this);
Container c = getContentPane();
c.add(whiteboard, BorderLayout.CENTER);
// Box b = Box.createHorizontalBox();
// b.add(whiteboard);
//b.add(yellowboard);
// Container c = getContentPane();
c.add(controlpanel, BorderLayout.EAST);
// c.add(b, BorderLayout.CENTER);
//c.add(go, BorderLayout.NORTH);
}
/**
* Processes Go button events.
*/
/*public void actionPerformed(ActionEvent e)
{
if (Math.random() < 1)
{
whiteboard.dropCube();
}
else
{
//
THIS IS NOT IN C++!!!!!!
1 Answer
- 10 years ago
Seriously. Do you read the textbook?
From quick look, it is about the object called: falling cube
they are about the size, dimension and color of the object.
Then, it has a control panel with buttons of how to change color if you click on them
To understand it, you need to put these code in the test
Remove some code and test to see what code affects what.
Basically, garbage in garbage out
check it out: http://www.cplusplus.com/doc/tutorial/
Hope it helps.
Peace out