gpt4 book ai didi

java - (drawRect) 为什么点击撤销按钮后原点总是有一个点?

转载 作者:行者123 更新时间:2023-12-02 12:28:14 28 4
gpt4 key购买 nike

你好,我是新来的,也是 Java 编程的新手。最近我正在尝试在 JPanel 中编写绘图功能。问题是,当我添加新的绘制矩形(只需在 JRadiobutton 中选择绘制矩形,然后拖动空白区域)时,它工作正常,但是当我按下撤消按钮时,原点处有一个点。绘制椭圆形和绘制折线效果很好。谁能帮我吗?

(抱歉我的英语不好,如果你没有明白我的问题,我很抱歉你可以尝试运行代码 -> 绘制矩形 -> 单击撤消 -> 在原点有一个点(这就是我的问题)我试图解释))

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();
}
}

最佳答案

不会解决您的问题,但有一些一般性提示:

class draw_Image extends JPanel
  1. 类名应以大写字符开头
  2. 不要在类名中使用“_”。

使用 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)

不要覆盖paint(...)。自定义绘制是通过重写 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 中的所有信息,这将很简单,只需删除列表中的最后一项即可。

关于java - (drawRect) 为什么点击撤销按钮后原点总是有一个点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45392453/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com