Anfängerproblem mit repaint()

  • Anfängerproblem mit repaint()

    Hallo,
    wahrscheinlich schon des öfteren so ähnlich gestellt...
    Aber ich find den Fehler nicht. Irgendwie funktioniert repaint nicht so wie ich mir das vorstelle. Eigentlich dürfte immer nur die aktuelle Variante des Hauses gezeichnet werden.Zeichnet sich aber immer alles...

    Quellcode

    1. import java.awt.*;
    2. import javax.swing.*;
    3. import java.awt.geom.*;
    4. import java.awt.event.*;
    5. import java.awt.Component.*;
    6. import java.applet.*;
    7. public class Haus extends JApplet implements Runnable{
    8. int x =20;
    9. int y =20;
    10. Polygon polygon;
    11. public void init(){
    12. setBackground(Color.BLUE);
    13. }
    14. public void start(){
    15. Thread th = new Thread (this);
    16. th.start();
    17. }
    18. public void run(){
    19. Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    20. while (x<200){
    21. x +=20;
    22. repaint();
    23. try{
    24. Thread.sleep(20);
    25. }catch (InterruptedException ex){}
    26. Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    27. }
    28. }
    29. public void paint(Graphics gc){
    30. Graphics2D g = (Graphics2D) gc;
    31. int a[] = {x,x,x+30,x+30,x+15};
    32. int b[] = {y,y+30,y+30,y,y-15};
    33. polygon = new Polygon (a,b,5);
    34. g.setColor(Color.red);
    35. g.fill(polygon);
    36. }
    37. }
    Alles anzeigen
  • So, hallo nochmal...

    ich hab mein programm jetzt ein wenig weitergeschrieben. Allerdings hab ich jetzt schon wieder das Problem, wie oben.


    Quellcode

    1. import java.awt.*;
    2. import java.applet.*;
    3. import java.awt.event.*;
    4. import java.util.*;
    5. import javax.swing.*;
    6. import java.awt.geom.*;
    7. import java.lang.Object.*;
    8. class MHarry extends Canvas{
    9. MHarry(){
    10. setBackground(new Color (70,60,60));
    11. }
    12. public void paint (Graphics g){
    13. super.paint(g);
    14. Graphics2D gc = (Graphics2D) g;
    15. // einige geometrische Figren als Hintergrund
    16. g.dispose();
    17. }
    18. }
    19. /** Eigentliches Applet*/
    20. public class MHaus extends Applet{
    21. MCanvas malflaeche;
    22. int druck;
    23. public MHaus(){
    24. setLayout(new BorderLayout());
    25. malflaeche = new MCanvas();
    26. /** Äußere Graphig*/
    27. JPanel panel = new JPanel();
    28. // Buttons mit Action im Panel
    29. }
    30. public void start(){
    31. MAktion neu = new MAktion(malflaeche,druck);
    32. neu.start();
    33. }
    34. /**Aktionen anlegen*/
    35. class Lauscher implements ActionListener{
    36. public void actionPerformed(ActionEvent e){
    37. String label;
    38. label = e.getActionCommand();
    39. //Reaktion der Buttons
    40. malflaeche.repaint();
    41. }/**void ActionListener*/
    42. }/** ActionListener*/
    43. }
    44. class MAktion extends Thread implements Runnable{
    45. Canvas anzeige;
    46. int xpos;
    47. int[] v;
    48. int[] w;
    49. Polygon polygon;
    50. int s = 20;
    51. MAktion (Canvas c, int h){
    52. anzeige = c;
    53. xpos = h;
    54. }
    55. public void run(){
    56. while (isInterrupted() == false){
    57. bewegen();
    58. try{
    59. sleep(s);
    60. }catch(InterruptedException e){
    61. return;
    62. }
    63. }
    64. }
    65. void anzeigen(){
    66. Graphics g = anzeige.getGraphics();
    67. /** Buttonreaktion*/
    68. g.translate(150,150);
    69. g.setColor(Color.red);
    70. polygon = new Polygon(v,w,5);
    71. g.fillPolygon(polygon);
    72. g.dispose();
    73. }
    74. void bewegen(){
    75. Graphics g = anzeige.getGraphics();
    76. // Bewegung siehe Haus
    77. anzeigen();
    78. }
    79. }
    Alles anzeigen

    Lg Harryseule