V02
This commit is contained in:
parent
48460e53f1
commit
f053d762ca
|
|
@ -2,7 +2,17 @@ package packageIHM;
|
||||||
|
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.EventQueue;
|
||||||
|
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.border.EmptyBorder;
|
||||||
|
|
||||||
|
import packageJPC.JPanelConversation;
|
||||||
|
|
||||||
|
import javax.swing.JTextField;
|
||||||
|
import javax.swing.SwingConstants;
|
||||||
|
import javax.swing.JButton;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.event.ComponentAdapter;
|
import java.awt.event.ComponentAdapter;
|
||||||
import java.awt.event.ComponentEvent;
|
import java.awt.event.ComponentEvent;
|
||||||
|
|
@ -10,115 +20,104 @@ import java.awt.event.MouseEvent;
|
||||||
import java.awt.event.MouseMotionListener;
|
import java.awt.event.MouseMotionListener;
|
||||||
import java.awt.event.MouseWheelEvent;
|
import java.awt.event.MouseWheelEvent;
|
||||||
import java.awt.event.MouseWheelListener;
|
import java.awt.event.MouseWheelListener;
|
||||||
import java.util.ArrayList;
|
import java.awt.event.ActionEvent;
|
||||||
|
|
||||||
import javax.swing.AbstractAction;
|
public class MaJFrame extends JFrame implements MouseMotionListener, MouseWheelListener {
|
||||||
import javax.swing.Action;
|
|
||||||
import javax.swing.JButton;
|
|
||||||
import javax.swing.JFrame;
|
|
||||||
import javax.swing.JPanel;
|
|
||||||
import javax.swing.JTextField;
|
|
||||||
import javax.swing.SwingConstants;
|
|
||||||
import javax.swing.border.EmptyBorder;
|
|
||||||
import javax.swing.border.LineBorder;
|
|
||||||
|
|
||||||
public class MaJFrame extends JFrame implements MouseMotionListener, MouseWheelListener{
|
private static final long serialVersionUID = 2891820963050555444L;
|
||||||
|
|
||||||
private static final long serialVersionUID = 7252959164975426293L;
|
|
||||||
private JPanel contentPane;
|
private JPanel contentPane;
|
||||||
private JPanelConversation conversationPanel;
|
private JTextField textField;
|
||||||
private JPanel panel;
|
private JPanelConversation panel_1;
|
||||||
private JTextField textFieldMsg;
|
private int numeroMessage=0;
|
||||||
private ArrayList<Message> messages;
|
|
||||||
private int previousY;
|
/**
|
||||||
|
* Launch the application.
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
EventQueue.invokeLater(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
MaJFrame frame = new MaJFrame();
|
||||||
|
frame.setVisible(true);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the frame.
|
* Create the frame.
|
||||||
*/
|
*/
|
||||||
public MaJFrame() {
|
public MaJFrame() {
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
setBounds(100, 100, 450, 300);
|
setBounds(100, 100, 700, 400);
|
||||||
contentPane = new JPanel();
|
contentPane = new JPanel();
|
||||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||||
contentPane.setLayout(new BorderLayout(0, 0));
|
contentPane.setLayout(new BorderLayout(0, 0));
|
||||||
|
|
||||||
panel = new JPanel();
|
|
||||||
panel.setBorder(new LineBorder(new Color(0, 0, 0)));
|
|
||||||
contentPane.add(panel, BorderLayout.NORTH);
|
|
||||||
panel.setLayout(new BorderLayout(0, 0));
|
|
||||||
|
|
||||||
|
|
||||||
Action action = new AbstractAction() {
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
addMessage();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
JButton buttonAdd = new JButton("Add");
|
|
||||||
panel.add(buttonAdd, BorderLayout.EAST);
|
|
||||||
buttonAdd.addActionListener(new ActionListener() {
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
addMessage();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
textFieldMsg = new JTextField();
|
|
||||||
panel.add(textFieldMsg, BorderLayout.CENTER);
|
|
||||||
textFieldMsg.setColumns(10);
|
|
||||||
textFieldMsg.addActionListener(action);
|
|
||||||
|
|
||||||
messages = new ArrayList<Message>();
|
|
||||||
String msgLeft = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n"
|
|
||||||
+ "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";
|
|
||||||
String msgRight = "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n"
|
|
||||||
+ "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
|
|
||||||
|
|
||||||
for (int i=0;i<20;i++) {
|
|
||||||
if (i%2==0) {
|
|
||||||
messages.add(new Message(Color.BLUE, i + " " + msgLeft, SwingConstants.LEFT));
|
|
||||||
} else {
|
|
||||||
messages.add(new Message(Color.RED, i + " " + msgRight, SwingConstants.RIGHT));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
conversationPanel = new JPanelConversation(this, messages);
|
|
||||||
conversationPanel .setBackground(Color.WHITE);
|
|
||||||
conversationPanel.addMouseMotionListener(this);
|
|
||||||
conversationPanel.addMouseWheelListener(this);
|
|
||||||
|
|
||||||
contentPane.add(conversationPanel, BorderLayout.CENTER);
|
|
||||||
setContentPane(contentPane);
|
setContentPane(contentPane);
|
||||||
|
|
||||||
|
JPanel panel = new JPanel();
|
||||||
|
contentPane.add(panel, BorderLayout.NORTH);
|
||||||
|
|
||||||
|
textField = new JTextField();
|
||||||
|
panel.add(textField);
|
||||||
|
textField.setColumns(30);
|
||||||
|
|
||||||
|
JButton btnNewButton = new JButton("Ajouter");
|
||||||
|
btnNewButton.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent arg0) {
|
||||||
|
clicAjouter();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
panel.add(btnNewButton);
|
||||||
|
|
||||||
|
JButton btnNewButton_1 = new JButton("Effacer");
|
||||||
|
btnNewButton_1.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent arg0) {
|
||||||
|
clicEffacer();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
panel.add(btnNewButton_1);
|
||||||
|
|
||||||
|
panel_1 = new JPanelConversation();
|
||||||
|
contentPane.add(panel_1, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
// Gestion de la souris
|
||||||
|
panel_1.addMouseMotionListener(this);
|
||||||
|
panel_1.addMouseWheelListener(this);
|
||||||
addComponentListener(new ComponentAdapter() {
|
addComponentListener(new ComponentAdapter() {
|
||||||
public void componentResized(ComponentEvent componentEvent) {
|
public void componentResized(ComponentEvent componentEvent) {
|
||||||
conversationPanel.calculateOffsetToShowLastMessage();
|
panel_1.calculateOffsetToShowLastMessage();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void addMessage() {
|
protected void clicEffacer() {
|
||||||
//panel.setImage("image1.jpg");
|
panel_1.effacerTout();
|
||||||
//panel_1.setImage("image2.png");
|
}
|
||||||
if (textFieldMsg.getText().length()>0) {
|
|
||||||
messages.add(new Message(Color.RED,textFieldMsg.getText(),SwingConstants.RIGHT));
|
protected void clicAjouter() {
|
||||||
conversationPanel.repaint();
|
if (numeroMessage%2==0)
|
||||||
textFieldMsg.setText("");
|
panel_1.ajouterMessage(Color.blue,textField.getText(),SwingConstants.LEFT);
|
||||||
}
|
else
|
||||||
|
panel_1.ajouterMessage(Color.red,textField.getText(),SwingConstants.RIGHT);
|
||||||
|
textField.setText("");
|
||||||
|
numeroMessage++; // permet d'afficher alternativement à gauche et à droite
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Gestion de la souris
|
||||||
|
private int previousY=0;
|
||||||
|
|
||||||
private void up(int x) {
|
private void up(int x) {
|
||||||
conversationPanel.up(x);
|
panel_1.up(x);
|
||||||
conversationPanel.repaint();
|
panel_1.repaint();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void down(int x) {
|
private void down(int x) {
|
||||||
conversationPanel.down(x);
|
panel_1.down(x);
|
||||||
conversationPanel.repaint();
|
panel_1.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -139,8 +138,6 @@ public class MaJFrame extends JFrame implements MouseMotionListener, MouseWheelL
|
||||||
public void mouseMoved(MouseEvent e) {
|
public void mouseMoved(MouseEvent e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseWheelMoved(MouseWheelEvent e) {
|
public void mouseWheelMoved(MouseWheelEvent e) {
|
||||||
if (e.getWheelRotation() < 0) {
|
if (e.getWheelRotation() < 0) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,156 @@
|
||||||
|
package packageIHM;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.awt.event.ComponentAdapter;
|
||||||
|
import java.awt.event.ComponentEvent;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
import java.awt.event.MouseMotionListener;
|
||||||
|
import java.awt.event.MouseWheelEvent;
|
||||||
|
import java.awt.event.MouseWheelListener;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import javax.swing.AbstractAction;
|
||||||
|
import javax.swing.Action;
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JTextField;
|
||||||
|
import javax.swing.SwingConstants;
|
||||||
|
import javax.swing.border.EmptyBorder;
|
||||||
|
import javax.swing.border.LineBorder;
|
||||||
|
|
||||||
|
import packageJPC.JPanelConversation;
|
||||||
|
import packageJPC.Message;
|
||||||
|
|
||||||
|
public class MaJFrameEtienne extends JFrame implements MouseMotionListener, MouseWheelListener{
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 7252959164975426293L;
|
||||||
|
private JPanel contentPane;
|
||||||
|
private JPanelConversation conversationPanel;
|
||||||
|
private JPanel panel;
|
||||||
|
private JTextField textFieldMsg;
|
||||||
|
private ArrayList<Message> messages;
|
||||||
|
private int previousY;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the frame.
|
||||||
|
*/
|
||||||
|
public MaJFrameEtienne() {
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setBounds(100, 100, 450, 300);
|
||||||
|
contentPane = new JPanel();
|
||||||
|
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||||
|
contentPane.setLayout(new BorderLayout(0, 0));
|
||||||
|
|
||||||
|
panel = new JPanel();
|
||||||
|
panel.setBorder(new LineBorder(new Color(0, 0, 0)));
|
||||||
|
contentPane.add(panel, BorderLayout.NORTH);
|
||||||
|
panel.setLayout(new BorderLayout(0, 0));
|
||||||
|
|
||||||
|
|
||||||
|
Action action = new AbstractAction() {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
addMessage();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
JButton buttonAdd = new JButton("Add");
|
||||||
|
panel.add(buttonAdd, BorderLayout.EAST);
|
||||||
|
buttonAdd.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
addMessage();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
textFieldMsg = new JTextField();
|
||||||
|
panel.add(textFieldMsg, BorderLayout.CENTER);
|
||||||
|
textFieldMsg.setColumns(10);
|
||||||
|
textFieldMsg.addActionListener(action);
|
||||||
|
|
||||||
|
messages = new ArrayList<Message>();
|
||||||
|
String msgLeft = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n"
|
||||||
|
+ "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";
|
||||||
|
String msgRight = "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n"
|
||||||
|
+ "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
|
||||||
|
|
||||||
|
for (int i=0;i<20;i++) {
|
||||||
|
if (i%2==0) {
|
||||||
|
messages.add(new Message(Color.BLUE, i + " " + msgLeft, SwingConstants.LEFT));
|
||||||
|
} else {
|
||||||
|
messages.add(new Message(Color.RED, i + " " + msgRight, SwingConstants.RIGHT));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
conversationPanel = new JPanelConversation(this, messages);
|
||||||
|
conversationPanel .setBackground(Color.WHITE);
|
||||||
|
conversationPanel.addMouseMotionListener(this);
|
||||||
|
conversationPanel.addMouseWheelListener(this);
|
||||||
|
|
||||||
|
contentPane.add(conversationPanel, BorderLayout.CENTER);
|
||||||
|
setContentPane(contentPane);
|
||||||
|
|
||||||
|
addComponentListener(new ComponentAdapter() {
|
||||||
|
public void componentResized(ComponentEvent componentEvent) {
|
||||||
|
conversationPanel.calculateOffsetToShowLastMessage();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void addMessage() {
|
||||||
|
//panel.setImage("image1.jpg");
|
||||||
|
//panel_1.setImage("image2.png");
|
||||||
|
if (textFieldMsg.getText().length()>0) {
|
||||||
|
messages.add(new Message(Color.RED,textFieldMsg.getText(),SwingConstants.RIGHT));
|
||||||
|
conversationPanel.repaint();
|
||||||
|
textFieldMsg.setText("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void up(int x) {
|
||||||
|
conversationPanel.up(x);
|
||||||
|
conversationPanel.repaint();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void down(int x) {
|
||||||
|
conversationPanel.down(x);
|
||||||
|
conversationPanel.repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseDragged(MouseEvent e) {
|
||||||
|
int y = e.getY();
|
||||||
|
if (y < previousY) {
|
||||||
|
if (Math.abs(previousY-y)<60)
|
||||||
|
up(Math.abs(previousY-y));
|
||||||
|
|
||||||
|
} else if (y > previousY) {
|
||||||
|
if (Math.abs(previousY-y)<60)
|
||||||
|
down(Math.abs(previousY-y));
|
||||||
|
}
|
||||||
|
previousY = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseMoved(MouseEvent e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseWheelMoved(MouseWheelEvent e) {
|
||||||
|
if (e.getWheelRotation() < 0) {
|
||||||
|
up(20);
|
||||||
|
} else {
|
||||||
|
down(20);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package packageIHM;
|
package packageJPC;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
@ -25,7 +25,28 @@ public class JPanelConversation extends JPanel
|
||||||
private int fullHeight = -1;
|
private int fullHeight = -1;
|
||||||
|
|
||||||
|
|
||||||
JPanelConversation(MaJFrame mjf, ArrayList<Message> messages) {
|
public JPanelConversation() {
|
||||||
|
setLayout(null);
|
||||||
|
messages = new ArrayList<Message>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ajouterMessage(Color color, String msg, int side) {
|
||||||
|
messages.add(new Message(color,msg,side));
|
||||||
|
this.repaint();
|
||||||
|
}
|
||||||
|
public void ajouterMessage(Message msg) {
|
||||||
|
messages.add(msg);
|
||||||
|
this.repaint();
|
||||||
|
}
|
||||||
|
public void effacerTout() {
|
||||||
|
this.removeAll();
|
||||||
|
messages.clear();
|
||||||
|
this.repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public JPanelConversation(ArrayList<Message> messages) {
|
||||||
setLayout(null);
|
setLayout(null);
|
||||||
int posX = marginLeft;
|
int posX = marginLeft;
|
||||||
int posY;
|
int posY;
|
||||||
|
|
@ -70,6 +91,7 @@ public class JPanelConversation extends JPanel
|
||||||
bubble.setSize(bubble.getPreferredSize());
|
bubble.setSize(bubble.getPreferredSize());
|
||||||
bubble.setLocation(marginLeft, currentHeight);
|
bubble.setLocation(marginLeft, currentHeight);
|
||||||
bubble.setLineWrap(true);
|
bubble.setLineWrap(true);
|
||||||
|
bubble.addMouseMotionListener(new RedispatchingMouseAdapter() );
|
||||||
add(bubble);
|
add(bubble);
|
||||||
msg.setBubble(bubble);
|
msg.setBubble(bubble);
|
||||||
msg.setPosY(currentHeight);
|
msg.setPosY(currentHeight);
|
||||||
|
|
@ -109,6 +131,7 @@ public class JPanelConversation extends JPanel
|
||||||
|
|
||||||
public void paintComponent(Graphics g) {
|
public void paintComponent(Graphics g) {
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
|
this.setBackground(Color.WHITE);
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
calculateOffsetToShowLastMessage();
|
calculateOffsetToShowLastMessage();
|
||||||
initialized=true;
|
initialized=true;
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package packageIHM;
|
package packageJPC;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
|
|
||||||
import javax.swing.JTextArea;
|
import javax.swing.JTextArea;
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
package packageIHM;
|
package packageJPC;
|
||||||
|
|
||||||
|
|
||||||
import java.awt.Component;
|
import java.awt.Component;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package packageIHM;
|
package packageJPC;
|
||||||
|
|
||||||
import java.awt.BasicStroke;
|
import java.awt.BasicStroke;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
Loading…
Reference in New Issue