auf jedenna gut zum rumspielen sind se echt praktisch und spaßig
Quellcode |
|
1 2 3 4 5 6 7 8 9 |
Exception in thread "main" java.lang.NoClassDefFoundError: Hauptfenster Caused by: java.lang.ClassNotFoundException: Hauptfenster at java.net.URLClassLoader$1.run(URLClassLoader.java:217) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) at java.lang.ClassLoader.loadClass(ClassLoader.java:321) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) at java.lang.ClassLoader.loadClass(ClassLoader.java:266) Could not find the main class: Hauptfenster. Program will exit. |
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 |
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Hauptfenster extends JFrame { private Zeichenfläche zeichenfläche; private JPanel Formwahl; private JRadioButton RBStrecke; private JRadioButton RBRechteck; private JRadioButton RBEllipse; enum Formen { Strecke, Rechteck, Ellipse; } Formen Form; public Formen getForm() { return Form; } // Konstruktor public Hauptfenster() { super("Formen zeichnen mit Swing"); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); Form = Formen.Strecke; Elemente_erzeugen(); Funktionalitäten_hinzufügen(); pack(); setVisible(true); } private void Elemente_erzeugen() { zeichenfläche = new Zeichenfläche(this); Formwahl = new JPanel(); RBStrecke = new JRadioButton("Strecke", true); RBRechteck = new JRadioButton("Rechteck"); RBEllipse = new JRadioButton("Ellipse"); Formwahl.add(RBStrecke); Formwahl.add(RBRechteck); Formwahl.add(RBEllipse); ButtonGroup bg = new ButtonGroup(); bg.add(RBStrecke); bg.add(RBRechteck); bg.add(RBEllipse); setLayout(new BorderLayout()); add(zeichenfläche, BorderLayout.SOUTH); add(Formwahl, BorderLayout.CENTER); } private void Funktionalitäten_hinzufügen() { RBStrecke.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { Form = Formen.Strecke; } }); RBRechteck.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { Form = Formen.Rechteck; } }); RBEllipse.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { Form = Formen.Ellipse; } }); } public static void main(String[] args) { new Hauptfenster(); } } |
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 |
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import javax.swing.JPanel; public class Zeichenfläche extends JPanel { int p1x,p2x,p1y,p2y; boolean erster_punkt = false; LinkedList<Form> formenliste; public Zeichenfläche(final Hauptfenster parent) { setBackground(Color.white); setPreferredSize(new Dimension(800,600)); formenliste = new LinkedList<Form>(); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent me) { if(!erster_punkt) { p1x = me.getX(); p1y = me.getY(); System.out.println("Klick: " + p1x + " " + p1y); erster_punkt = true; } else { p2x = me.getX(); p2y = me.getY(); Hauptfenster.Formen form = parent.getForm(); switch(form) { case Strecke: Strecke strecke = new Strecke(p1x,p1y,p2x,p2y); formenliste.add(strecke); case Ellipse: Ellipse ellipse = new Ellipse(p1x,p1y,p2x,p2y); formenliste.add(ellipse); case Rechteck: Rechteck rechteck = new Rechteck(p1x,p1y,p2x,p2y); formenliste.add(rechteck); } erster_punkt = false; } } }); } @Override public void paintComponent(Graphics g) { for (Iterator<Form> it = formenliste.iterator();it.hasNext();) it.next().zeichne(g); } } |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.awt.Graphics; public abstract class Form { protected int p1_x; protected int p1_y; protected int p2_x; protected int p2_y; public Form(int x1,int y1,int x2,int y2) { p1_x = x1; p1_y = y1; p2_x = x2; p2_y = y2; } public abstract void zeichne(Graphics g); } |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.awt.Graphics; public class Ellipse extends Form { public Ellipse(int x1, int y1, int x2, int y2) { super(x1, y1, x2, y2); } @Override public void zeichne(Graphics g) { g.drawOval(p1_x, p1_y, p2_x-p1_x, p2_y-p1_y); } } |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.awt.Graphics; public class Rechteck extends Form { public Rechteck(int x1, int y1, int x2, int y2) { super(x1, y1, x2, y2); } @Override public void zeichne(Graphics g) { g.drawRect(p1_x,p1_y,p2_x-p1_x,p2_y-p1_y); } } |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.awt.Graphics; public class Strecke extends Form { public Strecke(int x1,int y1,int x2,int y2) { super(x1,y1,x2,y2); } @Override public void zeichne(Graphics g) { g.drawLine(p1_x, p1_y, p2_x, p2_y); } } |