(对不起,我的英语很差,如果你没有理解我的问题,我很抱歉,你可以尝试运行代码->绘制矩形->单击撤消->在原点处得到一个点(这是我试图解释的问题))
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];
int[] polyX = new int[SMALLER_SIZE];
int[] polyY = new int[SMALLER_SIZE];
private ArrayList<Point> points = new ArrayList<Point>();
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);
编辑:底部的解决方案 这是一个跳棋游戏。单击一个按钮后,它等待单击第二个按钮与之交换。然而,有时你可能不想移动那个按钮,但一旦你点击了它,就没有回头路了,因为我无法禁用它。 在这里的其他帖子中,我看到人们使用 这只是使它在第一次单击后不可见。 这什么都干不了。 这也没什么用。编辑:所有这些方法都用true和false进行了尝试。 私有无效交换(){ 但你也需要 这样它就重新启用了它,或者其他什么,
我正在尝试网络抓取,我需要模拟对buttoN的点击,我已经尝试过了: 并返回此错误: selenium . common . exceptions . nosuchelementexception:消息:没有这样的元素:找不到元素:{"method":"xpath "," selector ":"//*[@ id = " CTL 00 _ CP h1 _ BtnTipoGobierno "]" }
在中的一个考试制造者,在您回答完问题后,将弹出一个并询问您是否要重考,我单击否,程序仍在运行。
我使用的是:
在我的程序中,它将单击浏览器中的一个按钮,并且在该页面中,应该会出现另一个按钮。出现该按钮后,我的程序将立即运行下一个操作来单击下一个按钮。我目前收到此错误: ElementNotVisibleException:消息:元素不可见 因此,我假设我正在调用该操作,以便在该按钮出现之前单击下一个按钮。我的问题是,我该怎么做才能让我的程序等到我可以点击按钮,再点击按钮? 这就是我的程序底部的代码的样子。
我一直在自己编程一个应用程序,但每次我点击一个按钮,它就会遇到一些问题(因为问题出在它的OnCreate方法上)。我把它缩小到我认为是什么原因,似乎是这条线: