Итак, моя цель - открыть окно, в котором вы можете нарисовать несколько линий на белом фоне, просто нажав. Проблема в том, что когда он пытается сохранить его, он всегда возвращается в виде png, но в виде квадратного изображения. Если я рисую треугольник своими линиями, я получаю треугольник внутри белого квадрата, но я хочу только треугольник. Я был бы очень признателен за любую помощь
Я испробовал каждое решение, которое натолкнулось на stackoverflow, и я попытался понять Graphics2D подробно, но, к сожалению, не смог
public class Draw{
public static void main(String[] args){
Icon iconB = new ImageIcon("blue.gif");
Icon iconM = new ImageIcon("magenta.gif");
Icon iconR = new ImageIcon("red.gif");
Icon iconBl = new ImageIcon("black.gif");
Icon iconG = new ImageIcon("green.gif");
JFrame frame = new JFrame("Paint It");
//Creates a frame with a title of "Paint it"
Container content = frame.getContentPane();
//Creates a new container
content.setLayout(new BorderLayout());
//sets the layout
final PadDraw drawPad = new PadDraw();
//creates a new padDraw, which is pretty much the paint program
content.add(drawPad, BorderLayout.CENTER);
//sets the padDraw in the center
JPanel panel = new JPanel();
panel.setOpaque(false);
//creates a JPanel
panel.setPreferredSize(new Dimension(32, 68));
panel.setMinimumSize(new Dimension(32, 68));
panel.setMaximumSize(new Dimension(32, 68));
//This sets the size of the
content.add(panel, BorderLayout.SOUTH);
//sets the panel to the left
frame.setSize(480, 360);
//sets the size of the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//makes it so you can close
frame.setVisible(true);
//makes it so you can see it
}
}
class PadDraw extends JComponent{
Image image;
//this is gonna be your image that you draw on
Graphics2D graphics2D;
//this is what we'll be using to draw on
int currentX, currentY, oldX, oldY;
//these are gonna hold our mouse coordinates
int firstX;
int firstY;
//Now for the constructors
//will draw from tail to head
public PadDraw(){
setDoubleBuffered(false);
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
if (currentX == 0 && currentY == 0) {
firstX= e.getX();
firstY = e.getY();
oldX = e.getX();
oldY = e.getY();
}
currentX = e.getX();
currentY = e.getY();
graphics2D.drawLine(oldX, oldY, currentX, currentY);
repaint();
oldX = currentX;
oldY = currentY;
}
});
}
public void paintComponent(Graphics g){
if(image == null){
image = createImage(getSize().width, getSize().height);
graphics2D = (Graphics2D) image.getGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.setComposite(AlphaComposite.Clear);
graphics2D.fillRect(0, 0, getSize().width, getSize().height);
graphics2D.setComposite(AlphaComposite.Src);
clear();
}
g.drawImage(image, 0, 0, null);
}
//this is the painting bit
//if it has nothing on it then
//it creates an image the size of the window
//sets the value of Graphics as the image
//sets the rendering
//runs the clear() method
//then it draws the image
public void clear(){
currentX = 0;
currentY = 0;
graphics2D.setPaint(Color.white);
graphics2D.fillRect(0, 0, getSize().width, getSize().height);
graphics2D.setPaint(Color.black);
repaint();
}
//this is the clear
//it sets the colors as white
//then it fills the window with white
//thin it sets the color back to black
public void save(){
repaint();
if (currentX != 0 && currentY != 0) {
graphics2D.drawLine(oldX, oldY, firstX, firstY);
currentX = 0;
currentY = 0;
}
try {
BufferedImage bfrdImage = new BufferedImage
(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
Graphics2D bGr = bfrdImage.createGraphics();
bGr.setComposite(AlphaComposite.Clear);
bGr.fillRect(0, 0, getSize().width, getSize().height);
bGr.setComposite(AlphaComposite.Src);
bGr.drawImage(image, 0, 0, null);
javax.imageio.ImageIO.write(bfrdImage, "PNG", new File("Drawing.PNG"));
bGr.dispose();
} catch (Exception ex) {
Logger.getLogger(PadDraw.class.getName()).log(Level.SEVERE, null, ex);
}
}
//saves and also comes back to the first point to finalize the shape}