Hello I'm new here and also new to Java programming. Recently I'm trying to code a drawing function in JPanel. The problem is, when I add new drawRect (just select draw Rect in the JRadiobutton then drag on the white space) it works fine but when I press undo button there is a dot at the origin. Draw oval and draw polyline works fine. Can anyone help me out?
(Sorry for my poor english, if you don't get my question I'm sorry you can try running the code -> draw rectangles -> click undo -> at the origin got a dot (that's the problem I'm trying to explain))
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();
}
}