Skip to content

Instantly share code, notes, and snippets.

@ChristophHaag
Created March 21, 2015 11:16
Show Gist options
  • Save ChristophHaag/7de6c10a236a40148b8f to your computer and use it in GitHub Desktop.
Save ChristophHaag/7de6c10a236a40148b8f to your computer and use it in GitHub Desktop.
Zeichne einen "Roboter" mit n einstellbaren Gelenken auf ein JPanel
import sun.awt.VerticalBagLayout;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
public class lines {
static lines l = new lines();
class Koordinaten {
int x;
int y;
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setKoordinate(int x, int y) {
this.x = x; this.y = y;
}
public Koordinaten(int x, int y) {
this.x = x;
this.y = y;
}
}
static Koordinaten calculateEnd(Koordinaten p1, int Länge, int Winkel) {
double rad = Winkel * Math.PI / 180.;
double dx = Math.cos(rad) * Länge;
double dy = Math.sin(rad) * Länge;
Koordinaten end = l.new Koordinaten((int) (p1.getX() + dx) , (int) (p1.getY() + dy));
return end;
}
class RoboterTeil {
int Länge;
int Winkel;
Color c;
public RoboterTeil(int Länge, int Winkel, Color c) {
this.Länge = Länge;
this.Winkel = Winkel;
this.c = c;
setWinkel(Winkel);
}
public Color getColor() {
return c;
}
public void setWinkel(int Winkel) {
this.Winkel = Winkel;
}
public void setLänge(int länge) {
this.Länge = länge;
}
public Koordinaten getEnde(Koordinaten Anfang, int WinkelVorher) {
Koordinaten p2 = calculateEnd(Anfang, Länge, Winkel + WinkelVorher);
return p2;
}
public int getWinkel() {
return Winkel;
}
}
class Roboter {
RoboterTeil[] rt;
Roboter(RoboterTeil[] Teile) {
this.rt = Teile;
}
public void setTeilLänge(int TeilIndex, int Länge) {
rt[TeilIndex].setLänge(Länge);
}
public void setTeilWinkel(int TeilIndex, int Winkel) {
rt[TeilIndex].setWinkel(Winkel);
}
public void drawRobot(Graphics g, Koordinaten Ursprung) {
Graphics2D g2d = (Graphics2D) g.create();
Koordinaten letztesEnde = Ursprung;
int letzterWinkel = 0;
for (RoboterTeil teil : rt) {
int x1 = letztesEnde.getX();
int y1 = letztesEnde.getY();
int x2 = teil.getEnde(letztesEnde, letzterWinkel).getX();
int y2 = teil.getEnde(letztesEnde, letzterWinkel).getY();
g2d.setColor(teil.getColor());
g2d.setStroke(new BasicStroke(4));
g2d.drawLine(x1, y1, x2, y2);
letztesEnde = teil.getEnde(letztesEnde, letzterWinkel);
letzterWinkel += teil.getWinkel();
}
}
}
class DrawPanel extends JPanel {
Roboter r;
DrawPanel(Roboter r) {
this.r = r;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Koordinaten Ursprung = new Koordinaten(getWidth()/2, getHeight()/2); // ändert sich je nach Fenstergröße
r.drawRobot(g, Ursprung);
}
}
public static void main(String[] args) {
RoboterTeil[] rt = {
l.new RoboterTeil(100, 0, Color.BLACK),
l.new RoboterTeil(200, 0, Color.RED),
l.new RoboterTeil(100, 0, Color.YELLOW),
};
final Roboter r = l.new Roboter(rt);
final DrawPanel panel = l.new DrawPanel(r);
panel.setPreferredSize(new Dimension(900, 700));
final JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LayoutManager layout = new VerticalBagLayout();
application.setLayout(layout);
application.add(panel);
for ( int i = 0; i < rt.length; i++) {
final JSpinner spinner = new JSpinner();
final int j = i; // Kein Kommentar
spinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
r.setTeilWinkel(j, Integer.valueOf((Integer) spinner.getValue()));
panel.repaint();
}
});
application.add(spinner);
}
application.setSize(1200, 900);
application.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment