07
Авг
2017

Поле для рисование (графический редактор) java

Пишу графический редактор на java. Не могу понять, как сделать то поле, на котором все будет происходить. Понятно, что все используют Graphics, Graphics2D, но я вообще не понимаю, как его включить и куда его приставить в моем коде. Пытался делать как у других, но у меня не получалось

Вот код, строчек многовато, но по существу мало:

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.text.AttributedCharacterIterator;
import java.util.LinkedHashMap;

import static java.awt.Font.MONOSPACED;
import static java.awt.image.BufferedImage.TYPE_INT_RGB;


public class GraphicsEditor extends JFrame {
    File file; // Our selected file

    BufferedImage f = new BufferedImage(600, 300, TYPE_INT_RGB);
    Graphics2D g = f.createGraphics();

    private ActionListener open = new ActionListener() { // Opens a file
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            JFileChooser chooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter(
                    "JPG & GIF Images", "jpg", "gif", "png");
            chooser.setFileFilter(filter);
            if(JFileChooser.APPROVE_OPTION ==
                    (chooser.showDialog(null, "Open"))) {

                file = chooser.getSelectedFile();
            }
        }
    };

    private ActionListener save = new ActionListener() { // Saves a file
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            JFileChooser chooser = new JFileChooser();
            File file = null;
            int r = chooser.showSaveDialog(null);
           /* if(JFileChooser.APPROVE_OPTION ==
                    (chooser.showSaveDialog(null)))*/


        }
    };

   /* Remove the full paths to the icons sometime*/

   ImageIcon pencilIcon = new ImageIcon("D:\\java\\ideaprojects\\graphics editor\\Icons\\Pencil.png");
   ImageIcon eraserIcon = new ImageIcon("D:\\java\\ideaprojects\\graphics editor\\Icons\\Eraser.png");
   ImageIcon textIcon = new ImageIcon("D:\\java\\ideaprojects\\graphics editor\\Icons\\Text.png");
   ImageIcon sizeIcon1 = new ImageIcon("D:\\java\\ideaprojects\\graphics editor\\Icons\\1.png");
   ImageIcon sizeIcon2 = new ImageIcon("D:\\java\\ideaprojects\\graphics editor\\Icons\\2.png");
   ImageIcon sizeIcon3 = new ImageIcon("D:\\java\\ideaprojects\\graphics editor\\Icons\\3.png");
   ImageIcon whiteIcon = new ImageIcon("D:\\java\\ideaprojects\\graphics editor\\Icons\\white.png");
   ImageIcon blackIcon = new ImageIcon("D:\\java\\ideaprojects\\graphics editor\\Icons\\black.png");
   ImageIcon redIcon = new ImageIcon("D:\\java\\ideaprojects\\graphics editor\\Icons\\red.png");
   ImageIcon greenIcon = new ImageIcon("D:\\java\\ideaprojects\\graphics editor\\Icons\\green.png");
   ImageIcon yellowIcon = new ImageIcon("D:\\java\\ideaprojects\\graphics editor\\Icons\\yellow.png");
   ImageIcon blueIcon = new ImageIcon("D:\\java\\ideaprojects\\graphics editor\\Icons\\blue.png");

   JButton pencil = new JButton(pencilIcon);
   JButton eraser = new JButton(eraserIcon);
   JButton text = new JButton(textIcon);
   JButton size1 = new JButton(sizeIcon1);
   JButton size2 = new JButton(sizeIcon2);
   JButton size3 = new JButton(sizeIcon3);
   JButton white = new JButton(whiteIcon);
   JButton black = new JButton(blackIcon);
   JButton red = new JButton(redIcon);
   JButton green = new JButton(greenIcon);
   JButton yellow = new JButton(yellowIcon);
   JButton blue = new JButton(blueIcon);



    private JMenu[] menus = {
            new JMenu("File")/*, new JMenu("Tools"),
            new JMenu("Size")*/
    };

    private JMenuItem[] items = {
            new JMenuItem("Open"), new JMenuItem("Save"),
            new JMenuItem("Save as")/*, new JMenuItem("Pencil"),
            new JMenuItem("Eraser"), new JMenuItem("Text"),
            new JMenuItem("1"), new JMenuItem("2"),
            new JMenuItem("2")*/
    };

    public GraphicsEditor() {
        for(int i = 0; i < items.length; i++)
            menus[i/3].add(items[i]);

        JMenuBar mb = new JMenuBar();
        for(JMenu jm : menus)
            mb.add(jm);
        setJMenuBar(mb);

        Dimension colours = new Dimension(15, 15);
        Dimension sizes = new Dimension(20, 20);
        size1.setPreferredSize(sizes);
        size2.setPreferredSize(sizes);
        size3.setPreferredSize(sizes);
        pencil.setPreferredSize(colours);
        eraser.setPreferredSize(colours);
        text.setPreferredSize(colours);
        white.setPreferredSize(colours);
        black.setPreferredSize(colours);
        red.setPreferredSize(colours);
        green.setPreferredSize(colours);
        yellow.setPreferredSize(colours);
        blue.setPreferredSize(colours);


        add(pencil);
        add(eraser);
        add(text);
        add(white);
        add(black);
        add(red);
        add(green);
        add(yellow);
        add(blue);
        add(size1);
        add(size2);
        add(size3);

        g.setBackground(Color.BLACK);

        items[0].addActionListener(open);  // open a file
        items[1].addActionListener(save); // save the file

        setLayout(new FlowLayout());

    }

    public static void main(String[] args) {
        Swing.go(new GraphicsEditor(), 750, 550);
    }
}

Источник: https://ru.stackoverflow.com/questions/702856/%D0%9F%D0%BE%D0%BB%D0%B5-%D0%B4%D0%BB%D1%8F-%D1%80%D0%B8%D1%81%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5-%D0%B3%D1%80%D0%B0%D1%84%D0%B8%D1%87%D0%B5%D1%81%D0%BA%D0%B8%D0%B9-%D1%80%D0%B5%D0%B4%D0%B0%D0%BA%D1%82%D0%BE%D1%80-java

Тебе может это понравится...

Добавить комментарий