Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Main;
- public class Main {
- public static void main(String[] args) {
- new MainFrame();
- }
- }
- /////////////////////////////////////////
- package Main;
- import java.awt.BorderLayout;
- import java.awt.Dimension;
- import javax.swing.JFrame;
- import draw.DrawPanel;
- import Menu.MainMenu;
- import Menu.MainToolbar;
- public class MainFrame extends JFrame {
- private static final long serialVersionUID = 1L;
- private static final Dimension SIZE = new Dimension(800, 600);
- private DrawPanel panel = new DrawPanel();
- public MainFrame(){
- super("Java Paint");
- setLayout(new BorderLayout());
- add(panel, BorderLayout.CENTER);
- //
- MainMenu oknog = new MainMenu();
- setJMenuBar(oknog);
- MainToolbar oknot = new MainToolbar();
- add(oknot, BorderLayout.NORTH);
- setSize(SIZE);
- setResizable(true);
- setLocationRelativeTo(null);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setVisible(true);
- }
- }
- //////////////////////////////////////////
- package draw;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.util.ArrayList;
- import java.util.List;
- import javax.swing.JPanel;
- import draw.shapes.Shape;
- import listeners.DrawPanelListener;
- public class DrawPanel extends JPanel {
- private static final long serialVersionUID = 1L;
- private DrawPanelListener dpl;
- private List<Shape>shapes = new ArrayList<>();
- private Shape tmpShape;
- public DrawPanel()
- {
- setOpaque(true);
- setBackground(Color.WHITE);
- dpl = new DrawPanelListener(this);
- addMouseListener(dpl);
- addMouseMotionListener(dpl);
- }
- public void paintComponents(Graphics g){
- super.paintComponent(g);
- if(tmpShape != null)
- tmpShape.render(g);
- for(Shape s : shapes )
- {
- if(s != null)
- s.render(g);
- }
- }
- public List<Shape> getShapes() {
- return shapes;
- }
- public void setShapes(List<Shape> shapes) {
- this.shapes = shapes;
- }
- public Shape getTmpShape() {
- return tmpShape;
- }
- public void setTmpShape(Shape tmpShape) {
- this.tmpShape = tmpShape;
- }
- }
- /////////////////////////////////////////////////
- package Menu;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class MainMenu extends JMenuBar{
- private JMenuItem newAction = new JMenuItem("Nowy");
- private JMenuItem openAction = new JMenuItem("Otwórz");
- private JMenuItem saveAction = new JMenuItem("Zapisz");
- private JMenuItem saveAsAction = new JMenuItem("Zapisz jako");
- private JMenuItem printAction = new JMenuItem("Drukuj");
- private JMenuItem exit = new JMenuItem("Zamknij");
- private JMenuItem undoAction = new JMenuItem("Cofnij");
- private JMenuItem redoAction = new JMenuItem("Redo");
- private JMenuItem copyAction = new JMenuItem("Kopiuj");
- private JMenuItem cutAction = new JMenuItem("Wytnij");
- private JMenuItem pasteAction = new JMenuItem("Wklej");
- private static final long serialVersionUID = 1L;
- public MainMenu(){
- JMenu plikmenu = new JMenu("Plik");
- JMenu edycjamenu = new JMenu("Edycja");
- this.add(plikmenu);
- this.add(edycjamenu);
- plikmenu.add(newAction);
- plikmenu.add(openAction);
- plikmenu.add(saveAction);
- plikmenu.add(saveAsAction);
- plikmenu.addSeparator();
- plikmenu.add(printAction);
- plikmenu.addSeparator();
- plikmenu.add(exit);
- edycjamenu.add(undoAction);
- edycjamenu.add(redoAction);
- edycjamenu.addSeparator();
- edycjamenu.add(copyAction);
- edycjamenu.add(cutAction);
- edycjamenu.add(pasteAction);
- initFileActions();
- initEditionAction();
- }
- private void initEditionAction() {
- undoAction.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- }
- });
- redoAction.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- }
- });
- copyAction.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- }
- });
- cutAction.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- }
- });
- pasteAction.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- }
- });
- }
- private void initFileActions(){
- newAction.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- }
- });
- openAction.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- }
- });
- saveAction.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- }
- });
- saveAsAction.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- }
- });
- printAction.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- }
- });
- exit.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- System.exit(0);
- }
- });
- }
- }
- ///////////////////////////////
- package listeners;
- import java.awt.Color;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.util.List;
- import draw.DrawPanel;
- import draw.shapes.Shape;
- import draw.shapes.Square;
- public class DrawPanelListener implements MouseListener , MouseMotionListener {
- private DrawPanel panel;
- private boolean draw = false;
- private Shape drawShape;
- public DrawPanelListener(DrawPanel dp){
- panel = dp;
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @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(!draw)
- draw= true;
- drawShape = new Square(e.getX(),e.getY(), e.getX() + 1, e.getY() + 1, Color.black);
- panel.setTmpShape(drawShape);
- panel.repaint();
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- drawShape.setX2(e.getX());
- drawShape.setY2(e.getY());
- List<Shape>shapes = panel.getShapes();
- shapes.add(drawShape);
- panel.setTmpShape(null);
- panel.setShapes(shapes);
- drawShape = null;
- panel.repaint();
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- // TODO Auto-generated method stub
- drawShape.setX2(e.getX());
- drawShape.setY2(e.getY());
- panel.setTmpShape(drawShape);
- panel.repaint();
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- }
- ////////////////////////////////
- package draw.shapes;
- import java.awt.Color;
- import java.awt.Graphics;
- public abstract class Shape {
- private int x,y;
- private int y2,x2;
- private Color color;
- public Shape (int x, int y, int x2, int y2, Color c)
- {
- this.x =x;
- this.y=y;
- this.y2 = y2;
- this.x2 = x2;
- color = c;
- }
- public abstract void render(Graphics g);
- public int Wight() {
- if(x2>x)
- return x2-x;
- else
- return x-x2;
- }
- public int Height()
- {
- if(y2>y)
- return y2-y;
- else
- return y-y2;
- }
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- public int getY2() {
- return y2;
- }
- public void setY2(int y2) {
- this.y2 = y2;
- }
- public int getX2() {
- return x2;
- }
- public void setX2(int x2) {
- this.x2 = x2;
- }
- public Color getColor() {
- return color;
- }
- public void setColor(Color color) {
- this.color = color;
- }
- }
- package draw.shapes;
- import java.awt.Color;
- import java.awt.Graphics;
- public class Square extends Shape {
- public Square(int x, int y, int x2, int y2, Color c) {
- super(x, y, x2, y2, c);
- // TODO Auto-generated constructor stub
- }
- @Override
- public void render(Graphics g) {
- g.setColor(getColor());
- g.fillRect(getX(), getY(), Wight(), Height());
- // TODO Auto-generated method stub
- }
- }
- //////////////////////////////////////
- package Menu;
- import javax.swing.JToolBar;
- public class MainToolbar extends JToolBar{
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public MainToolbar() {
- // TODO Auto-generated constructor stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement