Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import java.awt.event.*; import javax.swing.*; import java.awt.*; public class Malen extends JFrame implements ActionListener { private Malfläche Fläche1; private JLabel lXPos; private JLabel lYPos; private JLabel lHoehe; private JLabel lBreite; private JTextField tZahl1; private JTextField tZahl2; private JTextField tZahl3; private JTextField tZahl4; private JButton bAdd; public Malen() { super("Malprogramm 1.0"); setSize(1000,800); setLayout(null); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Fläche1 = new Malfläche(); Fläche1.setBounds(10,10,500,500); Fläche1.setBackground(Color.WHITE); lXPos = new JLabel("X-Pos:"); lXPos.setBounds(530,10,100,30); lYPos = new JLabel("Y-Pos:"); lYPos.setBounds(530,30,100,30); lHoehe = new JLabel("Hoehe:"); lHoehe.setBounds(530,50,100,30); lBreite = new JLabel("Breite:"); lBreite.setBounds(530,70,100,30); tZahl1 = new JTextField(20); tZahl1.setBounds(570, 15,30,20); tZahl2 = new JTextField(20); tZahl2.setBounds(570, 35,30,20); tZahl3 = new JTextField(20); tZahl3.setBounds(570, 55,30,20); tZahl4 = new JTextField(20); tZahl4.setBounds(570, 75,30,20); bAdd = new JButton("adden"); bAdd.addActionListener(this); bAdd.setBounds(530,100,70,30); add(Fläche1); add(lXPos); add(lYPos); add(lHoehe); add(lBreite); add(tZahl1); add(tZahl2); add(tZahl3); add(tZahl4); add(bAdd); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == bAdd) { // Wie kann ich nun das Viereck zeichnen lassen ? } } } |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.awt.event.*; import javax.swing.*; import java.awt.*; public class Malfläche extends Canvas { int nXPos; int nYPos; int nBreite; int nHoehe; public Malfläche() { } public void paint(Graphics g) { g.drawRect(nXPos, nYPos, nHoehe, nBreite); } } |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
import java.awt.event.*; import javax.swing.*; import java.awt.*; public class Malen extends JFrame implements ActionListener { private Malfläche Fläche1; private JLabel lXPos; private JLabel lYPos; private JLabel lHoehe; private JLabel lBreite; private JTextField tZahl1; private JTextField tZahl2; private JTextField tZahl3; private JTextField tZahl4; private JButton bAdd; boolean AddClicked; private Thread malen; public Malen() { super("Malprogramm 1.0"); setSize(1000,800); setLayout(null); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); AddClicked = false; Fläche1 = new Malfläche(); Fläche1.setBounds(10,10,500,500); Fläche1.setBackground(Color.WHITE); lXPos = new JLabel("X-Pos:"); lXPos.setBounds(530,10,100,30); lYPos = new JLabel("Y-Pos:"); lYPos.setBounds(530,30,100,30); lHoehe = new JLabel("Hoehe:"); lHoehe.setBounds(530,50,100,30); lBreite = new JLabel("Breite:"); lBreite.setBounds(530,70,100,30); tZahl1 = new JTextField(20); tZahl1.setBounds(570, 15,30,20); tZahl2 = new JTextField(20); tZahl2.setBounds(570, 35,30,20); tZahl3 = new JTextField(20); tZahl3.setBounds(570, 55,30,20); tZahl4 = new JTextField(20); tZahl4.setBounds(570, 75,30,20); bAdd = new JButton("adden"); bAdd.addActionListener(this); bAdd.setBounds(530,100,70,30); add(Fläche1); add(lXPos); add(lYPos); add(lHoehe); add(lBreite); add(tZahl1); add(tZahl2); add(tZahl3); add(tZahl4); add(bAdd); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == bAdd) { AddClicked = true; remove(Fläche1); Fläche1 = new Malfläche(); Fläche1.setBounds(10,10,500,500); Fläche1.setBackground(Color.WHITE); add(Fläche1); } } class Malfläche extends Canvas { public Malfläche() { } public void paint(Graphics g) { if(AddClicked == true) { g.setColor(Color.blue); g.drawRect(Integer.parseInt(tZahl1.getText()), Integer.parseInt(tZahl2.getText()), Integer.parseInt(tZahl3.getText()), Integer.parseInt(tZahl4.getText())); AddClicked = false; } } } } |
Quellcode |
|
1 |
Shape a+1 = new Shape(10,10,10,10); |
Quellcode |
|
1 |
Vector<Shape> myVec = new Vector<Shape>(); |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import java.awt.event.*; import javax.swing.*; import java.awt.*; public class Malen extends JFrame implements ActionListener { private Malfläche Fläche1; private JLabel lXPos; private JLabel lYPos; private JLabel lHoehe; private JLabel lBreite; private JTextField tZahl1; private JTextField tZahl2; private JTextField tZahl3; private JTextField tZahl4; private JButton bAdd; public boolean AddClicked; public int[] Shape; public int i; public int AnzahlShapes; public Malen() { super("Malprogramm 1.0"); setSize(1000,800); setLayout(null); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); AddClicked = false; i = 0; AnzahlShapes = 0; Fläche1 = new Malfläche(); Fläche1.setBounds(10,10,500,500); Fläche1.setBackground(Color.WHITE); lXPos = new JLabel("X-Pos:"); lXPos.setBounds(530,10,100,30); lYPos = new JLabel("Y-Pos:"); lYPos.setBounds(530,30,100,30); lHoehe = new JLabel("Hoehe:"); lHoehe.setBounds(530,50,100,30); lBreite = new JLabel("Breite:"); lBreite.setBounds(530,70,100,30); tZahl1 = new JTextField(20); tZahl1.setBounds(570, 15,30,20); tZahl2 = new JTextField(20); tZahl2.setBounds(570, 35,30,20); tZahl3 = new JTextField(20); tZahl3.setBounds(570, 55,30,20); tZahl4 = new JTextField(20); tZahl4.setBounds(570, 75,30,20); bAdd = new JButton("adden"); bAdd.addActionListener(this); bAdd.setBounds(530,100,70,30); add(Fläche1); add(lXPos); add(lYPos); add(lHoehe); add(lBreite); add(tZahl1); add(tZahl2); add(tZahl3); add(tZahl4); add(bAdd); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == bAdd) { AddClicked = true; remove(Fläche1); Fläche1 = new Malfläche(); Fläche1.setBounds(10,10,500,500); Fläche1.setBackground(Color.WHITE); add(Fläche1); } } class Malfläche extends Canvas { public Malfläche() { } public void paint(Graphics g) { int zahl1; int zahl2; int zahl3; int zahl4; if(AddClicked == true) { Shape[i] = Integer.parseInt(tZahl1.getText()); i++; Shape[i] = Integer.parseInt(tZahl2.getText()); i++; Shape[i] = Integer.parseInt(tZahl3.getText()); i++; Shape[i] = Integer.parseInt(tZahl4.getText()); i++; AnzahlShapes++; AddClicked = false; } for(int j = 0; j<AnzahlShapes; j = j + 4) { g.setColor(Color.blue); zahl1 = Shape[j]; zahl2 = Shape[j+1]; zahl3 = Shape[j+2]; zahl4 = Shape[j+3]; g.drawRect(zahl1, zahl2, zahl3, zahl4); } } } } |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Malen$Malfläche.paint(Malen.java:127) at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248) at sun.awt.RepaintArea.paint(RepaintArea.java:224) at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:306) at java.awt.Component.dispatchEventImpl(Component.java:4577) at java.awt.Component.dispatchEvent(Component.java:4331) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) |
Quellcode |
|
1 |
Shape[i] = Integer.parseInt(tZahl1.getText()); |
Werbeanzeige