Results 1 to 10 of 10
Thread: Checkerboard Program
- 09-20-2010, 09:48 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
Checkerboard Program
Hi guys, my CPS 181 class professor is having us right a OOP Program that asks user for board colors and checker colors and then draws it out on a canvas. I have everything written correctly but i dont know how to get my checkers to place on specific squares. Any help would be greatly appreciated.
To save space i will post ONLY the driver (checkerboard.java) and not the other .java files im using
Java Code:/** * This is the start of the checkerboard * program that displays a checkerboard in * the starting position. * * @author Justin Newberry * @VERSION Final */ import java.util.Scanner; public class CheckerBoard { // Put your data attributes here private Square[][] sqrs; // Declare a 2D array private Circle[][] circles; private final int NUMROWS = 8; private final int NUMCOLS = 8; private final int SIZESQUARE = 50; private final int BORDER = 40; private final int SIZECIRCLE = 30; public int color; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub CheckerBoard board = new CheckerBoard(); board.create(); } public CheckerBoard() { // Instantiate or initialize attributes here sqrs = null; circles = null; } /** * Creates the checker board. This method currently just draws 2 squares as an example. * Use this as a starting point in this assignment. * */ private void create() { Canvas.getCanvas(); // Draw the squares //Square sq1 = new Square(); //sq1.makeVisible(); //Square sq2 = new Square(); //sq2.moveLeft(); //sq2.changeColor("blue"); //sq2.makeVisible(); createsqrs(); createcircles(); } public void createsqrs() { // Create a 2D array of Squares // Create the array of rows sqrs = new Square[NUMROWS][]; Scanner color = new Scanner(System.in); System.out.print("Enter a color for the first set of squares: "); String color1 = color.next(); System.out.print("Enter a color for the second set of squares: "); String color2 = color.next(); for(int i = 0; i < sqrs.length; i++) { sqrs[i] = new Square[NUMCOLS]; for(int j = 0; j < sqrs[i].length; j++) { if((i + j) % 2 == 0) { sqrs[i][j] = new Square(SIZESQUARE, BORDER + i * SIZESQUARE, BORDER + j * SIZESQUARE, color1); } else { sqrs[i][j] = new Square(SIZESQUARE, BORDER + i * SIZESQUARE, BORDER + j * SIZESQUARE, color2); } sqrs[i][j].makeVisible(); } } } public void createcircles(){ circles = new Circle[NUMROWS][NUMCOLS]; Scanner color = new Scanner(System.in); System.out.print("Enter a color for the first set of checkers: "); String color3 = color.next(); System.out.print("Enter a color for the second set of checkers: "); String color4 = color.next(); int[] xPos1 = {0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2}; int[] yPos1 = {1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7}; for(int i = 0; i < circles.length; i++) { circles[i] = new Circle[NUMCOLS]; for(int j = 0; j < circles[i].length; j++) { if((i + j) % 2 == 0) { circles[i][j] = new Circle(SIZECIRCLE, BORDER + xPos1[i] * SIZECIRCLE, BORDER + yPos1[j] * SIZECIRCLE, color3); } else { circles[i][j] = new Circle(SIZECIRCLE, BORDER + xPos1[i] * SIZECIRCLE, BORDER + yPos1[j] * SIZECIRCLE, color4); } sqrs[i][j].makeVisible(); } } } }Last edited by Jnoobs; 09-20-2010 at 10:01 PM.
- 09-21-2010, 12:07 AM #2
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
Will someone please help :[
-
I'm not sure that there's enough information in your question for us to be able to help you. You may wish to go into more detail on your problem and perhaps give us more information about what GUI library you're using, more about the other classes, etc...
- 09-21-2010, 01:08 AM #4
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
im using 5 .java files: Canvas.java, Checkerboard.java, Square.java, Circle.java, and Geometricshapes.java
I have the program working to draw the checkerboard 2 different colors, i also have been able to make the program draw the checkers (circle.java) with 2 different colors but i dont know how to get them to place on their starting spots (by that i mean how a checkerboard game is setup before playing). This program doesnt actually play checkers, only draws a checkerboard and its checker pieces
My professor replied with this in an email:We had discussed making some 1-D arrays of i and j positions for the starting square of each checker, like:
int[] xPos1 = {0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, };
int[] yPos1 = {1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7};
or something like this, then accessing the array elements in a formula like the square formula.
Let me know if this helps.
- 09-21-2010, 02:06 AM #5
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
im stuck on this one part and cant get around it and its due in 2 hours :/
-
1) I still don't know which GUI library you're using. Swing? AWT? SWT? Something different?
2) It looks as if you're mixing GUI code with console (Scanner code). For most GUI libraries, this is an invitation for trouble. If this is Swing, then you may wish to create a dialog or use a JOptionPane to get user input.
3) If you do create a Scanner object, don't forget to close() it when you're done with it. Else you could potentially run out of resources (doubtful in this small program, but nevertheless, it could happen in a future program so this is always a good habit to get into -- if you use a resource, close it when you're done).
4) You still haven't given us a clue as to how the Square or Circle classes work, and likely that is the key on how to draw a Circle.
In short, given the information you've given us, and more importantly the information you haven't given us, I think that it is impossible for anyone to help. If you still need help, you'll probably want to give us more info. Also, while it may seem harsh, but your due date/time is your concern, not ours. Our goal is to help you learn Java. It's up to you to use this knowledge to pass your class, but it is not our concern.
Much luck.
- 09-21-2010, 03:17 AM #7
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
im using swing library and using Eclipse as the IDE. The scanner works just fine.
square:
circle:Java Code:import java.awt.*; /* Author: Justin Newberry * Date: 8/20/2010 */ public class Square extends GeometricShape { private int size; public Square(){ this(50, 40, 40, "blue"); } public Square(int size, int x, int y, String color) { super(x, y, color); this.size = size; } public void changeSize(int newSize) { erase(); size = newSize; draw(); } public void changeColor(String newColor) { color = newColor; draw(); } protected void draw() { if(isVisible) { Canvas canvas = Canvas.getCanvas(); canvas.draw(this, color, new Rectangle(xPosition, yPosition, size, size)); canvas.wait(10); } } }
Java Code:import java.awt.geom.*; /*Author: Justin Newberry * Date: 8/20/2010 */ public class Circle extends GeometricShape { protected int diameter; public Circle(){ this(30, 20, 60, "blue"); } public Circle(int size, int x, int y, String color) { super(x, y, color); this.diameter = size; } public void changeSize(int newDiameter) { erase(); diameter = newDiameter; draw(); } public void changeColor(String newColor) { color = newColor; draw(); } protected void draw() { if(isVisible) { Canvas canvas = Canvas.getCanvas(); canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition, diameter, diameter)); canvas.wait(10); } } }
-
Most of your GUI code is not Swing code, and I suspect that while perhaps you're indirectly using Swing there's another library, perhaps one for students, that's sitting on top of it. For instance GeometricShape is not a class of Swing or AWT libraries, and while AWT has a Canvas class, the standard Canvas class has no draw method. So sorry, I can't help. :( Perhaps someone else will recognize the library you're working with, but if not, you can speed up the process by telling us.
Regardless, best of luck and good night!
- 09-21-2010, 03:35 AM #9
- 09-21-2010, 03:46 AM #10
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
Reason i am using a scanner is bc that is what my prof asked us to use.
i was wrong about swing GUI, it is awt im using and here are the other 2 .java's so that u can see what im working with
GeometricShape:
Canvas:Java Code:public abstract class GeometricShape { protected int xPosition; protected int yPosition; protected String color; protected boolean isVisible; public static void main(String[] args) { } public GeometricShape() { xPosition = 50; yPosition = 40; color = "red"; isVisible = false; } public GeometricShape(int x, int y, String color) { xPosition = x; yPosition = y; this.color = color; isVisible = false; } public void makeVisible() { isVisible = true; draw(); } public void makeInvisible() { erase(); isVisible = false; } public void moveRight() { moveHorizontal(20); } public void moveLeft() { moveHorizontal(-20); } public void moveUp() { moveVertical(-20); } public void moveDown() { moveVertical(20); } public void moveHorizontal(int distance) { erase(); xPosition += distance; draw(); } public void moveVertical(int distance) { erase(); yPosition += distance; draw(); } public void slowMoveHorizontal(int distance) { int delta; if(distance < 0) { delta = -1; distance = -distance; } else { delta = 1; } for(int i = 0; i < distance; i++) { xPosition += delta; draw(); } } public void slowMoveVertical(int distance) { int delta; if(distance < 0) { delta = -1; distance = -distance; } else { delta = 1; } for(int i = 0; i < distance; i++) { yPosition += delta; draw(); } } protected void erase() { if(isVisible) { Canvas canvas = Canvas.getCanvas(); canvas.erase(this); } } protected abstract void draw(); }
Java Code:import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /*Author: Justin Newberry * Date: 8/20/2010 */ public class Canvas { // Note: The implementation of this class (specifically the handling of // shape identity and colors) is slightly more complex than necessary. This // is done on purpose to keep the interface and instance fields of the // shape objects in this project clean and simple for educational purposes. private static Canvas canvasSingleton; /** * Factory method to get the canvas singleton object. */ public static Canvas getCanvas() { if(canvasSingleton == null) { canvasSingleton = new Canvas("Checkerboard Program", 480, 480, Color.white); //i reset the name of the canvas } canvasSingleton.setVisible(true); return canvasSingleton; } // ----- instance part ----- private JFrame frame; private CanvasPane canvas; private Graphics2D graphic; private Color backgroundColour; private Image canvasImage; private List<Object> objects; private HashMap<Object,ShapeDescription> shapes; /** * Create a Canvas. * @param title title to appear in Canvas Frame * @param width the desired width for the canvas * @param height the desired height for the canvas * @param bgClour the desired background colour of the canvas */ private Canvas(String title, int width, int height, Color bgColour) { frame = new JFrame(); canvas = new CanvasPane(); frame.setContentPane(canvas); frame.setTitle(title); canvas.setPreferredSize(new Dimension(width, height)); backgroundColour = bgColour; frame.pack(); objects = new ArrayList<Object>(); shapes = new HashMap<Object,ShapeDescription>(); } /** * Set the canvas visibility and brings canvas to the front of screen * when made visible. This method can also be used to bring an already * visible canvas to the front of other windows. * @param visible boolean value representing the desired visibility of * the canvas (true or false) */ public void setVisible(boolean visible) { if(graphic == null) { // first time: instantiate the offscreen image and fill it with // the background colour Dimension size = canvas.getSize(); canvasImage = canvas.createImage(size.width, size.height); graphic = (Graphics2D)canvasImage.getGraphics(); graphic.setColor(backgroundColour); graphic.fillRect(0, 0, size.width, size.height); graphic.setColor(Color.black); } frame.setVisible(visible); } /** * Draw a given shape onto the canvas. * @param referenceObject an object to define identity for this shape * @param color the color of the shape * @param shape the shape object to be drawn on the canvas */ // Note: this is a slightly backwards way of maintaining the shape // objects. It is carefully designed to keep the visible shape interfaces // in this project clean and simple for educational purposes. public void draw(Object referenceObject, String color, Shape shape) { objects.remove(referenceObject); // just in case it was already there objects.add(referenceObject); // add at the end shapes.put(referenceObject, new ShapeDescription(shape, color)); redraw(); } /** * Erase a given shape's from the screen. * @param referenceObject the shape object to be erased */ public void erase(Object referenceObject) { objects.remove(referenceObject); // just in case it was already there shapes.remove(referenceObject); redraw(); } /** * Set the foreground colour of the Canvas. * @param newColour the new colour for the foreground of the Canvas */ public void setForegroundColor(String colorString) { if(colorString.equals("red")) graphic.setColor(Color.red); else if(colorString.equals("black")) graphic.setColor(Color.black); else if(colorString.equals("blue")) graphic.setColor(Color.blue); else if(colorString.equals("yellow")) graphic.setColor(Color.yellow); else if(colorString.equals("green")) graphic.setColor(Color.green); else if(colorString.equals("magenta")) graphic.setColor(Color.magenta); else if(colorString.equals("white")) graphic.setColor(Color.white); else graphic.setColor(Color.black); } /** * Wait for a specified number of milliseconds before finishing. * This provides an easy way to specify a small delay which can be * used when producing animations. * @param milliseconds the number */ public void wait(int milliseconds) { try { Thread.sleep(milliseconds); } catch (Exception e) { // ignoring exception at the moment } } /** * Redraw ell shapes currently on the Canvas. */ private void redraw() { erase(); for(Iterator<Object> i=objects.iterator(); i.hasNext(); ) { ((ShapeDescription)shapes.get(i.next())).draw(graphic); } canvas.repaint(); } /** * Erase the whole canvas. (Does not repaint.) */ private void erase() { Color original = graphic.getColor(); graphic.setColor(backgroundColour); Dimension size = canvas.getSize(); graphic.fill(new Rectangle(0, 0, size.width, size.height)); graphic.setColor(original); } /************************************************************************ * Inner class CanvasPane - the actual canvas component contained in the * Canvas frame. This is essentially a JPanel with added capability to * refresh the image drawn on it. */ private class CanvasPane extends JPanel { /** * */ private static final long serialVersionUID = 1L; @Override public void paint(Graphics g) { g.drawImage(canvasImage, 0, 0, null); } } /************************************************************************ * Inner class CanvasPane - the actual canvas component contained in the * Canvas frame. This is essentially a JPanel with added capability to * refresh the image drawn on it. */ private class ShapeDescription { private Shape shape; private String colorString; public ShapeDescription(Shape shape, String color) { this.shape = shape; colorString = color; } public void draw(Graphics2D graphic) { setForegroundColor(colorString); graphic.fill(shape); } } }Last edited by Jnoobs; 09-21-2010 at 03:49 AM.
Similar Threads
-
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 06:53 PM -
Creating Checkerboard from a 2 dimensional 'for' loop.
By New2Java in forum New To JavaReplies: 3Last Post: 07-23-2009, 07:45 AM -
Creating Checkerboard from a 2 dimensional 'for' loop
By New2Java in forum New To JavaReplies: 1Last Post: 07-22-2009, 10:10 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks