(drawRect)为什么单击撤消按钮后总是有一个点?

时间:2017-07-29 18:07:30

标签: java swing awt

您好我是这里的新手,也是Java编程的新手。最近我试图在JPanel中编写绘图函数。问题是,当我添加新的drawRect时(只需在JRadiobutton中选择draw Rect然后在白色空间上拖动)它工作正常但是当我按下undo按钮时,原点上有一个点。绘制椭圆和绘制折线工作正常。谁能帮我吗?

(抱歉我的英语很差,如果你不能得到我的问题,我很抱歉你可以尝试运行代码 - >绘制矩形 - >点击撤消 - >在原点得到了dot(这是我试图解释的问题))

package com.jetbrains;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.BufferedImage;

public class Main
{
private BufferedImage buffPNG = new BufferedImage(900,550,BufferedImage.TYPE_INT_ARGB);

private Main()
{
    draw_Image img = new draw_Image();

    JFrame frame = new JFrame("Ken's Paint");
    JButton undo_Button = new JButton("Undo");
    JRadioButton rb1 = new JRadioButton("Free Hand");
    JRadioButton rb2 = new JRadioButton("Draw Rect");
    JRadioButton rb3 = new JRadioButton("Draw Oval");
    JMenuBar menu_Bar = new JMenuBar();

    undo_Button.setContentAreaFilled(false);
    undo_Button.setFocusPainted(false);
    ButtonGroup mode_Group = new ButtonGroup();
    mode_Group.add(rb1);
    mode_Group.add(rb2);
    mode_Group.add(rb3);
    img.setDraw_Mode(1);
    rb1.setSelected(true);

    menu_Bar.add(rb1);
    menu_Bar.add(rb2);
    menu_Bar.add(rb3);
    menu_Bar.add(undo_Button);
    frameCreate(frame);
    frame.setJMenuBar(menu_Bar);
    frame.add(img);

    rb1.addActionListener(e -> img.setDraw_Mode(1));
    rb2.addActionListener(e -> img.setDraw_Mode(2));
    rb3.addActionListener(e -> img.setDraw_Mode(3));
    undo_Button.addActionListener(e -> img.undo_Pressed());
}

class draw_Image extends JPanel
{
    int layerX = 0;
    int layerY = 0;
    int[] polyX = new int[3000];
    int[] polyY = new int[3000];
    int[] thick = new int[10000];
    int[] nPoint = new int[10000];
    int[] draw_Mode = new int[10000];
    int[][] x = new int[10000][3000];
    int[][] y = new int[10000][3000];
    Color[] color = new Color[10000];
    boolean dragged = false;

    draw_Image() 
    {
        setLayout(null);
        setBounds(0,0,900,550);
        setBackground(Color.lightGray);
        for(int p = 0; p < 10000; p++)
        {
            draw_Mode[p] = 1;
            thick[p] = 3;
            color[p] = Color.black;
        }

        addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent e) {super.mouseReleased(e);}

            @Override
            public void mouseReleased(MouseEvent e)
            {
                super.mouseReleased(e);
                if(dragged)
                {
                    layerX++;
                    dragged = false;
                }
                layerY = 0;
            }
        });

        addMouseMotionListener(new MouseMotionListener()
        {
            @Override
            public void mouseDragged(MouseEvent e)
            {
                dragged = true;
                for(int k = layerY; k < 3000; k++)
                {
                    x[layerX][k] = e.getX();
                    y[layerX][k] = e.getY();
                }
                nPoint[layerX]++;
                layerY++;
                repaint();
            }

            @Override
            public void mouseMoved(MouseEvent e){}
        });
    }

    @Override
    public void paint(Graphics g)
    {
        super.paint(g);
        Graphics2D g2 = (Graphics2D)g;
        Graphics2D g3 = buffPNG.createGraphics();
        g2.setColor(Color.white);
        g2.fillRect(0,0,900,550);
        g3.setColor(Color.white);
        g3.fillRect(0,0,900,550);

        /* Draw the image in polyline form (implemented multidimensional array (2D is used)) */
        for(int i = 0; i <= layerX; i++)
        {
            for(int j = 0; j < 3000; j++)
            {
                polyX[j] = x[i][j];
                polyY[j] = y[i][j];
            }

            g2.setColor(color[i]);           /* Set the line color (g2 is for display) */
            g3.setColor(color[i]);           /* Set the line color (g3 is for buffered image) */
            g2.setStroke(new BasicStroke(thick[i])); /* Set line thickness (g2 is for display) */
            g3.setStroke(new BasicStroke(thick[i])); /* Set line thickness (g3 is for buffered image) */

            if(draw_Mode[i] == 1) /* free hand */
            {
                g2.drawPolyline(polyX,polyY,nPoint[i]);
                g3.drawPolyline(polyX,polyY,nPoint[i]);
            }

            else if(draw_Mode[i] == 2) /* draw rect */
            {
                g2.drawRect(polyX[0],polyY[0],(polyX[2999] - polyX[0]),(polyY[2999] - polyY[0]));
                g3.drawRect(polyX[0],polyY[0],(polyX[2999] - polyX[0]),(polyY[2999] - polyY[0]));
            }

            else if(draw_Mode[i] == 3) /* draw oval */
            {
                g2.drawOval(polyX[0],polyY[0],(polyX[2999] - polyX[0]),(polyY[2999] - polyY[0]));
                g3.drawOval(polyX[0],polyY[0],(polyX[2999] - polyX[0]),(polyY[2999] - polyY[0]));
            }
        }
    }

    void setDraw_Mode(int mode) /* Method to set draw mode */
    {
        for(int q = layerX; q < 10000; q++)
        {
            draw_Mode[q] = mode;
        }
    }


    void undo_Pressed() /* Undo an action / Return to previous line drawing */
    {
        if(layerX > 0)layerX--;
        for(int j = 0; j < 3000; j++)
        {
            x[layerX][j] = 0;
            y[layerX][j] = 0;
        }
        nPoint[layerX] = 0;
        setDraw_Mode(draw_Mode[layerX+1]);
        repaint();
    }
}

