Hello everyone,
I'm new at programming but everyone starts as newbeginner, right? I'm supposed to make chess patterns on a panel in different sizes but something goes wrong.
It work fine but it doesn't. I think I'm missing something but what I don't know.
Here's the code:
import java.awt.*; public class ChessPattern { public static void main(String[] args0){ DrawingPanel panel = new DrawingPanel(420, 300); panel.setBackground(Color.darkGray); Graphics g = panel.getGraphics(); square(g, 0, 0, 10, 5, 5); square(g, 210, 30, 15, 4, 4); square(g, 60, 40, 15, 7, 7); } //method to draw chess pattern squares public static void square(Graphics g, int x, int y, int size, int squarecolumn, int squarerow){ //the column for(int j = 0; j < squarecolumn; j++){ //the row for(int i = 0; i < squarerow; i++){ //if i is even for row and column then gray if(i%2 == 0 && j%2 == 0){ g.setColor(Color.gray); //if i is odd for row and column then gray }else if(i%2 == 1 && j%2 == 1){ g.setColor(Color.gray); //if i is even for row and j is odd for column then white }else if(i%2 == 0 && j%2 == 1){ g.setColor(Color.white); //if i is odd for row and j is even for column then white }else if(i%2 == 1 && j%2 == 0){ g.setColor(Color.white); } //where and how much to fill going by row g.fillRect(x+i*size,y+j*size, size, size); } //where and how much to fill going by column g.fillRect(x, y+j*size, size, size); } //Drawing the reactangle border g.setColor(Color.black); g.drawRect(x, y, size*squarecolumn, size*squarerow); } }
and here't how it looks:
I found out that everytime I let the squares be even x even size then I get a pattern like the right while odd x odd size gets me the chess pattern I want.
Ex: square(g, 0, 0, 10, 5, 5);
square(g, 210, 30, 15, 4, 4);
square(g, 60, 40, 15, 7, 7);
Could someone help me out and tell/explain me what I am missing or doing wrong?
Thanks^^
PS: For people who wants the DrawingPanel class which was on the CD as support to GUI programming:
/* Stuart Reges and Marty Stepp 07/01/2005 The DrawingPanel class provides a simple interface for drawing persistent images using a Graphics object. An internal BufferedImage object is used to keep track of what has been drawn. A client of the class simply constructs a DrawingPanel of a particular size and then draws on it with the Graphics object, setting the background color if they so choose. To ensure that the image is always displayed, a timer calls repaint at regular intervals. */ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class DrawingPanel implements ActionListener { public static final int DELAY = 250; // delay between repaints in millis private static final String DUMP_IMAGE_PROPERTY_NAME = "drawingpanel.save"; private static String TARGET_IMAGE_FILE_NAME = null; private static final boolean PRETTY = true; // true to anti-alias private static boolean DUMP_IMAGE = false; // true to write DrawingPanel to file private int width, height; // dimensions of window frame private JFrame frame; // overall window frame private JPanel panel; // overall drawing surface private BufferedImage image; // remembers drawing commands private Graphics2D g2; // graphics context for painting private JLabel statusBar; // status bar showing mouse position private long createTime; static { TARGET_IMAGE_FILE_NAME = System.getProperty(DUMP_IMAGE_PROPERTY_NAME); DUMP_IMAGE = (TARGET_IMAGE_FILE_NAME != null); } // construct a drawing panel of given width and height enclosed in a window public DrawingPanel(int width, int height) { this.width = width; this.height = height; this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); this.statusBar = new JLabel(" "); this.statusBar.setBorder(BorderFactory.createLineBorder(Color.BLACK)); this.panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0)); this.panel.setBackground(Color.WHITE); this.panel.setPreferredSize(new Dimension(width, height)); this.panel.add(new JLabel(new ImageIcon(image))); // listen to mouse movement MouseInputAdapter listener = new MouseInputAdapter() { public void mouseMoved(MouseEvent e) { DrawingPanel.this.statusBar.setText("(" + e.getX() + ", " + e.getY() + ")"); } public void mouseExited(MouseEvent e) { DrawingPanel.this.statusBar.setText(" "); } }; this.panel.addMouseListener(listener); this.panel.addMouseMotionListener(listener); this.g2 = (Graphics2D)image.getGraphics(); this.g2.setColor(Color.BLACK); if (PRETTY) { this.g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); this.g2.setStroke(new BasicStroke(1.1f)); } this.frame = new JFrame("Drawing Panel"); this.frame.setResizable(false); this.frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { if (DUMP_IMAGE) { DrawingPanel.this.save(TARGET_IMAGE_FILE_NAME); } System.exit(0); } }); this.frame.getContentPane().add(panel); this.frame.getContentPane().add(statusBar, "South"); this.frame.pack(); this.frame.setVisible(true); if (DUMP_IMAGE) { createTime = System.currentTimeMillis(); this.frame.toBack(); } else { this.toFront(); } // repaint timer so that the screen will update new Timer(DELAY, this).start(); } // used for an internal timer that keeps repainting public void actionPerformed(ActionEvent e) { this.panel.repaint(); if (DUMP_IMAGE && System.currentTimeMillis() > createTime + 4 * DELAY) { this.frame.setVisible(false); this.frame.dispose(); this.save(TARGET_IMAGE_FILE_NAME); System.exit(0); } } // obtain the Graphics object to draw on the panel public Graphics2D getGraphics() { return this.g2; } // set the background color of the drawing panel public void setBackground(Color c) { this.panel.setBackground(c); } // show or hide the drawing panel on the screen public void setVisible(boolean visible) { this.frame.setVisible(visible); } // makes the program pause for the given amount of time, // allowing for animation public void sleep(int millis) { try { Thread.sleep(millis); } catch (InterruptedException e) {} } // take the current contents of the panel and write them to a file public void save(String filename) { String extension = filename.substring(filename.lastIndexOf(".") + 1); // create second image so we get the background color BufferedImage image2 = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB); Graphics g = image2.getGraphics(); g.setColor(panel.getBackground()); g.fillRect(0, 0, this.width, this.height); g.drawImage(this.image, 0, 0, panel); // write file try { ImageIO.write(image2, extension, new java.io.File(filename)); } catch (java.io.IOException e) { System.err.println("Unable to save image:\n" + e); } } // makes drawing panel become the frontmost window on the screen public void toFront() { this.frame.toFront(); } }