Results 1 to 2 of 2
  1. #1
    ranss41 is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: 2d array grid in JPanel with random placed images

    this is cool...I want to know how to put background image to whole 2d array grid. to following code:

    import java.awt.*;
    import java.awt.geom.Line2D;
    import javax.swing.*;

    public class MakingAGrid extends JPanel {
    SquareRx[][] squares;
    final int PAD = 10;
    //ImageIcon sky = new ImageIcon ("http://www.java-forums.org/images/1.png");
    public MakingAGrid() {
    int ROWS = 10;
    int COLS = 10;
    squares = new SquareRx[ROWS][COLS];
    for(int i = 0; i < ROWS; i++) {
    for(int j = 0; j < COLS; j++) {
    squares[i][j] = new SquareRx(i, j);
    }
    }
    }
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
    RenderingHints.VALUE_ANTIALIAS_ON);

    int w = getWidth();
    int h = getHeight();
    double xInc = (double)(w - 2*PAD)/squares[0].length;
    double yInc = (double)(h - 2*PAD)/squares.length;
    // Draw vertical grid lines.
    g2.setPaint(Color.blue);
    for(int i = 0; i <= squares[0].length; i++) {
    double x = PAD + i*xInc;
    g2.draw(new Line2D.Double(x, PAD, x, h-PAD));
    }
    // Draw horizontal grid lines.
    for(int i = 0; i <= squares.length; i++) {
    double y = PAD + i*yInc;
    g2.draw(new Line2D.Double(PAD, y, w-PAD, y));
    }
    }
    public static void main(String[] args) {
    MakingAGrid test = new MakingAGrid();
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(test);
    f.setSize(400,400);
    f.setLocation(200,200);
    f.setVisible(true);
    }
    }


    class SquareRx {
    private final int row;
    private final int col;
    private boolean occupied = false;
    public SquareRx(int r, int c) {
    row = r;
    col = c;
    }
    public void setOccupied(boolean occupied) {
    this.occupied = occupied;
    }

    public boolean isOccupied() {
    return occupied;
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: 2d array grid in JPanel with random placed images

    ranss41, another moderator already told you not to post to old dead threads.
    how can I draw a board in 2d array?

    I've moved this post to a new thread. If you continue to ignore moderator advice, you may be banned for a period.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 8
    Last Post: 11-29-2011, 02:07 AM
  2. Help making a grid inside of a JPanel
    By satanfx55 in forum AWT / Swing
    Replies: 3
    Last Post: 05-13-2011, 07:18 PM
  3. Replies: 10
    Last Post: 01-12-2011, 07:16 PM
  4. Replies: 0
    Last Post: 12-16-2010, 05:14 PM
  5. random images on the fly??
    By FunHog in forum AWT / Swing
    Replies: 3
    Last Post: 03-31-2009, 05:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •