hallo
Ich habe ein Programm geschrieben, dass ein Koordinatensystem anzeigt( Aktueller Stand siehe Bild).
Jetzt Möchte ich Programmieren, dass man in diesem System linien malen kann. Das Problem ist, dass der beginn der Linie immer bei 0,0 liegt.
Ich konnte das Problem darauf eingrenzen, dass die Variabele ready sich immer auf true stellt. Ich habe auch schon per steuerung f im Programm nach irgendwelchen zeilen gesucht die so nicht sein sollten und das ganze auf true stellen konnte aber nichts finden.
Hier die beiden klassen die sehr wahrscheinlich das Problem darstellen:
Schonmal Vielen dank für eure hilfe.
Ich habe ein Programm geschrieben, dass ein Koordinatensystem anzeigt( Aktueller Stand siehe Bild).
Jetzt Möchte ich Programmieren, dass man in diesem System linien malen kann. Das Problem ist, dass der beginn der Linie immer bei 0,0 liegt.
Ich konnte das Problem darauf eingrenzen, dass die Variabele ready sich immer auf true stellt. Ich habe auch schon per steuerung f im Programm nach irgendwelchen zeilen gesucht die so nicht sein sollten und das ganze auf true stellen konnte aber nichts finden.
Hier die beiden klassen die sehr wahrscheinlich das Problem darstellen:
Code:
public class Draw extends JLabel{
static boolean pressed = false;
static boolean paused = false;
static int x ,y;
static int pX,pY,ptX,ptY;
static boolean ready = false;
static int color;//1 = red; 2 = green; 3 = blue
int[] paintX = new int[200];
int[] paintY = new int[200];
int[] paintX2 = new int[200];
int[] paintY2 = new int[200];
int[] colors = new int[200];
static boolean found;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//background
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, 1000, 1000);
//field lines
g.setColor(Color.black);
for(int i = 0; i<9;i++) {
g.drawLine(200 + i * 200,0,200+i * 200,1000);
g.drawLine(0, 200 + i * 200, 1000, 200 + i * 200);
}
//Say cordi
if(pressed && paused == false) {
if(y < 59)
g.drawString(""+x +", "+y, x-30, y+25);
else
g.drawString(""+x +", "+y, x-30, y-50);
}
//paint
if(ready == false) {
if(color == 1) {
g.setColor(Color.red);
}
if(color == 2) {
g.setColor(Color.green);
}
if(color == 3) {
g.setColor(Color.blue);
}
g.drawLine(pX,pY,ptX,ptY);
}
for(int i = 0;i<200;i++) {
if(colors[i] == 0) {
if(found == false && ready == false) {
paintX[i] = pX;
paintY[i] = pY;
paintX2[i] = ptX;
paintY2[i] = ptY;
colors[i] = color;
}
}
else {
if(colors[i] == 1) {
g.setColor(Color.red);
}
if(colors[i] == 2) {
g.setColor(Color.green);
}
if(colors[i] == 3) {
g.setColor(Color.blue);
}
g.drawLine(paintX[i],paintY[i],paintX2[i],paintY2[i]);
}
}
//paused
if(paused) {
g.setColor(Color.gray);
g.fillRect(0,0,1000,1000);
g.setColor(Color.black);
Font arial = new Font("Arial",Font.BOLD,50);
g.setFont(arial);
g.drawString("press any Button to continue", 130, 400);
}
repaint();
}
}
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
Draw.paused = true;
new Gui();
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
if(Draw.ready = false) {
Draw.pX = e.getX();
Draw.pY = e.getY();
System.out.println(""+ e.getX()+", " + e.getY());
Draw.ready = true;
}
else {
System.out.println("second"+ e.getX()+", " + e.getY());
Draw.ptX = e.getX();
Draw.ptY = e.getY();
Draw.ready = false;
}
}
public void mouseEntered(MouseEvent e) {
Draw.pressed = true;
}
public void mouseExited(MouseEvent e) {
Draw.pressed = false;
}
}
Schonmal Vielen dank für eure hilfe.