Рисование на jframe - Java
Формулировка задачи:
Добрый денёк.
В один момент надоело решать задачки джавы "а-ля хабрахабр", решился игру наподобие марио написать.
Незадача. Не умею рисовать на форме. суть: читаю двумерный массив из файла:
1 0 1 0 0 1 0 1 0 1
1 0 1 0 0 1 0 1 0 1
1 0 1 0 0 1 0 1 0 1
1 0 1 0 0 1 0 1 0 1
1 0 1 0 0 1 0 1 0 1
1 0 1 0 0 1 0 1 0 1
1 0 1 0 0 1 0 1 0 1
1 0 1 0 0 1 0 1 0 1
1 0 1 0 0 1 0 1 0 1
1 0 1 0 0 1 0 1 0 1
В классе Мар:
вывожу в консоль. Ура работает, однако налПоинтерЭкзепшн портит жизнь. Подскажите, где я туплю с графикой?
форма должна быть заполнена клеточками 32 на 32 пиксела. соответсвуя массиву array
p.s. Только учусь))
Листинг программы
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package mapion2;
- import java.io.File;
- import java.io.IOException;
- import java.util.Scanner;
- /**
- *
- * @author например дмитрий
- */
- public class Map {
- int [][] array; //десь храним карту
- public Map() {
- w = 10;
- h = 10;
- array = new int[w][h];
- }
- int w;
- int h;
- public int getWigth() {
- return w;
- }
- public int getHeigth() {
- return h;
- }
- public int[][] getArray() {
- return array;
- }
- void readMap () throws IOException { //читаем из файла
- File file = new File("J:\\Java\\Mapion2\\src\\mapion2\\input.txt");
- Scanner scanner = new Scanner(file);
- String tmpString;
- for (int i = 0; i < 10; i++) {
- tmpString = scanner.nextLine();
- String [] tmp = tmpString.split(" ");
- for (int j = 0; j < 10; j++) {
- array [i][j] = Integer.parseInt(tmp[j]);
- }
- }
- return;
- }
- void printConsoleArray() { //тестовый вывод в консоль
- for (int i = 0; i < array.length; i++) {
- for (int j = 0; j < array.length; j++) {
- System.out.print(array[i][j] + " ");
- }
- System.out.println();
- }
- return;
- }
- }
Листинг программы
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package mapion2;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.JFrame;
- /**
- *
- * @author например дмитрий
- */
- public class Mapion2 {
- /**
- * @param args the command line arguments
- * @throws java.io.IOException
- */
- public static void main(String[] args) throws IOException {
- // java.awt.Image imgEarth;
- // imgEarth = new ImageIcon("earth.bmp").getImage();
- // java.awt.Image imgHeaven;
- // imgHeaven = new ImageIcon("heaven.bmp").getImage();
- Map map = new Map();
- map.readMap();
- map.printConsoleArray();
- JFrame jframe;
- jframe = new JFrame("null");
- JPanel jaJPanel;
- jaJPanel = new JPanel();
- jframe.setSize(32 * map.getWigth(), 32 * map.getHeigth());
- jframe.add(jaJPanel);
- BufferedImage imgEarth = null;
- try {
- imgEarth = ImageIO.read(new File("earth.bmp"));
- } catch (IOException e) {
- }
- Graphics g1 = jaJPanel.getGraphics();
- BufferedImage imgHeaven = null;
- try {
- imgHeaven = ImageIO.read(new File("heaven.bmp"));
- } catch (IOException e) {
- }
- int [][] array;
- array = map.getArray();
- for (int i = 0; i < map.getWigth(); i++) {
- for (int j = 0; j < map.getHeigth(); j++) {
- if (array[i][j] == 0) {
- g1.drawImage(imgEarth, j, j, Color.yellow, jframe);
- }
- if (array[i][j] == 1) {
- g1.drawImage(imgHeaven, j, j, Color.yellow, jframe);
- }
- }
- }
- jframe.setVisible(true);
- }
- }
Листинг программы
- int [][] array;
- array = map.getArray();
- for (int i = 0; i < map.getWigth(); i++) {
- for (int j = 0; j < map.getHeigth(); j++) {
- if (array[i][j] == 0) {
- g1.drawImage(imgEarth, i * 32, j * 32, Color.yellow, jframe);
- }
- if (array[i][j] == 1) {
- g1.drawImage(imgHeaven, i * 32, j * 32, Color.yellow, jframe);
- }
- }
- }
хммм
Решение задачи: «Рисование на jframe»
textual
Листинг программы
- public class JaJpanel extends JPanel {
- @Override
- protected void paintComponent(Graphics g) {
- Graphics2D g2d = (Graphics2D) g;
- //тут рисуем то что нужно используя в качестве графического контекста g2d
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д