Paint Program Assistance

问题描述 投票:0回答:1

我正在尝试创建一个绘画程序,允许用户单击特定的工具并将其绘制在画布上。每个工具都应像Microsoft Paint一样工作,用户可以通过拖动和释放来绘制形状,然后自由移动它们。

但是,我无法使用当前的代码绘制。我不知道问题是由什么引起的,因为我单击画布时没有弹出任何消息,所以我不知道我的工具是否已正确实现。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.awt.geom.*;

public class guiInterface {
    static int shapeCheck = 0;
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
}
private static void createAndShowGUI() {
    // create a new JFrame, which is a top-level window
    JFrame frame = new JFrame("Canvas Painter");
    // Tell the frame that the application should exit when we close it
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //sets up a menu bar
    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);
    //setus up the file aspect of the menu bar
    JMenu file = new JMenu("File");
    menuBar.add(file); 
    //adds the items for the file aspect
    JMenuItem newButton = new JMenuItem("New");
    file.add(newButton);
    JMenuItem delete = new JMenuItem("Delete");
    file.add(delete);
    JMenuItem quitButton = new JMenuItem("Quit");
    file.add(quitButton);
    //when clicked it will exit out of the program
    quitButton.addActionListener(e -> System.exit(0));
    //sets up the edit aspect of the menu bar
    JMenu edit = new JMenu("Edit");
    menuBar.add(edit);
    //sets up the view aspect of the menu bar
    JMenu view = new JMenu("View");
    menuBar.add(view);
    //adds the aspects to the view part of the program. 
    JMenuItem next = new JMenuItem("Next");
    view.add(next);
    JMenuItem previous = new JMenuItem("Previous");
    view.add(previous);
    //Left Hand Side tool pallette
    //if possible add the toggle buttons to a group
    JPanel buttons = new JPanel(new GridLayout(0, 1));
    ButtonGroup group = new ButtonGroup();
    JToggleButton select = new JToggleButton();
    select.setIcon(new ImageIcon("C:\\Users\\HamzaHamid\\Desktop\\cs4470\\swing-examples\\HW1\\selectbutton.png"));
    group.add(select);
    JToggleButton line = new JToggleButton();
    line.setIcon(new ImageIcon("C:\\Users\\HamzaHamid\\Desktop\\cs4470\\swing-examples\\HW1\\linebutton.png"));
    group.add(line);
    JToggleButton rectangle = new JToggleButton();
    rectangle.setIcon(new ImageIcon("C:\\Users\\HamzaHamid\\Desktop\\cs4470\\swing-examples\\HW1\\rectanglebutton.png"));
    group.add(rectangle);
    JToggleButton oval = new JToggleButton();
    oval.setIcon(new ImageIcon("C:\\Users\\HamzaHamid\\Desktop\\cs4470\\swing-examples\\HW1\\ovalbutton.png"));
    group.add(oval);
    JToggleButton pen = new JToggleButton();
    pen.setIcon(new ImageIcon("C:\\Users\\HamzaHamid\\Desktop\\cs4470\\swing-examples\\HW1\\rectanglebutton.png"));
    group.add(pen);
    JToggleButton text = new JToggleButton();
    text.setIcon(new ImageIcon("C:\\Users\\HamzaHamid\\Desktop\\cs4470\\swing-examples\\HW1\\textbutton.png"));
    group.add(text);

    //adds the button to a JPanel called buttons
    buttons.add(select);
    buttons.add(line);
    buttons.add(rectangle);
    buttons.add(oval);
    buttons.add(pen);
    buttons.add(text);

    //main content area
    //Container content = frame.getContentPane();
    //content.setLayout(new BorderLayout());
    final ContentArea canvas = new ContentArea(shapeCheck);
    //content.add(canvas, BorderLayout.CENTER);

    //status bar
    JLabel status = new JLabel();
    status.setText(" Status Bar");

    //This creates actions when each specific button is clicked.
    newButton.addActionListener(e -> status.setText(" New Button has been clicked"));
    delete.addActionListener(e -> status.setText(" Delete Button has been clicked"));
    next.addActionListener(e -> status.setText(" Next Button has been clicked"));
    previous.addActionListener(e -> status.setText(" Previous Button has been clicked"));
    select.addActionListener(e -> status.setText(" Select button has been clicked"));
    line.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            status.setText(" Line button has been clicked");
            shapeCheck = 1;
        }
    });
    rectangle.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            status.setText(" Rectangle button has been clicked");
            shapeCheck = 2;
        }
    });
    oval.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            status.setText(" Ellipse button has been clicked");
            shapeCheck = 3;
        }
    });
    pen.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            status.setText(" Pen button has been clicked");
            shapeCheck = 4;
        }
    });
    text.addActionListener(e -> status.setText(" Text Button has been clicked"));

    //Extra Credit 
    //Color chooser
    JToggleButton color1 = new JToggleButton();
    color1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Color newColor = JColorChooser.showDialog(null, "Choose", color1.getBackground());
            if(newColor != null){
                color1.setBackground(newColor);
            }
        }
    });
    //this pretty much says that a color has been chosen
    color1.addActionListener(e -> status.setText(" Color 1 Button has been clicked"));
    //adds the color button to the group
    //which is then added to the buttons JPanel
    group.add(color1);
    buttons.add(color1);
    //creates a pannel of the buttons, and other things
    JPanel left = new JPanel(new BorderLayout());
    left.add(buttons, BorderLayout.NORTH);
    frame.add(left, BorderLayout.WEST);
    frame.add(canvas, BorderLayout.EAST);
    //frame.add(content, BorderLayout.EAST);
    frame.add(status, BorderLayout.SOUTH);

    // pack() causes the size of the frame to be set just large enough to contain its
    // children; setVisible(true) puts it on the screen
    frame.pack();
    frame.setVisible(true);
    }
}
//creates the content area and places it in the text area
class ContentArea extends JComponent {
      int oldX = 0;
     int oldY = 0;
   int currentX = 0;
int currentY = 0;
ArrayList<Shape> variousShapes = new ArrayList<Shape>();
Shape theShape;
public ContentArea(int shapeCheck){
    int shapeNum = shapeCheck;
    setDoubleBuffered(false);
    addMouseListener(new MouseAdapter(){
        public void mousePressed(MouseEvent e){
            oldX = e.getX();
            oldY = e.getY();
            currentX = oldX;
            currentY = oldY;
            repaint();
        }
        public void mouseReleased(MouseEvent e) {
            switch(shapeCheck) {
                case 1: 
                    theShape = drawLine(oldX, oldY, currentX, currentY);
                case 2: 
                    theShape = drawRectangle(oldX, oldY, e.getX(), e.getY());
                case 3:
                    theShape = drawEllipse(oldX, oldY, e.getX(), e.getY());
                default:
                    theShape = null;

            }
            variousShapes.add(theShape);
            oldX = 0;
            oldY = 0;
            currentY = 0;
            currentX = 0;
            repaint();
        }
    });
    //if the mouse is pressed it sets the oldX & oldY
    //coordinates as the mouses x & y coordinates
    addMouseMotionListener(new MouseMotionAdapter(){
        public void mouseDragged(MouseEvent e){
            if (shapeCheck == 4) {
                theShape = drawLine(oldX, oldY, currentX, currentY);
            }
            repaint();
        }

    });
}

public void paintComponent(Graphics g){
    Graphics2D graphics1 = (Graphics2D) g;
    super.paintComponent(g);
    graphics1 = (Graphics2D) g;
    graphics1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    for (Shape s : variousShapes) {
        graphics1.draw(s);
    }
}
private Ellipse2D.Double drawEllipse (int x1, int y1, int x2, int y2) {
    int x = Math.min(x1, x2);
    int y = Math.min(y1, y2);
    int width = Math.abs(x2 - x1);
    int height = Math.abs(y2 - y1);
    return new Ellipse2D.Double(x, y, width, height);
}
private Rectangle2D.Double drawRectangle (int x1, int y1, int x2, int y2) {
    int x = Math.min(x1, x2);
    int y = Math.min(y1, y2);
    int width = Math.abs(x2 - x1);
    int height = Math.abs(y2 - y1);
    return new Rectangle2D.Double(x, y, width, height);
}
private Line2D.Double drawLine (int x1, int y1, int x2, int y2) {
    return new Line2D.Double(x1, y1, x2, y2);
}
java swing paint
1个回答
0
投票

我发现了一些问题。首先,我认为您的画布不可见。通过以下方式将其添加到中心:

frame.add(canvas, BorderLayout.CENTER);

接下来,将shapeCheck传递给ContentArea构造函数。这是shapeCheck的副本,但ContentArea需要读取guiInterface.shapeCheck中的实际变量。

最后,您的switch语句需要break语句,否则它将始终落入default并设置theShape = null;

public void mouseReleased(MouseEvent e) {
    switch (guiInterface.shapeCheck) {
        case 1:
            theShape = drawLine(oldX, oldY, e.getX(), e.getY());
            break;
        case 2:
            theShape = drawRectangle(oldX, oldY, e.getX(), e.getY());
            break;
        case 3:
            theShape = drawEllipse(oldX, oldY, e.getX(), e.getY());
            break;
        default:
            theShape = null;
            break;
    }
    if (null != theShape) {
        variousShapes.add(theShape);
    }
    oldX = 0;
    oldY = 0;
    currentY = 0;
    currentX = 0;
    repaint();
}

以及绘图工具的修复程序:

addMouseMotionListener(new MouseMotionAdapter() {
    public void mouseDragged(MouseEvent e) {
        if (delete_me.shapeCheck == 4) {
            variousShapes.add(drawLine(oldX, oldY, e.getX(), e.getY()));
            oldX = e.getX();
            oldY = e.getY();
            repaint();
        }               
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.