-5

I am a beginner in Java & i have to make a java program that takes the input from a 2d array with dimensions 347*697 (array's value range from 2200 to 4200 ). I have to do such that for every 100 increase in range there is a set of specific color(for e.g 2200-2300 color(214,217,223) & so on). I have never done anything .. & this is the program i found little bit similar ... Can anyone give me idea how to convert it I know its too much to ask for , but tomorrow i have to give a ppt. & when every option is closed i came here. Please guys, I need help badly

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public class Map extends JPanel {

public static final Color CITY = new Color(214,217,223);
public static final Color DESERT = new Color(255,204,102);
public static final Color DIRT_ROAD = new Color(153,102,0);
public static final Color FOREST = new Color(0,102,0);
public static final Color HILLS = new Color(51,153,0);
public static final Color LAKE = new Color(0,153,153);
public static final Color MOUNTAINS = new Color(102,102,255);
public static final Color OCEAN = new Color(0,0,153);
public static final Color PAVED_ROAD = new Color(51,51,0);
public static final Color PLAINS = new Color(102,153,0);

public static final Color[] TERRAIN = {
    CITY,
    DESERT,
    DIRT_ROAD,
    FOREST,
    HILLS,
    LAKE,
    MOUNTAINS,
    OCEAN,
    PAVED_ROAD,
    PLAINS
};

public static final int NUM_ROWS = 215;
public static final int NUM_COLS = 300;

public static final int PREFERRED_GRID_SIZE_PIXELS = 10;

// In reality you will probably want a class here to represent a map tile,
// which will include things like dimensions, color, properties in the
// game world.  Keeping simple just to illustrate.
private final Color[][] terrainGrid;

public Map(){
    this.terrainGrid = new Color[NUM_ROWS][NUM_COLS];
    Random r = new Random();
    // Randomize the terrain
    for (int i = 0; i < NUM_ROWS; i++) {
        for (int j = 0; j < NUM_COLS; j++) {
            int randomTerrainIndex = r.nextInt(TERRAIN.length);
            Color randomColor = TERRAIN[randomTerrainIndex];
            this.terrainGrid[i][j] = randomColor;
        }
    }
    int preferredWidth = NUM_COLS * PREFERRED_GRID_SIZE_PIXELS;
    int preferredHeight = NUM_ROWS * PREFERRED_GRID_SIZE_PIXELS;
    setPreferredSize(new Dimension(preferredWidth, preferredHeight));
}

@Override
public void paintComponent(Graphics g) {
    // Important to call super class method
    super.paintComponent(g);
    // Clear the board
    g.clearRect(0, 0, getWidth(), getHeight());
    // Draw the grid
    int rectWidth = getWidth() / NUM_COLS;
    int rectHeight = getHeight() / NUM_ROWS;

    for (int i = 0; i < NUM_ROWS; i++) {
        for (int j = 0; j < NUM_COLS; j++) {
            // Upper left corner of this terrain rect
            int x = i * rectWidth;
            int y = j * rectHeight;
            Color terrainColor = terrainGrid[i][j];
            g.setColor(terrainColor);
            g.fillRect(x, y, rectWidth, rectHeight);
        }
    }
}

public static void main(String[] args) {
    // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame frame = new JFrame("Game");
            Map map = new Map();
            frame.add(map);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    });
}

}

5
  • 1
    This question appears to be off-topic because it is primarily a work or homework dump. Jul 6, 2014 at 13:44
  • @HovercraftFullOfEels I just need logic how can i use different colors for different values in an Array ... I don't want you to solve it dude ..Trust me its really important, & i just need a bit of help
    – Shirish
    Jul 6, 2014 at 13:47
  • 1
    Trust me, it's important to us that you show the fruits of your efforts. This is not a site to do your homework for you. Jul 6, 2014 at 13:51
  • 1
    I'm sorry you feel that way. Myself, I'm much more receptive to questions that show some effort, rather than simply dumping "found" code and throwing up the hands, that show your code attempt, that ask specific questions about your code attempt. That's just how I am. Jul 6, 2014 at 14:03
  • @HovercraftFullOfEels I already clarified it that i don't know java applets and GUI & the code output was also to explain everyone understand what i have in my mind(coz i m unaware of terms). I knew that i will encounter someone like you too... who will constantly remind me of my fault of not knowing this language instead of helping. U meant it or not, but none offense taken here.. take care mate :)
    – Shirish
    Jul 6, 2014 at 14:16

1 Answer 1

0

Create a byte[] with the rgb values for each pixel of the image you want. The byte[] length will be width * height * 3 (3 values for each pixel). Look at this for an example of how create a BufferedImage from the byte[].

Another alternative is to create a BufferedImage and set the rgb value for each pixel directly. In this case, you have to bit twiddle all 3 rgb values into a single int value.

3
  • Second method u suggested , Can i define value of rgb to a range of values also by using a for loop?
    – Shirish
    Jul 6, 2014 at 13:55
  • You must set the specific rgb value for each pixel. This would likely be done from within nested for loops over width and height. Jul 6, 2014 at 14:03
  • Thanks for your help. Now i know what i have to do. Thanks again :)
    – Shirish
    Jul 6, 2014 at 14:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.