- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
你好,我是新来的,也是 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
使用 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/
我试图弄清楚如何获取所有可见窗口的框架。我尝试了以下代码,但它仅适用于应用程序本身,其他窗口报告 {0,0,0,0} NSArray *windowArray = [NSWindow wind
左矩形=新矩形(0,0,WIDTH/9,HEIGHT);在此代码中,如果我增加宽度,为什么它看起来会进一步延伸到 JFrame 的左侧?如果我减少它为什么它会向右延伸?这不是像坐标平原一样工作吗?高度
假设我有一个 AffineTransform (transform) 并调用它的一堆方法。假设我旋转并平移它。然后我用它转换图形对象 (g2d): g2d.transform(transform);
我认为当你对物体施加力时,它会施加到 body 的原点(可能是质心)。现在我正在尝试创建类似俄罗斯方 block 的 block ,并通过应用 LinearPulse 使它们跳跃,如下所示: body
我正在阅读 Big Nerd Ranch 指南第 4 版,但在处理 View 和 View 层次结构的章节中,我遇到了一些与 View 来源有关的问题。 我已经在我的 View Controller
我想把原点放在中心,所以我做了: SetViewportOrgEx(hdc,width/2,height/2,NULL); (如下面的代码所示) 现在,在实现双缓冲后它似乎无法正常工作。任何想法为什么
当我的 Sprite 旋转原点固定在窗口的左上角时,我遇到了这个问题(与 sprite.Draw 和 sprite.Draw2D 相同)无论哪种方式,如果我改变旋转中心它仍然在左上角。我需要 Spri
有人告诉我,您可以通过以下方式向与当前 Git 分支名称相匹配的远程分支推送和 pull : git push origin HEAD 或 git pull origin HEAD 它以前一直对我有用
我正在使用自定义 View 和 Canvas 在屏幕上绘制对象。 View 位于距屏幕原点的 Y 偏移处。我想平移 Canvas ,然后绘制一个对象。但是,当我使用 setmatrix 平移 Canv
尝试拖动组。为什么 origin 在这里不起作用?注意到当你第一次点击它时它是如何跳跃的吗? JSFIDDLE基于此:http://bl.ocks.org/mbostock/1557377 var d
我正在尝试使用以下立方体的透视视角: http://jsfiddle.net/TrySpace/JvFSQ/5/ 但是我没有按照我的预期去做,我想要改变实际的视角。所以当transformOrigin
我的 OpenShift Origin 安装似乎有问题。 当我获得路由器的端点时,我得到以下信息: oc get endpoints --namespace=default --selector=ro
这是怎么做到的?我正在寻找 iOS7/8 解决方案。 KeyboardWillShow 并不令人满意,因为我需要在键盘实际显示之前根据键盘高度调整 View 大小。 最佳答案 keyboardWill
我正在 iframe 上监听其内容何时发生变化。当它发生时,我想知道内容的当前来源是什么。 我无法访问该内容,因为它违反了相同的域策略,但我只要根据网址或主机知道它显示的内容就可以了。 我可以通过 j
我一直致力于使用 CSS3 创建动画条形图。它在钢筋的两侧运行良好,但钢筋的顶部和底部一直存在问题。 我一直在通过 jQuery 改变它们的 css“高度”属性来缩放边,但我意识到这不是最好的方法。我
Edit3:我的问题与我预期的功能完全不同。我让代码保留下来,也许这对某人有帮助 :)(并且不要忘记调试!)。 我试图找到直线与三角形相交的 vector 。 当前状态:随机交叉,即使鼠标不在地板上和
我需要在原始 View 的坐标中计算 UIView subview 的可见 CGRect。如果比例为 1,我可以正常工作,但如果其中一个 super View 或 View 本身被缩放(收缩),可见的
这个问题类似于以前回答的问题 Fast interpolation over 3D array ,但无法解决我的问题。 我有一个维度为(时间、高度、纬度、经度)的 4D 数组,标记为 y.shape=
我正在制作自定义贴纸包。这是一个 iMessage 扩展应用程序。我继承了 UICollectionView 而不是使用基本的 MSMessagesViewController。因此,只有当我在展开
我使用 CGWindowListCopyWindowInfo 获取所有窗口的列表。它根据屏幕的 左上角 原点为我提供每个窗口的坐标。 如果我使用 NSWindow 的 setFrame 方法,坐标基于
我是一名优秀的程序员,十分优秀!