Создание двухмерного массива Point - Java
Формулировка задачи:
Добрый день. Подскажите, что не так пожалуйста.
Задача в окне JFrame нарисовать стену из кубиков (картинок)
Ошибка при компилировании
я так понимаю обращение идет к элементу нулевому. Вроде как картинок хватает, т.е у меня 6 кирпичей вдлинну, и 4 линии.
Спасибо заранее. Прошу не судить строго, я только начинаю.
Код окна
public class BrickWall { private int stein_b=100, stein_h=40; // ширина кубика, высота кубика private int ppWidth = 600;// ширина окна private int ppHeight = 800; // высота окна private Point[][] position= {null,{new Point(1,1)}}; private Image[] imgArray = new Image[6]; Image img; public int lines; public BrickWall( ) { int p_x=0; int p_y=100; for (int i=0; i<lines; i++){ for(int j=0; j<6; j++){ position[i][j]=new Point(); } } for (int i=0; i<lines; i++) { for (int j=0; j<6; j++){ position[i][j].x=p_x; position[i][j].y=p_y; p_x=p_x+stein_b; } p_y=p_y+stein_h; } img = new ImageIcon("1.jpg").getImage(); imgArray[0] = img; img = new ImageIcon("2.jpg").getImage(); imgArray[1] = img; img = new ImageIcon("3.jpg").getImage(); imgArray[2] = img; img = new ImageIcon("4.jpg").getImage(); imgArray[3] = img; img = new ImageIcon("5.jpg").getImage(); imgArray[4] = img; img = new ImageIcon("6.jpg").getImage(); imgArray[5] = img; } public void paintComponent(Graphics g) { // zeichnet den Ball int image_index=0; for (int i=0; i<lines; i++){ for(int j=0; j<6; j++){ g.drawImage(imgArray[image_index],position[i][j].x,position[i][j].y,null); image_index=image_index+1; } image_index=0;} } public void setLines(int lines) { this.lines=lines; } public int getLines() { return lines; }
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at BrickWall.paintComponent(BrickWall.java:84) at Breakout.paint(Breakout.java:144) at javax.swing.RepaintManager$4.run(Unknown Source) at javax.swing.RepaintManager$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.access$1200(Unknown Source) at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.Image; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import javax.swing.ImageIcon; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; public class Breakout extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private BrickWall wall = new BrickWall(); private JMenuBar menuBar = new JMenuBar(); public Breakout() { JMenu infoMenu = new JMenu("Info"); menuBar.add(infoMenu); JMenuItem rules = new JMenuItem("Spielregeln"); rules.addActionListener(this); rules.setActionCommand("rules"); infoMenu.add(rules); JMenuItem keyboardAssignment = new JMenuItem("Tastaturbelegung"); keyboardAssignment.addActionListener(this); keyboardAssignment.setActionCommand("keyboardAssignment"); infoMenu.add(keyboardAssignment); JMenu gameMenu = new JMenu("Spiel"); menuBar.add(gameMenu); JMenuItem newGame = new JMenuItem("neues Spiel"); newGame.addActionListener(this); newGame.setActionCommand("newGame"); gameMenu.add(newGame); JMenuItem endGame = new JMenuItem("Spiel beenden"); endGame.addActionListener(this); endGame.setActionCommand("endGame"); gameMenu.add(endGame); this.setJMenuBar(menuBar); setLayout(new BorderLayout()); JLabel background=new JLabel(new ImageIcon("background.jpg")); add(background); setVisible(true); setSize(600, 800); setTitle("Breakout"); setLocationRelativeTo(null); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent ae) { String menu = ae.getActionCommand(); switch (menu) { case "rules": showRules(); break; case "keyboardAssignment": showKeyboardAssignment(); break; case "newGame": break; case "endGame": System.exit(0); break; } } private void showKeyboardAssignment() { JDialog assignmentDialog = new JDialog(); assignmentDialog.setTitle("Tastaturbelegung"); assignmentDialog.setSize(400, 400); assignmentDialog.setLocationRelativeTo(null); JLabel rules = new JLabel( "<html><center>linke Pfeiltaste: bewegt Schläger nach links <br>rechte Pfeiltaste: bewegt Schläger nach rechts.<center></html>"); assignmentDialog.add(rules); assignmentDialog.setVisible(true); } private void showRules() { JDialog rulesDialog = new JDialog(); rulesDialog.setTitle("Spielregeln"); rulesDialog.setSize(400, 400); rulesDialog.setLocationRelativeTo(null); JLabel rules = new JLabel( "<html><center>Der Ball muss mit dem Schläger so gelenkt werden, dass er Mauersteine trifft. So werden diese zum Verschwinden gebracht.Erst wenn alle Mauersteine getroffen wurden, ist das Spiel gewonnen.Wenn der Ball runterfällt, ist das Spiel verloren.<center></html>"); rulesDialog.add(rules); rulesDialog.setVisible(true); } public static void main(String[] args) { new Breakout(); } @Override public void paint(Graphics g) { super.paint(g); wall.setLines(4); wall.paintComponent(g); } }
Решение задачи: «Создание двухмерного массива Point»
textual
Листинг программы
private Point[][] position=new Point[4][6];
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д