IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

avec Java Discussion :

The method add


Sujet :

avec Java

  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    684
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 684
    Points : 147
    Points
    147
    Par défaut The method add
    bonjour
    j'ai essayé ce programme en compilant j'ai erreur
    suivantes veuillez m'aider s'il vous plaît

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method add(String, Component) in the type Container is not applicable for the arguments (String, MosaicCanvas)

    at Textprg.Mosaic.open(Mosaic.java:30)
    at Textprg.Brighten.main(Brighten.java:13)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
     
    package Textprg;
    public class Brighten 
    {
    	final static int ROWS=80;
    	final static int COLUMNS=80;
    	final static int SQUARE_SIZE=5;
     
    	static int currentRow;
    	static int currentColumn;
     
    	public static void main(String[]args)
    	{
    		Mosaic.open(ROWS,COLUMNS,SQUARE_SIZE,SQUARE_SIZE);
    		currentRow=ROWS/2;
    		currentColumn=COLUMNS/2;
    		while(Mosaic.isOpen())
    		{
    			brightenSquare(currentRow,currentColumn);
    			randomMove();
    			Mosaic.delay(20);
     
    		}
    	}
    static void brightenSquare(int row,int col)
    {
    	int r=Mosaic.getRed(row,col);
    	r+=25;
    	Mosaic.setColor(row,col,r,0,0);
    }
    static void randomMove()
    {
    	int directionNum;
    	directionNum=(int)(4*Math.random());
    	switch(directionNum)
    	{
    	case 0:
    		currentRow--;
    		if (currentRow<0)
    			currentRow=ROWS-1;
    		break;
    	case 1:
    		currentColumn++;
    		if (currentColumn>=COLUMNS)
    			currentColumn=0;
    		break;
    	case 2:
    		currentRow++;
    		if (currentRow>=ROWS)
    			currentRow=0;
    		break;
    	case 3:
    		currentColumn--;
    		if (currentColumn<0)
    			currentColumn=COLUMNS-1;
    		break;
    	}
    }
    }
     
     
    package Textprg;
    import java.awt.*;
    import java.awt.event.*;
    public class Mosaic 
    {
    	   private static Frame window;         // A mosaic window, null if no window is open.
    	   private static MosaicCanvas canvas;  // A component that actually manages and displays the rectangles
     
    	   public static void open() {
    	         // Open a mosaic window with a 20-by-20 grid of squares, where each
    	         // square is 15 pixels on a side.
    	      open(20,20,15,15);
    	   }
     
    	   public static void open(int rows, int columns) {
    	         // Open a mosaic window with the specified numbers or rows and columns
    	         // of squares, where each square is 15 pixels on a side.
    	      open(rows,columns,15,15);
    	   }
     
    	   synchronized public static void open(int rows, int columns, int blockWidth, int blockHeight) {
    	         // Open a window that shows a mosaic with the specified number of rows and
    	         // columns of rectangles.  blockWidth and blockHeight specify the
    	         // desired width and height of rectangles in the grid.  If a mosaic window
    	         // is already open, it will be closed before the new window is open.
    	      if (window != null)
    	         window.dispose();
    	      canvas = new MosaicCanvas(rows,columns,blockWidth,blockHeight);
    	      window = new Frame("Mosaic Window");
    	      window.add("Center",canvas);
    	      window.addWindowListener(
    	            new WindowAdapter() {  // close the window when the user clicks its close box
    	               public void windowClosing(WindowEvent evt) {
    	                  close();
    	               }
    	            });
    	      window.pack();
    	      window.show();
    	   }
     
    	   synchronized public static void close() {
    	          // If there is a mosiac window, close it.
    	      if (window != null) {
    	         window.dispose();
    	         window = null;
    	         canvas = null;
    	      }
    	   }
     
    	   synchronized public static boolean isOpen() {
    	          // This method returns a boolean value that can be used to
    	          // test whether the window is still open.
    	      return (window != null);
    	   }
     
    	   public static void delay(int milliseconds) {
    	         // Calling this routine causes a delay of the specified number
    	         // of milliseconds in the program that calls the routine.  It is
    	         // provided here as a convenience.
    	      if (milliseconds > 0) {
    	        try { Thread.sleep(milliseconds); }
    	        catch (InterruptedException e) { }
    	      }
    	   }
     
    	   public static Color getColor(int row, int col) {
    	         // Returns the object of type Color that represents the color
    	         // of the grid in the specified row and column.
    	      if (canvas == null)
    	         return Color.black;
    	      return canvas.getColor(row, col);
    	   }
     
    	   public static int getRed(int row, int col) {
    	         // Returns the red component, in the range 0 to 255, of the
    	         // rectangle in the specified row and column.
    	      if (canvas == null)
    	         return 0;
    	      return canvas.getRed(row, col);
    	   }
     
    	   public static int getGreen(int row, int col) {
    	         // Returns the green component, in the range 0 to 255, of the
    	         // rectangle in the specified row and column.
    	      if (canvas == null)
    	         return 0;
    	      return canvas.getGreen(row, col);
    	   }
     
    	   public static int getBlue(int row, int col) {
    	         // Returns the blue component, in the range 0 to 255, of the
    	         // rectangle in the specified row and column.
    	      if (canvas == null)
    	         return 0;
    	      return canvas.getBlue(row, col);
    	   }
     
    	   public static void setColor(int row, int col, Color c) {
    	          // Set the rectangle in the specified row and column to have
    	          // the specified color.
    	      if (canvas == null)
    	         return;
    	      canvas.setColor(row,col,c);
    	   }
     
    	   public static void setColor(int row, int col, int red, int green, int blue) {
    	          // Set the rectangle in the specified row and column to have
    	          // the color with the specifed red, green, and blue components.
    	      if (canvas == null)
    	         return;
    	      canvas.setColor(row,col,red,green,blue);
    	   }
     
    	   public static void fill(Color c) {
    	          // Set all the rectangels in the grid to the color c.
    	      if (canvas == null)
    	         return;
    	      canvas.fill(c);
    	   }
     
    	   public static void fill(int red, int green, int blue) {
    	          // Set all the rectangles in the grid to the color with
    	          // the specified red, green, and blue components.
    	      if (canvas == null)
    	         return;
    	      canvas.fill(red,green,blue);
    	   }
     
    	   public static void fillRandomly() {
    	          // Sets each rectangle in the grid to a different randomly
    	          // chosen color.
    	      if (canvas == null)
    	         return;
    	      canvas.fillRandomly();
    	   }
     
    	}  // end of class Mosaic

  2. #2
    Membre averti
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    342
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 342
    Points : 419
    Points
    419
    Par défaut
    Bonjour je suppose que ton erreur intervien sur cette ligne

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    window.add("Center",canvas);
    je me demande donc si la class "MosaicCanvas" implement bien "Component" ?

  3. #3
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    684
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 684
    Points : 147
    Points
    147
    Par défaut
    bonjour
    implement bien "Component" ?
    explique moi s'il te plaît

  4. #4
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    on ne peux rajouter que des classes qui étendent java.awt.Component à une Frame. Si ta classe n'étends pas directement ou indirectement Component, tu ne peux pas l'ajouter à une Frame.

  5. #5
    Membre expérimenté Avatar de Ivelios
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juillet 2008
    Messages
    1 031
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 031
    Points : 1 540
    Points
    1 540
    Par défaut
    Grillé :

    Bref, pour exemple MosaicCanvas doit Extends Panel ou Button ou TextField ou ...
    Il était une fois [...] Et ils vécurent heureux et eurent beaucoup d'enfants!

  6. #6
    Membre à l'essai
    Inscrit en
    Juillet 2010
    Messages
    16
    Détails du profil
    Informations forums :
    Inscription : Juillet 2010
    Messages : 16
    Points : 19
    Points
    19
    Par défaut
    Bonjour,

    Le composant MosaicCanvas doit hériter de java.awt.Component, mais cela doit être le cas compte tenu du nom. Par contre, il est possible qu'il y ait une méthode statique associée pour obtenir une instance et que new MosaicCanvas() ne soit pas la bonne manière pour l'obtenir.

    Sinon ce code paraît obsolète :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    window.add("Center",canvas);
    Il y a un BorderLayout par défaut et cela centre également, normallement, tu dois pouvoir écrire :

    Sinon

    window.add( canvas, BorderLayour.CENTER ) est plus récent.

    Cordialement

  7. #7
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    684
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 684
    Points : 147
    Points
    147
    Par défaut
    j'ai corrigé comme tu m'a dit

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    window.add( canvas, BorderLayout.CENTER )
    j'ai toujours erreur message:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method add(String, Component) in the type Container is not applicable for the arguments (MosaicCanvas, String)

    at Textprg.Mosaic.open(Mosaic.java:30)
    at Textprg.Brighten.main(Brighten.java:13)

    j'ai ajouté la class MosaicCanvas
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
     
    package Textprg;
    import java.awt.*;
    public class MosaicCanvas 
    {
    	   protected int rows, columns;
    	   protected int blockWidth, blockHeight;
     
    	   protected Color defaultColor = Color.black;
     
    	   private Color[][] grid;
    	   private boolean blemished = false;
     
    	   public MosaicCanvas(int rows, int columns) {
    	      this(rows,columns,0,0);
    	   }
     
    	   public MosaicCanvas(int rows, int columns, int blockWidth, int blockHeight) {
    	      this.rows = rows;
    	      this.columns = columns;
    	      this.blockWidth = blockWidth;
    	      this.blockHeight = blockHeight;
    	      grid = new Color[rows][columns];
    	      for (int i = 0; i < rows; i++)
    	         for (int j = 0; j < columns; j++)
    	            grid[i][j] = defaultColor;
    	      setBackground(defaultColor);
    	   }
     
    	   public Dimension preferredSize() {
    	      if (blockWidth <= 0)
    	         blockWidth = 10;
    	      if (blockHeight <= 0)
    	         blockHeight = 10;
    	      return new Dimension(columns*blockWidth, rows*blockHeight);
    	         // fixed July 24, 1998: order of parameters was reversed.
    	   }
     
    	   public synchronized void paint(Graphics g) {
    	      if (!blemished) {
    	         g.setColor(grid[0][0]);
    	         g.fillRect(0,0,size().width,size().height);
    	      }
    	      else {
    	         double rowHeight = (double)size().height / rows;
    	         double colWidth = (double)size().width / columns;
    	         for (int i = 0; i < rows; i++) {
    	            int y = (int)Math.round(rowHeight*i);
    	            int h = (int)Math.round(rowHeight*(i+1)) - y;
    	            for (int j = 0; j < columns; j++) {
    	               int x = (int)Math.round(colWidth*j);
    	               int w = (int)Math.round(colWidth*(j+1)) - x;
    	               g.setColor(grid[i][j]);
    	               g.fillRect(x,y,w,h);
    	            }
    	         }
     
    	      }
    	   }
     
    	   public synchronized void drawSquare(int row, int col) {
    	      double rowHeight = (double)size().height / rows;
    	      double colWidth = (double)size().width / columns;
    	      int y = (int)Math.round(rowHeight*row);
    	      int h = (int)Math.round(rowHeight*(row+1)) - y;
    	      int x = (int)Math.round(colWidth*col);
    	      int w = (int)Math.round(colWidth*(col+1)) - x;
    	      Graphics g = getGraphics();
    	      g.setColor(grid[row][col]);
    	      g.fillRect(x,y,w,h);
    	   }
     
    	   public void update(Graphics g) {
    	      paint(g);
    	   }
     
    	   public Color getColor(int row, int col) {
    	      if (row >=0 && row < rows && col >= 0 && col < columns)
    	         return grid[row][col];
    	      else
    	         return defaultColor;
    	   }
     
    	   public int getRed(int row, int col) {
    	      return getColor(row,col).getRed();
    	   }
     
    	   public int getGreen(int row, int col) {
    	      return getColor(row,col).getGreen();
    	   }
     
    	   public int getBlue(int row, int col) {
    	      return getColor(row,col).getBlue();
    	   }
     
    	   public void setColor(int row, int col, Color c) {
    	      if (row >=0 && row < rows && col >= 0 && col < columns && c != null) {
    	         grid[row][col] = c;
    	         blemished = true;
    	         drawSquare(row,col);
    	      }
    	   }
     
    	   public void setColor(int row, int col, int red, int green, int blue) {
    	      if (row >=0 && row < rows && col >= 0 && col < columns) {
    	         red = (red < 0)? 0 : ( (red > 255)? 255 : red);
    	         green = (green < 0)? 0 : ( (green > 255)? 255 : green);
    	         blue = (blue < 0)? 0 : ( (blue > 255)? 255 : blue);
    	         grid[row][col] = new Color(red,green,blue);
    	         drawSquare(row,col);
    	         blemished = true;
    	      }
    	   }
     
    	   public void fill(Color c) {
    	      if (c == null)
    	         return;
    	      for (int i = 0; i < rows; i++)
    	         for (int j = 0; j < columns; j++)
    	            grid[i][j] = c;
    	      blemished = false;
    	      repaint();      
    	   }
     
    	   public void fill(int red, int green, int blue) {
    	      red = (red < 0)? 0 : ( (red > 255)? 255 : red);
    	      green = (green < 0)? 0 : ( (green > 255)? 255 : green);
    	      blue = (blue < 0)? 0 : ( (blue > 255)? 255 : blue);
    	      fill(new Color(red,green,blue));
    	   }
     
    	   public void fillRandomly() {
    	      for (int i = 0; i < rows; i++)
    	         for (int j = 0; j < columns; j++) {
    	            int r = (int)(256*Math.random());
    	            int g = (int)(256*Math.random());
    	            int b = (int)(256*Math.random());
    	            grid[i][j] = new Color(r,g,b);
    	      }
    	      blemished = true;
    	      repaint();
    	   }
     
    	   public void delay(int milliseconds) {
    	      if (milliseconds > 0) {
    	         try { Thread.sleep(milliseconds); }
    	         catch (InterruptedException e) { }
    	      }
    	   }
     
    	}

  8. #8
    Membre à l'essai
    Inscrit en
    Juillet 2010
    Messages
    16
    Détails du profil
    Informations forums :
    Inscription : Juillet 2010
    Messages : 16
    Points : 19
    Points
    19
    Par défaut
    Ta classe MosaicCanvas est incorrecte, elle devrait commencer par :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    package Textprg;
    import java.awt.*;
    public class MosaicCanvas extends Component {
    
    ...
    
    }
    ou bien

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    package Textprg;
    import java.awt.*;
    public class MosaicCanvas extends Canvas {
    
    ...
    
    }
    Autre détail, la convention de nommage des packages est minuscule, il vaudrait mieux avoir un package "textprg" plutôt que "Textprg". C'est juste une convention, cela ne change rien à la compil et à l'exécution.

  9. #9
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    684
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 684
    Points : 147
    Points
    147
    Par défaut
    j'ai essayé ce solution,mon programme marche bien,merci beaucoup
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    package Textprg;
    import java.awt.*;
    public class MosaicCanvas extends Canvas {
     
    ...
     
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Methode add d'ArrayList
    Par davdoo91 dans le forum Collection et Stream
    Réponses: 4
    Dernier message: 28/02/2009, 16h50
  2. Réponses: 15
    Dernier message: 18/02/2009, 18h53
  3. The method clone() from the type Object is not visible
    Par satchmoo dans le forum Débuter avec Java
    Réponses: 3
    Dernier message: 15/01/2009, 19h16
  4. [A-03] Methode Add et Item d'une collection
    Par elfiestador dans le forum VBA Access
    Réponses: 4
    Dernier message: 09/12/2008, 10h18
  5. Réponses: 1
    Dernier message: 19/11/2008, 17h47

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo