Gå til innhold

Anbefalte innlegg

Hei, driver for tiden å tukler litt med java, skrev denne her idag, men har noen små problemer, tror enten det er en feil i repaint, eller en feil i inkrementeringen, noen som har en løsning?

dette er classen jeg har problemer med:

 

class HelloComponentX extends JComponent {

int theY;
int theX;
String theMessage;

public HelloComponentX( String message ) {
	theMessage = message;

}

// check the arrow keys and adjust vars
public void keyPressed(KeyEvent e) {		
//if up arrow is pressed
    if (e.getKeyCode() == KeyEvent.VK_UP ) {
   theY = theY - 1;

    }
//if down arrow is pressed
    if (e.getKeyCode() == KeyEvent.VK_DOWN) {
   theY = theY + 1;		
    }	
//if left arrow is pressed
    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
   theX = theX - 1; 	
    }
 //if right arrow is pressed
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
    theX = theX + 1;	
    }

}


//drawing
public void paintComponent( Graphics g ) {


   	int X = 200 + theX;
   	int Y = 200 + theY;


	g.drawString("test", X, Y);
	repaint();

}

}

Lenke til kommentar
Videoannonse
Annonse

Jeg er definitivt ingen GUI-programmerer, men dette virker hos meg:

 

import java.awt.event.KeyEvent;

class HelloComponentX extends javax.swing.JComponent implements java.awt.event.KeyListener {

  int theY;
  int theX;
  int i;
  String theMessage;

  public HelloComponentX(String message) {
     theMessage = message;
  }

  // check the arrow keys and adjust vars
  @Override
  public void keyPressed(KeyEvent e) {
     int x = 0, y = 0;
     if(e.getKeyCode() == KeyEvent.VK_UP) y -= 1;
     if(e.getKeyCode() == KeyEvent.VK_DOWN) y += 1;
     if(e.getKeyCode() == KeyEvent.VK_LEFT) x -= 1;
     if(e.getKeyCode() == KeyEvent.VK_RIGHT) x += 1;
     if(x != 0 || y != 0) {
        theY += y;
        theX += x;
        repaint();
     }
  }

  @Override
  public void keyTyped(KeyEvent e) {}

  @Override
  public void keyReleased(KeyEvent e) {}

  //drawing
  public void paintComponent(java.awt.Graphics g) {
     int X = 200 + theX;
     int Y = 200 + theY;

     g.drawString("test " + i++, X, Y);
  }

  public static void main(String[] args) {
     javax.swing.JFrame me = new javax.swing.JFrame("En liten test");
     HelloComponentX hcx = new HelloComponentX("test");
     me.add(hcx);
     me.addKeyListener(hcx);
     me.show();
  }

}

Lenke til kommentar

Takker, men her er hele koden satt sammen i en stor klasse, fungerer ikke helt og klarer ikke å finne ut hvorfor, driver å lærer java så dette er vell egentlig over mitt kunnskapsnivå innenfor temaet! :p

meningen er at man skal kunne styre teksten rundt med piltastene!

 

import java.awt.*;
import javax.swing.*;
import java.awt.event.KeyEvent;

public class HelloJavaX {




public static void main(String[] args) {



	JFrame frame = new JFrame( "Hello JavaX" );
	frame.getContentPane().add( new HelloComponentX( "Hello" ) );
	frame.setSize( 450, 450);
	frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );				
       frame.setVisible( true );


}

}



class HelloComponentX extends javax.swing.JComponent implements java.awt.event.KeyListener{

   int theY;
   int theX;
   int i;
   String theMessage;

   public HelloComponentX(String message) {
      theMessage = message;
   }

   // check the arrow keys and adjust vars
   @Override
   public void keyPressed(KeyEvent e) {
      int x = 0, y = 0;
      if(e.getKeyCode() == KeyEvent.VK_UP) y -= 1;
      if(e.getKeyCode() == KeyEvent.VK_DOWN) y += 1;
      if(e.getKeyCode() == KeyEvent.VK_LEFT) x -= 1;
      if(e.getKeyCode() == KeyEvent.VK_RIGHT) x += 1;
      if(x != 0 || y != 0) {
         theY += y;
         theX += x;
         repaint();
      }
   }

   @Override
   public void keyTyped(KeyEvent e) {}

   @Override
   public void keyReleased(KeyEvent e) {}

   //drawing
   public void paintComponent(java.awt.Graphics g) {
      int X = 200 + theX;
      int Y = 200 + theY;

      g.drawString("test " + i++, X, Y);
   }

   public static void main(String[] args) {
      javax.swing.JFrame me = new javax.swing.JFrame("En liten test");
      HelloComponentX hcx = new HelloComponentX("test");
      me.add(hcx);
      me.addKeyListener(hcx);
      me.show();
   }

}

Lenke til kommentar

Oi, ser med en gang hva jeg gjorde feil der, var litt vanskelig å se det på editoren :p

Fungerer nå, tusen takk!

import javax.swing.*;
import java.awt.event.KeyEvent;

public class HelloJavaX {




public static void main(String[] args) {



	JFrame frame = new JFrame( "Hello JavaX" );		
	HelloComponentX hcx = new HelloComponentX("test");
	frame.setSize( 450, 450);
	frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );				
       frame.setVisible( true );
       frame.add(hcx);
    frame.addKeyListener(hcx);



}

}



class HelloComponentX extends javax.swing.JComponent implements java.awt.event.KeyListener{

   int theY;
   int theX;
   int i;
   String theMessage;

   public HelloComponentX(String message) {
      theMessage = message;
   }

   // check the arrow keys and adjust vars
   @Override
   public void keyPressed(KeyEvent e) {
      int x = 0, y = 0;
      if(e.getKeyCode() == KeyEvent.VK_UP) {y -= 1;}
      if(e.getKeyCode() == KeyEvent.VK_DOWN) {y += 1;}
      if(e.getKeyCode() == KeyEvent.VK_LEFT) {x -= 1;}
      if(e.getKeyCode() == KeyEvent.VK_RIGHT){ x += 1;}
      if(x != 0 || y != 0) {
         theY += y;
         theX += x;
         repaint();
      }
   }

   @Override
   public void keyTyped(KeyEvent e) {}

   @Override
   public void keyReleased(KeyEvent e) {}

   //drawing
   public void paintComponent(java.awt.Graphics g) {
      int X = 200 + theX;
      int Y = 200 + theY;

      g.drawString("test " + i++, X, Y);
   }



}

Endret av Leif.ross
Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...