JList

Fermé
isimisienne Messages postés 1 Date d'inscription mercredi 15 avril 2015 Statut Membre Dernière intervention 15 avril 2015 - Modifié par Whismeril le 15/04/2015 à 15:29
 Utilisateur anonyme - 15 avril 2015 à 15:30
bonjour
je veux ajouter un JList dans la partie droite
voici mon code
très urgent

package projet;


import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Panel;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListModel;
import javax.swing.border.Border;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class List implements  ListSelectionListener{
    private JButton enregistreBtn,reduireBtn,defaireBtn,deplacerBtn,clearBtn,agrandirBtn,simulerBtn;
    Drawing drawing;
    ActionListener actionListener=new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==clearBtn){
                drawing.efface();
            }
           
           else  if(e.getSource()==defaireBtn)
          {drawing.defaire();}
        }
    };
     public static void main(String args[]) {
   
    
       new List().Showe();
   
     }
     public void Showe(){
   
    JFrame frame=new JFrame("Commande");
    frame.setLayout(new BorderLayout());
    Container content=frame.getContentPane();
    content.setLayout(new BorderLayout());
     drawing=new Drawing();
    content.add(drawing,BorderLayout.CENTER);
    JPanel Controls=new JPanel();
     enregistreBtn=new JButton("enregistrer");
     enregistreBtn.addActionListener(actionListener);
      agrandirBtn=new JButton("agrandir");
     agrandirBtn.addActionListener(actionListener);
     reduireBtn=new JButton("reduire");
     reduireBtn.addActionListener(actionListener);
     defaireBtn=new JButton("defaire");
     defaireBtn.addActionListener(actionListener);
     clearBtn=new JButton("supprimer");
     clearBtn.addActionListener(actionListener);
     deplacerBtn=new JButton("deplacer");
     deplacerBtn.addActionListener(actionListener);
     simulerBtn=new JButton("simuler");
     simulerBtn.addActionListener(actionListener);
    Controls.add(enregistreBtn);
     Controls.add(agrandirBtn);
      Controls.add(defaireBtn);
       Controls.add(deplacerBtn);
        Controls.add(simulerBtn);
         Controls.add(reduireBtn);
         Controls.add(clearBtn);
enregistreBtn=new JButton("enregistrer");
     enregistreBtn.addActionListener(actionListener);
  
  
         content.add(Controls,BorderLayout.SOUTH);
         frame.setSize(600,600);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);
        
    }

    private void setLayout(GridBagLayout gridBagLayout) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private Object getContentPane() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
  private class Drawing extends JComponent {
   Vector<Shape> shapes = new Vector<Shape>();
    Point startDrag, endDrag;
    Graphics2D g2;
    public Drawing() {
      this.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
          startDrag = new Point(e.getX(), e.getY());
          endDrag = startDrag;
          repaint();
        }
        public void mouseReleased(MouseEvent e) {
          Shape r = makeRectangle(startDrag.x, startDrag.y, e.getX(), e.getY());
          shapes.add(r);
          startDrag = null;
          endDrag = null;
          repaint();
        }
        public void mouseClicked(MouseEvent e){
            
        }
      });
     this.addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
          endDrag = new Point(e.getX(), e.getY());
           
          repaint();
          
        }
      });
    } 
     private void paintBackground(Graphics2D g2){
      g2.setPaint(Color.LIGHT_GRAY);
      for (int i = 0; i < getSize().width; i += 10) {
        Shape line = new Line2D.Float(i, 0, i, getSize().height);
        g2.draw(line);
      }
      for (int i = 0; i < getSize().height; i += 10) {
        Shape line = new Line2D.Float(0, i, getSize().width, i);
        g2.draw(line);
      }
 
    }
   public void paint(Graphics g) {
      Graphics2D g2 = (Graphics2D) g;
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
       paintBackground(g2);
      Color[] colors = { Color.YELLOW, Color.MAGENTA, Color.CYAN , Color.RED, Color.BLUE, Color.PINK};
 
      int colorIndex = 0;
      g2.setStroke(new BasicStroke(2));
      g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f));
    for (Shape s : shapes) {
        g2.setPaint(Color.BLACK);
        g2.draw(s);
      g2.setPaint(colors[(colorIndex++) % 6]);
        g2.fill(s);}
      
      if (startDrag != null && endDrag != null) {
        g2.setPaint(Color.LIGHT_GRAY);
        Shape r = makeRectangle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
        g2.draw(r);
       
      }
     
    }
  
  private Rectangle2D.Float makeRectangle(int x1, int y1, int x2, int y2) {
      return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
    }
  public void enregistrer(){
      
  }
  public void simuler(){
     
  }
  public void agrandir(int m){
      m=10;
      shapes.setSize(shapes.size()+m);   
  }
  public void reduire(){
      
  }
  public void efface(){
         shapes.clear();
         repaint();   
          }
           public void defaire(){
       if(!shapes.isEmpty())
       {shapes.remove(shapes.lastElement());
      
       } 
     repaint();
          }
           public void magenta(){
          g2.setPaint(Color.magenta);
          }
           public void green(){
          g2.setPaint(Color.green);
          }
           public void black(){
          g2.setPaint(Color.black);
          }
           
  }}


EDIT: Ajout de la coloration syntaxique.

1 réponse

Utilisateur anonyme
15 avril 2015 à 15:30
Bonjour merci de garder à l'esprit que pour les bénévoles qui animent ce forum, la seule urgence est celle qu'ils se donnent.
0