private void frameCreate(JFrame frame)
{
    frame.pack();
    Insets insetValue = frame.getInsets();
    int height = insetValue.top + insetValue.bottom + 600 - 10;
    int width = insetValue.left + insetValue.right + 900 - 10;
    frame.setSize(width,height);                                    /* Set the frame size */
    frame.setLocation(195,50);                                      /* Set the frame start up location */
    frame.setResizable(false);                                      /* Disable frame resize & full window option */
    frame.setLayout(null);                                          /* Set the layout to null */
    frame.setVisible(true);                                         /* Set the frame visible */
    frame.getContentPane().setBackground(Color.white);              /* Set the frame background color */
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  /* Specify the default behavior upon closing */
}

public static void main(String[] args)
{
    new Main();
}
}

1 个答案:

答案 0 :(得分:1)

无法解决您的问题,但有些常规提示:

class draw_Image extends JPanel
  1. 班级名称应以大写字母
  2. 开头
  3. 请勿在班级名称中使用“_”。
  4. 使用API​​作为类命名约定的指南。

    int[] polyX = new int[3000];
    int[] polyY = new int[3000];
    int[] thick = new int[10000];
    int[] nPoint = new int[10000];
    

    不要在整个程序中使用硬编码。至少使用变量:

    private static int SMALLER_SIZE = 3000;
    private static int LARGER_SIZE = 10000;
    ...
    int[] polyX = new int[SMALLER_SIZE];
    int[] polyY = new int[SMALLER_SIZE];
    int[] thick = new int[LARGER_SIZE];
    int[] nPoint = new int[LARGER_SIZE];
    

    更好的是,不要使用数组。而是使用ArrayList。那么你不需要为数组的大小选择一些随机大数。

    int[] polyX = new int[SMALLER_SIZE];
    int[] polyY = new int[SMALLER_SIZE];
    

    不要保留两个数组。如果数据是相关的,则数据应存储在对象中。在上面的例子中,您可以使用属于JDK的Point对象。因此,使用ArrayList(而不是数组)的代码将类似于:

    private ArrayList<Point> points = new ArrayList<Point>();
    

    然后使用以下代码将Point对象添加到ArrayList:

    points.add( new Point(...) );
    

    现在您的循环代码没有硬编码值:

    for (int i = 0; i < points.getSize(); I++
    {
        Point p = points.get(i);
        // do something with the Point
    }
    

    一些绘画技巧:

    public void paint(Graphics g)
    

    不要覆盖油漆(...)。自定义绘画是通过覆盖paintComponent(...)

    完成的
        g2.setColor(Color.white);
        g2.fillRect(0,0,900,550);
        g3.setColor(Color.white);
        g3.fillRect(0,0,900,550);
    

    不要硬编码尺寸。你不知道屏幕尺寸是多少。相反,当您创建组件时,您只需执行以下操作:

    DrawImage panel= new DrawImage();
    panel.setBackground(Color.WHITE);
    

    现在调用super.paintComponent(...)时,面板的背景将被涂成白色。

    我也不知道你为什么使用透明的BufferedImage。只需直接在面板上绘画。

    如果您真的想知道如何在面板上的随机位置绘制矩形,那么您可以查看Custom Painting Approaches。它演示了两种常见的方法。您可能会使用Draw On Component示例,因为它允许您“撤消”绘制矩形。当然你需要实现这个逻辑,但是如果在ArrayList中保留所有信息,这很简单就是删除列表中的最后一项。