السلام عليكم ورحمة الله وبركاته
لقد وجدت الطريقة التي تعمل undo/redo
الزر redo يعمل بشكل جيد
لكن المشكل في الزر عندما أضيف cercle يعمل
و عندما أعيد الضغط على الزر redo هو الأخر يعمل بشكل جيد
لكن عندما أعيد الضغط على الزر undo فأنه يحدف كل شيئ مرة واحدة
أرجوا منكم توضيح أين هو المشكل
code
package javaapplication8;
import java.io.Serializable;
import java.awt.*;
import java.util.List;
import java.util.*;
public class Place implements Serializable {
private int x, y;
protected List place = new LinkedList();
public Place(){}
public Place(int x, int y){
setX(x);
setY(y);
}
public void setX(int a) {
x = a;
}
public void setY(int b) {
y = b;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean contains(int a, int b) {
return (boolean) ((x <= a) & (a <= x + 90) & (y <= b) & (b <= y + 20));
}
public void dessiner(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.GREEN);
g2d.fillOval(x, y, 50, 50);
}
public void addSe(Place place) {
this.place.add(place);
}
public void removeSe(Place place) {
this.place.remove(place);
}
}code
package javaapplication8;
import java.util.*;
import java.io.*;
import java.awt.Graphics2D.*;
import java.awt.Graphics.*;
public class Graph implements Serializable{
public boolean modifie = false;
protected List places= new LinkedList();
public short nombrePlaceMax = 1;
public Graph(){ }
public Place getPlace() {
for (Iterator itr = places.iterator(); itr.hasNext(); ) {
Place n = (Place) itr.next();
return n;
}
return null;
}
public void addPlace(Place place) {
++nombrePlaceMax;
places.add(place);
}
public void removePlace() {
for (Iterator itr = places.iterator(); itr.hasNext(); ) {
Place n = (Place) itr.next();
places.remove(n);
return;
}
}
}code
package javaapplication8;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
public class Paneau extends JPanel implements MouseListener,MouseMotionListener {
protected List placeSelected = new LinkedList();
static public Place placeChoisi;
public Paneau() {
addMouseListener(this);
addMouseMotionListener(this);
}
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
//super.paintComponent(g2d);
setBackground(Color.white);
if (Prin.graph.places!= null) {
for (Iterator itr = Prin.graph.places.iterator(); itr
.hasNext();) {
Place t = (Place) itr.next();
t.dessiner(g2d);
}
}
repaint() ;
}
public void mouseDragged(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) {
if (Prin.ETAT == 2 && e.getButton() == e.BUTTON1) {
Place nn = new Place(e.getX(),e.getY());
Prin.graph.addPlace(nn);
Prin.graph.modifie = true;
Prin.test();
repaint();
}
repaint();
}
public void mouseMoved(MouseEvent e) { }
public void mouseClicked(final MouseEvent e) {}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
}code
package javaapplication8;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
public class Prin extends JFrame implements ActionListener {
static public Graph graph;
public static int ETAT = 0;
private JPanel jPanel11 = null;
private JScrollPane jScrollPane = null;
private JToolBar jToolBar = null;
private JToggleButton btnPlace = null;
JButton undoButton = new JButton();
JButton redoButton = new JButton();
static Paneau jPanelDessin = null;
private JPanel jContentPane = null;
static List next = new LinkedList();
static int pointeur=0;
public Prin() {
super();
initialize();
}
private void initialize() {
this.setSize(700, 400);
graph = new Graph();
this.next.add(new nextcomp() );
this.setContentPane(getJContentPane());
this.setTitle("Dessin");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private JPanel getJPanel11() {
if (jPanel11 == null) {
jPanel11 = new JPanel();
//jPanel11.setLayout(null);
jPanel11.setSize(new java.awt.Dimension(20, 50));
jPanel11.add(getJRedoButton());
jPanel11.add(getJUndoButton());
jPanel11.setPreferredSize(new java.awt.Dimension(20,60));
jPanel11.add(getBtnPlace());
}
return jPanel11;
}
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBackground(java.awt.Color.white);
jScrollPane.setViewportView(getjPanelDessin());
}
return jScrollPane;
}
private JButton getJUndoButton(){
undoButton.setText("Undo");
undoButton.setPreferredSize(new java.awt.Dimension(100,30));
undoButton.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent e) {
if (pointeur > 0) {
redoButton.setEnabled(true);
pointeur--;
graph.modifie = true;
Retablir();
}
if (pointeur == 0) {
undoButton.setEnabled(false);
}
jPanelDessin.repaint();
}
});
return undoButton;
}
private JButton getJRedoButton(){
redoButton.setText("Redo");
redoButton.setPreferredSize(new java.awt.Dimension(100,30));
redoButton.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent e) {
if (pointeur == (next.size() - 2)) {
redoButton.setEnabled(false);
}
if (pointeur < (next.size() - 1)) {
getJUndoButton().setEnabled(true);
pointeur++;
// this.jMenuItem4.setEnabled(true);
graph.modifie= true;
Retablir();
}
//if(this.next.size()==0 | this.next.size()==this.pointeur)Suiv.setEnabled(false);
jPanelDessin.repaint();
}
});
return redoButton;
}
private JToggleButton getBtnPlace(){
if (btnPlace == null) {
btnPlace = new JToggleButton();
btnPlace.setText("Cercle");
btnPlace.setPreferredSize(new java.awt.Dimension(100,30));
btnPlace.setSelected(false);
btnPlace.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
//System.out.println("actionPerformed()");
if (ETAT != 2) {
ETAT = 2;
}
}
});
}
return btnPlace;
}
Paneau getjPanelDessin() {
if (jPanelDessin == null) {
jPanelDessin = new Paneau();
}
return jPanelDessin;
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJPanel11(), java.awt.BorderLayout.NORTH);
jContentPane.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
}
return jContentPane;
}
public void actionPerformed(ActionEvent e) {}
public static void main(String[] args) {
Prin pr = new Prin();
pr.setVisible(true);
}
static void test() {
Prin.pointeur++;
System.out.println("pointeur"+(pointeur));
System.out.println("next"+Prin.next.size());
Prin.next.add(new nextcomp());
System.out.println("next: "+Prin.next.size());
Prin.graph.modifie=true;
}
static void Retablir() {
graph.places.clear();
nextcomp nc = (nextcomp) next.get(pointeur);
System.out.println("next rt"+Prin.next.size());
if (nc != null) {
for (Iterator ity = nc.nextCercle.iterator(); ity.hasNext(); ) {
Place ncp = (Place) ity.next();
graph.places.add(ncp);}
}
}
static class nextcomp {
List nextCercle = new LinkedList();
public nextcomp() {
for (Iterator n = Prin.graph.places.iterator(); n.hasNext(); ) {
Place nd = (Place) n.next();
this.nextCercle.add(nd);
}
}
}
}