Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package paint;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.event.ActionEvent;
- import java.awt.event.*;
- import java.awt.ActiveEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class GUI extends JFrame implements MouseListener , MouseMotionListener{
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- JButton ClearBtn , RedBtn ,BlackBtn , LineBtn;
- JFrame paint = new JFrame("Paint");
- JPanel controls = new JPanel();
- private int OldX , OldY , CurrentX , CurrentY ;
- boolean LineClicked = false ;
- MouseEvent mouse;
- Sketch sketch = new Sketch();
- Container content = paint.getContentPane();
- ActionListener action= new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- System.out.println(e.getSource() == ClearBtn);
- System.out.println(e.getSource());
- if (e.getSource()==RedBtn) {
- sketch.red();
- }
- else if (e.getSource() == ClearBtn) {
- System.out.println(e.getSource() == ClearBtn);
- if(e.getSource() == ClearBtn){
- sketch.clear();
- }
- }
- }
- };
- public static void main (String[] args) {
- // GUI here = new GUI ();
- // here.setVisible(true);
- new GUI().show();
- }
- public void show () {
- paint.setLayout(new BorderLayout());
- // sketch.setLayout(new BorderLayout());
- content.add(sketch, BorderLayout.SOUTH);
- content.setLayout(new BorderLayout());
- content.add(sketch, BorderLayout.CENTER);
- // create control .. buttons
- JButton BlackBtn = new JButton();
- BlackBtn.setBackground(Color.black);
- BlackBtn.addActionListener(action);
- ClearBtn = new JButton();
- ClearBtn.setVisible(true);
- ClearBtn.setText("CLEAR");
- ClearBtn.addActionListener(action);
- ClearBtn.setBackground(Color.white);
- ClearBtn.setForeground(Color.black);
- getContentPane().add(ClearBtn);
- RedBtn = new JButton();
- RedBtn.setVisible(true);
- RedBtn.addActionListener(action);
- RedBtn.setBackground(Color.RED);
- RedBtn.setForeground(Color.black);
- getContentPane().add(RedBtn);
- RedBtn.setBackground(Color.red);
- LineBtn = new JButton();
- LineBtn.setVisible(true);
- LineBtn.addActionListener(action);
- LineBtn.setText("line");
- LineBtn.setForeground(Color.green);
- getContentPane().add(LineBtn);
- LineBtn.addMouseListener(this);
- controls.add(RedBtn);
- controls.add(BlackBtn);
- controls.add(LineBtn);
- controls.add(ClearBtn);
- content.add(controls , BorderLayout.NORTH);
- paint.setSize(600, 600);
- paint.setVisible(true);
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- // TODO Auto-generated method stub
- if (e.getSource() == LineBtn){
- LineClicked = true ;
- }
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseExited(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mousePressed(MouseEvent e) {
- // TODO Auto-generated method stub
- if (LineClicked){
- OldX = e.getX();
- OldY = e.getY();
- }
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- // TODO Auto-generated method stub
- CurrentX=e.getX();
- CurrentY=e.getY();
- if (sketch.graphic !=null){
- sketch.graphic.drawLine(OldX , OldY , CurrentX , CurrentY);
- repaint();
- OldX = CurrentX;
- OldY = CurrentY;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement