Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Cursor;
- import java.awt.Dimension;
- import java.awt.EventQueue;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import javax.swing.*;
- import java.awt.*;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import java.awt.SystemColor;
- import javax.swing.JButton;
- import javax.swing.JComponent;
- import javax.swing.JOptionPane;
- import javax.swing.JRadioButton;
- import javax.swing.ImageIcon;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.awt.event.ComponentEvent;
- import java.awt.event.ComponentListener;
- import java.awt.event.ItemEvent;
- import java.awt.event.ItemListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionAdapter;
- import java.awt.geom.AffineTransform;
- import java.awt.geom.PathIterator;
- import java.awt.geom.Point2D;
- import java.awt.geom.Rectangle2D;
- import javax.swing.JLabel;
- import java.awt.Color;
- import java.util.Random;
- class MyComponent extends JComponent {
- public void paint(Graphics g) {
- g.fillRect(30, 30, 100, 100);
- }
- }
- public class testA extends JFrame {
- private JPanel contentPane;
- boolean check=false;
- boolean choice=false;
- boolean choice1=false;
- private Dimension area;
- private static final int NONE = -1;
- private static final int BORDER = 3;
- private static final ComponentListener ComponentEvent = null;
- private int startX = NONE;
- private int startY = NONE;
- private int prevX = NONE;
- private int prevY = NONE;
- private boolean resize = false;
- int shape;
- Random rd = new Random();
- int size,size1;
- JButton btnNewButton = new JButton("");
- JButton btnNewButton_1 = new JButton("");
- JButton btnNewButton_2 = new JButton("");
- JButton btnNewButton_3 = new JButton("");
- JButton btnNewButton_4 = new JButton("");
- JButton btnNewButton_5 = new JButton("");
- JButton button = new JButton("");
- JButton button_1 = new JButton("");
- JButton button_2 = new JButton("");
- JButton button_3 = new JButton("");
- JButton button_4 = new JButton("");
- JButton button_5 = new JButton("");
- JPanel panel = new JPanel();
- JPanel panel_1 = new JPanel();
- JPanel panel_2 = new JPanel();
- JPanel panel_3 = new JPanel();
- JRadioButton rdbtnFill = new JRadioButton("Fill");
- JRadioButton rdbtnNewRadioButton = new JRadioButton("Unfill");
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- testA frame = new testA();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public testA() {
- setResizable(false);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 567, 490);
- contentPane = new JPanel();
- contentPane.setBackground(SystemColor.textHighlight);
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- JPanel panel = new JPanel();
- panel.setBackground(Color.WHITE);
- panel.setBounds(443, 0, 115, 252);
- contentPane.add(panel);
- panel.setLayout(null);
- btnNewButton.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\rect.GIF"));
- btnNewButton.setBounds(29, 24, 55, 49);
- panel.add(btnNewButton);
- btnNewButton.addActionListener(new ButtonListener());
- btnNewButton_1.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\round.GIF"));
- btnNewButton_1.setBounds(29, 76, 55, 49);
- panel.add(btnNewButton_1);
- btnNewButton_1.addActionListener(new ButtonListener());
- btnNewButton_2.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\triangle.GIF"));
- btnNewButton_2.setBounds(28, 136, 56, 53);
- panel.add(btnNewButton_2);
- btnNewButton_2.addActionListener(new ButtonListener());
- btnNewButton_3.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\oval.GIF"));
- btnNewButton_3.setBounds(29, 200, 55, 41);
- panel.add(btnNewButton_3);
- btnNewButton_3.addActionListener(new ButtonListener());
- JLabel lblShape = new JLabel("Shape");
- lblShape.setForeground(SystemColor.textHighlight);
- lblShape.setBackground(SystemColor.textHighlight);
- lblShape.setBounds(38, -1, 46, 14);
- panel.add(lblShape);
- panel_1.setBackground(Color.WHITE);
- panel_1.setBounds(443, 253, 115, 80);
- contentPane.add(panel_1);
- panel_1.setLayout(null);
- //rdbtnFill.setSelected(true);
- rdbtnFill.setBounds(12, 18, 84, 23);
- panel_1.add(rdbtnFill);
- rdbtnNewRadioButton.setBounds(12, 44, 84, 23);
- panel_1.add(rdbtnNewRadioButton);
- panel_2.setBackground(Color.WHITE);
- panel_2.setBounds(0, 334, 558, 128);
- contentPane.add(panel_2);
- panel_2.setLayout(null);
- btnNewButton_4.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\.5x.GIF"));
- btnNewButton_4.setBounds(23, 32, 83, 85);
- panel_2.add(btnNewButton_4);
- btnNewButton_4.addActionListener(new ButtonListener());
- button.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\2x.GIF"));
- button.setBounds(109, 32, 77, 82);
- panel_2.add(button);
- button.addActionListener(new ButtonListener());
- button_1.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\clear.GIF"));
- button_1.setBounds(270, 51, 90, 49);
- panel_2.add(button_1);
- button_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- panel_3.removeAll();
- panel_3.repaint();
- }
- });
- button_2.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\up.GIF"));
- button_2.setBounds(455, 21, 38, 27);
- panel_2.add(button_2);
- //button_2.addActionListener(new ButtonListener1());
- button_3.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\down.GIF"));
- button_3.setBounds(455, 84, 38, 33);
- panel_2.add(button_3);
- //button_3.addActionListener(new ButtonListener1());
- button_4.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\Left.GIF"));
- button_4.setBounds(423, 50, 32, 33);
- panel_2.add(button_4);
- //button_4.addActionListener(new ButtonListener1());
- button_5.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\right.GIF"));
- button_5.setBounds(493, 50, 32, 33);
- panel_2.add(button_5);
- //button_5.addActionListener(new ButtonListener1());
- btnNewButton_5.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\A.GIF"));
- btnNewButton_5.setBounds(455, 50, 38, 33);
- panel_2.add(btnNewButton_5);
- //btnNewButton_5.addActionListener( this);
- JLabel lblResize = new JLabel("Resize");
- lblResize.setForeground(SystemColor.textHighlight);
- lblResize.setBounds(82, 0, 46, 14);
- panel_2.add(lblResize);
- JLabel lblClear = new JLabel("Clear");
- lblClear.setForeground(SystemColor.textHighlight);
- lblClear.setBounds(289, 0, 46, 14);
- panel_2.add(lblClear);
- JLabel lblMove = new JLabel("Move");
- lblMove.setForeground(SystemColor.textHighlight);
- lblMove.setBounds(458, 0, 46, 14);
- panel_2.add(lblMove);
- panel_3.setBackground(Color.WHITE);
- panel_3.setBounds(0, 0, 441, 333);
- contentPane.add(panel_3);
- rdbtnFill.addItemListener(new JButtonListener()) ;
- rdbtnNewRadioButton.addItemListener(new JButtonListener()) ;
- }
- public class JButtonListener implements ItemListener
- {
- public void itemStateChanged(ItemEvent e) {
- if(e.getSource()==rdbtnFill)
- {
- check=true;
- }
- else if(e.getSource()==rdbtnNewRadioButton)
- {
- check=false;
- }
- }
- }
- class MyCanvas extends JComponent {
- public void paint(Graphics g) {
- super.paintComponent(g);
- g.setColor(Color.RED);
- g.fill3DRect(10, 10, 200, 200,true);
- }
- }
- class MyCanvas1 extends JComponent {
- public void paint(Graphics g) {
- super.paintComponent(g);
- g.setColor(Color.RED);
- g.drawRect(20, 20, 200, 200);
- }
- }
- class MyCanvas2 extends JComponent {
- public void paint(Graphics g) {
- super.paintComponent(g);
- g.setColor(Color.RED);
- g.fillOval(30, 30, 150, 150);
- }
- }
- class MyCanvas3 extends JComponent {
- public void paint(Graphics g) {
- super.paintComponent(g);
- g.setColor(Color.RED);
- g.drawOval(30, 30, 150, 150);
- }
- }
- class MyCanvas4 extends JComponent {
- public void paint(Graphics g) {
- super.paintComponent(g);
- int xPos=150;
- int yPos=50;
- int size=100;
- g.setColor(Color.RED);
- g.fillPolygon(
- new int[]{xPos, xPos + size, xPos - size},
- new int[]{yPos, yPos + size, yPos + size}, 3);
- }
- }
- class MyCanvas5 extends JComponent {
- public void paint(Graphics g) {
- super.paintComponent(g);
- int xPos=150;
- int yPos=50;
- int size=100;
- g.setColor(Color.RED);
- g.drawPolygon(
- new int[]{xPos, xPos + size, xPos - size},
- new int[]{yPos, yPos + size, yPos + size}, 3);
- }
- }
- class MyCanvas6 extends JComponent {
- public void paint(Graphics g) {
- super.paintComponent(g);
- g.setColor(Color.RED);
- g.drawOval(30, 30, 150, 100);
- }
- }
- class MyCanvas7 extends JComponent {
- public void paint(Graphics g) {
- super.paintComponent(g);
- g.setColor(Color.RED);
- g.fillOval(30, 30, 150, 100);
- }
- }
- public class ButtonListener implements ActionListener
- {
- public void actionPerformed(ActionEvent e) {
- if(check)
- {
- if(e.getSource()==btnNewButton)
- {
- MyCanvas can=new MyCanvas();
- can.setSize(200, 100);
- panel_3.add(can);
- panel_3.repaint();
- addComponent(can);
- //panel_3.addComponentListener(this);
- }
- else if(e.getSource()==btnNewButton_1)
- {
- panel_3.removeAll();
- MyCanvas2 can=new MyCanvas2();
- can.setSize(300, 300);
- panel_3.add(can);
- panel_3.repaint();
- addComponent(can);
- can.addComponentListener((ComponentListener) this);
- }
- else if(e.getSource()==btnNewButton_2)
- {
- panel_3.removeAll();
- MyCanvas4 can=new MyCanvas4();
- can.setSize(300, 300);
- panel_3.add(can);
- panel_3.repaint();
- addComponent(can);
- }
- else if(e.getSource()==btnNewButton_3)
- {
- panel_3.removeAll();
- MyCanvas7 can=new MyCanvas7();
- can.setSize(500, 500);
- panel_3.add(can);
- panel_3.repaint();
- addComponent(can);
- }
- }
- else
- {
- if(e.getSource()==btnNewButton)
- {
- panel_3.removeAll();
- MyCanvas1 can=new MyCanvas1();
- can.setSize(500, 500);
- panel_3.add(can);
- panel_3.repaint();
- addComponent(can);
- }
- else if(e.getSource()==btnNewButton_1)
- {
- panel_3.removeAll();
- MyCanvas3 can=new MyCanvas3();
- can.setSize(500, 500);
- panel_3.add(can);
- panel_3.repaint();
- addComponent(can);
- }
- else if(e.getSource()==btnNewButton_2)
- {
- panel_3.removeAll();
- MyCanvas5 can=new MyCanvas5();
- can.setSize(500, 500);
- panel_3.add(can);
- panel_3.repaint();
- addComponent(can);
- }
- else if(e.getSource()==btnNewButton_3)
- {
- panel_3.removeAll();
- MyCanvas6 can=new MyCanvas6();
- can.setSize(500, 500);
- panel_3.add(can);
- panel_3.repaint();
- addComponent(can);
- }
- }
- }
- }
- //This is mouse event Which will move shape with mouse
- public void addComponent( JComponent comp) {
- comp.setBounds(10, 10, 1000, 1000);
- comp.addMouseListener(new MouseAdapter() {
- public void mouseReleased(MouseEvent e) {
- startX = NONE;
- startY = NONE;
- ((JComponent) e.getSource()).setCursor(Cursor.getDefaultCursor());
- }
- public void mousePressed(MouseEvent e) {
- startX = e.getX();
- startY = e.getY();
- }
- });
- comp.addMouseMotionListener(new MouseMotionAdapter() {
- public void mouseMoved(MouseEvent e) {
- JComponent source = (JComponent) e.getSource();
- int x = e.getX();
- int y = e.getY();
- Rectangle bounds = source.getBounds();
- resize = x < BORDER || y < BORDER || Math.abs(bounds.width - x) < BORDER || Math.abs(bounds.height - y) < BORDER;
- if (resize) {
- // TODO: there are a lot of resize cursors here, this is just of proof of concept
- source.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
- } else {
- source.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
- }
- }
- public void mouseDragged(MouseEvent e) {
- int x = e.getX();
- int y = e.getY();
- if (startX != NONE && startY != NONE) {
- JComponent source = (JComponent) e.getSource();
- Rectangle bounds = source.getBounds();
- int deltaX = x - startX;
- int deltaY = y - startY;
- if (resize) {
- // TODO: handle all resize cases, left, right,...
- source.setSize(Math.max(10, bounds.width + x - prevX), Math.max(10, bounds.height + y - prevY));
- } else {
- source.setLocation(bounds.x + deltaX, bounds.y + deltaY);
- }
- // TODO:
- // TODO:
- } else {
- startX = x;
- startY = y;
- }
- prevX = x;
- prevY = y;
- }
- });
- panel_3.add(comp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement