0% found this document useful (0 votes)
4 views9 pages

M11 Java Paint App Session 1

The document describes the implementation of a Java Paint application using Swing. It includes features such as various drawing tools (pencil, rectangle, oval, arc, eraser), a color selection panel, and the ability to clear the canvas. The application is structured with a main class, a drawing panel, and methods to handle user interactions and drawing actions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

M11 Java Paint App Session 1

The document describes the implementation of a Java Paint application using Swing. It includes features such as various drawing tools (pencil, rectangle, oval, arc, eraser), a color selection panel, and the ability to clear the canvas. The application is structured with a main class, a drawing panel, and methods to handle user interactions and drawing actions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

M11 Java Paint App Session 1

Aaron Hernandez Jimenez


23 may 2025 ​

Group 131

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

public class PaintApp extends JFrame {


private DrawingPanel drawingPanel;
private JPanel toolPanel;
private JPanel colorPanel;
private ButtonGroup toolGroup;
private Color currentColor = Color.BLACK;
private String currentTool = "PENCIL";

// Predefined colors
private final Color[] colors = {
Color.BLACK, Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW,
Color.ORANGE, Color.PINK, Color.CYAN, Color.MAGENTA,
Color.GRAY
};

public PaintApp() {
setTitle("Paint Application");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());

initializeComponents();
setupLayout();

setSize(800, 600);
setLocationRelativeTo(null);
setVisible(true);
}

private void initializeComponents() {


// Initialize drawing panel
drawingPanel = new DrawingPanel();

// Initialize tool panel


toolPanel = new JPanel();
toolPanel.setLayout(new FlowLayout());
toolPanel.setBackground(Color.LIGHT_GRAY);

// Initialize color panel


colorPanel = new JPanel();
colorPanel.setLayout(new FlowLayout());
colorPanel.setBackground(Color.LIGHT_GRAY);

// Create tool buttons


createToolButtons();

// Create color buttons


createColorButtons();
}

private void createToolButtons() {


toolGroup = new ButtonGroup();
// Pencil tool
JToggleButton pencilBtn = new JToggleButton("Pencil");
pencilBtn.setSelected(true);
pencilBtn.addActionListener(e -> currentTool = "PENCIL");
toolGroup.add(pencilBtn);
toolPanel.add(pencilBtn);

// Rectangle tool
JToggleButton rectangleBtn = new JToggleButton("Rectangle");
rectangleBtn.addActionListener(e -> currentTool = "RECTANGLE");
toolGroup.add(rectangleBtn);
toolPanel.add(rectangleBtn);

// Oval tool
JToggleButton ovalBtn = new JToggleButton("Oval");
ovalBtn.addActionListener(e -> currentTool = "OVAL");
toolGroup.add(ovalBtn);
toolPanel.add(ovalBtn);

// Arc tool
JToggleButton arcBtn = new JToggleButton("Arc");
arcBtn.addActionListener(e -> currentTool = "ARC");
toolGroup.add(arcBtn);
toolPanel.add(arcBtn);

// Eraser tool
JToggleButton eraserBtn = new JToggleButton("Eraser");
eraserBtn.addActionListener(e -> currentTool = "ERASER");
toolGroup.add(eraserBtn);
toolPanel.add(eraserBtn);

// Clear button
JButton clearBtn = new JButton("Clear");
clearBtn.addActionListener(e -> drawingPanel.clearCanvas());
toolPanel.add(clearBtn);
}

private void createColorButtons() {


for (Color color : colors) {
JButton colorBtn = new JButton();
colorBtn.setBackground(color);
colorBtn.setPreferredSize(new Dimension(30, 30));
colorBtn.addActionListener(e -> currentColor = color);
colorBtn.setBorder(BorderFactory.createRaisedBevelBorder());
colorPanel.add(colorBtn);
}
}

private void setupLayout() {


JPanel topPanel = new JPanel(new BorderLayout());
topPanel.add(toolPanel, BorderLayout.NORTH);
topPanel.add(colorPanel, BorderLayout.SOUTH);

add(topPanel, BorderLayout.NORTH);
add(new JScrollPane(drawingPanel), BorderLayout.CENTER);
}

// Inner class for the drawing panel


private class DrawingPanel extends JPanel {
private BufferedImage canvas;
private Graphics2D g2d;
private Point startPoint;
private Point endPoint;
private boolean isDrawing = false;
private List<DrawingAction> drawingActions;

public DrawingPanel() {
setPreferredSize(new Dimension(800, 600));
setBackground(Color.WHITE);

drawingActions = new ArrayList<>();

// Initialize canvas
canvas = new BufferedImage(800, 600,
BufferedImage.TYPE_INT_RGB);
g2d = canvas.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, 800, 600);

addMouseListeners();
}

private void addMouseListeners() {


addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
startPoint = e.getPoint();
isDrawing = true;

if (currentTool.equals("PENCIL") ||
currentTool.equals("ERASER")) {
drawPoint(e.getPoint());
}
}

@Override
public void mouseReleased(MouseEvent e) {
if (isDrawing) {
endPoint = e.getPoint();

switch (currentTool) {
case "RECTANGLE":
drawRectangle();
break;
case "OVAL":
drawOval();
break;
case "ARC":
drawArc();
break;
}

isDrawing = false;
repaint();
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (isDrawing && (currentTool.equals("PENCIL") ||
currentTool.equals("ERASER"))) {
drawLine(startPoint, e.getPoint());
startPoint = e.getPoint();
repaint();
}
}
});
}

private void drawPoint(Point point) {


g2d.setColor(currentTool.equals("ERASER") ? Color.WHITE :
currentColor);
g2d.setStroke(new BasicStroke(currentTool.equals("ERASER") ?
10 : 2));
g2d.drawOval(point.x - 1, point.y - 1, 2, 2);
}

private void drawLine(Point start, Point end) {


g2d.setColor(currentTool.equals("ERASER") ? Color.WHITE :
currentColor);
g2d.setStroke(new BasicStroke(currentTool.equals("ERASER") ?
10 : 2));
g2d.drawLine(start.x, start.y, end.x, end.y);
}

private void drawRectangle() {


g2d.setColor(currentColor);
g2d.setStroke(new BasicStroke(2));

int x = Math.min(startPoint.x, endPoint.x);


int y = Math.min(startPoint.y, endPoint.y);
int width = Math.abs(endPoint.x - startPoint.x);
int height = Math.abs(endPoint.y - startPoint.y);

g2d.drawRect(x, y, width, height);


}

private void drawOval() {


g2d.setColor(currentColor);
g2d.setStroke(new BasicStroke(2));

int x = Math.min(startPoint.x, endPoint.x);


int y = Math.min(startPoint.y, endPoint.y);
int width = Math.abs(endPoint.x - startPoint.x);
int height = Math.abs(endPoint.y - startPoint.y);

g2d.drawOval(x, y, width, height);


}

private void drawArc() {


g2d.setColor(currentColor);
g2d.setStroke(new BasicStroke(2));

int x = Math.min(startPoint.x, endPoint.x);


int y = Math.min(startPoint.y, endPoint.y);
int width = Math.abs(endPoint.x - startPoint.x);
int height = Math.abs(endPoint.y - startPoint.y);

// Draw a 180-degree arc


g2d.drawArc(x, y, width, height, 0, 180);
}

public void clearCanvas() {


g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, getWidth(), getHeight());
repaint();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(canvas, 0, 0, null);
}
}
// Helper class for storing drawing actions (for future undo/redo
functionality)
private static class DrawingAction {
String tool;
Color color;
Point start;
Point end;

public DrawingAction(String tool, Color color, Point start, Point


end) {
this.tool = tool;
this.color = color;
this.start = start;
this.end = end;
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> new PaintApp());
}
}

You might also like