Question
import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import javax.swing.*; public class SimplePaint extends JPanel implements MouseListener, MouseMotionListener { public static void main(String[] args) { JFrame window
import java.awt.*; import java.awt.event.*; import java.util.ArrayList;
import javax.swing.*;
public class SimplePaint extends JPanel implements MouseListener, MouseMotionListener {
public static void main(String[] args) { JFrame window = new JFrame("Simple Paint"); SimplePaint content = new SimplePaint(); window.setContentPane(content); window.setSize(700,380); window.setLocation(100,100); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); window.setVisible(true);
}
private final static int BLACK = 0, RED = 1, GREEN = 2, BLUE = 3, CYAN = 4, MAGENTA = 5, YELLOW = 6;
private int currentColor = BLACK; private int prevX, prevY; private boolean dragging;
// *** Let's make a nested class to define a new data type that will be stored in a data structure // e.g. (not a Rectangle[] rectangles) private class Line { private int x1, x2, y1, y2; private int colorCode; public Line(int x1, int x2, int y1, int y2, int colorCode) { super(); this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; this.colorCode = colorCode; } } private ArrayList
public void paintComponent(Graphics g) { super.paintComponent(g);
int width = getWidth(); int height = getHeight();
int colorSpacing = (height - 56) / 7;
g.setColor(Color.GRAY); g.drawRect(0, 0, width-1, height-1); g.drawRect(1, 1, width-3, height-3); g.drawRect(2, 2, width-5, height-5); g.fillRect(width - 56, 0, 56, height); g.setColor(Color.WHITE); g.fillRect(width-53, height-53, 50, 50); g.setColor(Color.BLACK); g.drawRect(width-53, height-53, 49, 49); g.drawString("CLEAR", width-48, height-23); g.setColor(Color.BLACK); g.fillRect(width-53, 3 + 0*colorSpacing, 50, colorSpacing-3); g.setColor(Color.RED); g.fillRect(width-53, 3 + 1*colorSpacing, 50, colorSpacing-3); g.setColor(Color.GREEN); g.fillRect(width-53, 3 + 2*colorSpacing, 50, colorSpacing-3); g.setColor(Color.BLUE); g.fillRect(width-53, 3 + 3*colorSpacing, 50, colorSpacing-3); g.setColor(Color.CYAN); g.fillRect(width-53, 3 + 4*colorSpacing, 50, colorSpacing-3); g.setColor(Color.MAGENTA); g.fillRect(width-53, 3 + 5*colorSpacing, 50, colorSpacing-3); g.setColor(Color.YELLOW); g.fillRect(width-53, 3 + 6*colorSpacing, 50, colorSpacing-3); g.setColor(Color.WHITE); g.drawRect(width-55, 1 + currentColor*colorSpacing, 53, colorSpacing); g.drawRect(width-54, 2 + currentColor*colorSpacing, 51, colorSpacing-2); for (int i = 0; i < lines.size(); i++) { switch(lines.get(i).colorCode) { case BLACK: g.setColor(Color.BLACK); break; case RED: g.setColor(Color.RED); break; case GREEN: g.setColor(Color.GREEN); break; case BLUE: g.setColor(Color.BLUE); break; case CYAN: g.setColor(Color.CYAN); break; case MAGENTA: g.setColor(Color.MAGENTA); break; case YELLOW: g.setColor(Color.YELLOW); break; } g.drawLine(lines.get(i).x1, lines.get(i).y1, lines.get(i).x2, lines.get(i).y2); } } // end paintComponent()
private void changeColor(int y) { int width = getWidth(); int height = getHeight(); int colorSpacing = (height - 56) / 7; int newColor = y / colorSpacing; System.out.println("y: " + y + " colorSpacing: " + colorSpacing + " newColor: " + newColor); if (newColor < 0 || newColor > 6) return; currentColor = newColor; }
/** * This is called when the user presses the mouse anywhere in the panel. * There are three possible responses, depending on where the user clicked: * Change the current color, clear the drawing, or start drawing a curve. * (Or do nothing if user clicks on the border.) */ public void mousePressed(MouseEvent evt) { int x = evt.getX(); int y = evt.getY();
int width = getWidth(); int height = getHeight();
if (dragging == true) // Ignore mouse presses that occur return; // when user is already drawing a curve. // (This can happen if the user presses // two mouse buttons at the same time.) //***like left button is down+dragging but you click the right button if (x > width - 53) { if (y > height - 53) { // ***Clicked on "CLEAR button". } else { changeColor(y); // Clicked on the color palette. // ***update the highlighted square of color } } else if (x > 3 && x < width - 56 && y > 3 && y < height - 3) { // The user has clicked on the white drawing area. // Start drawing a curve from the point (x,y). prevX = x; prevY = y; dragging = true; }
} // end mousePressed()
/** * Called whenever the user releases the mouse button. If the user was drawing * a curve, the curve is done, so we should set dragging to false */ public void mouseReleased(MouseEvent evt) { if (dragging == false) return; // Nothing to do because the user isn't drawing. dragging = false; }
/** * Called whenever the user moves the mouse while a mouse button is held down. * If the user is drawing, draw a line segment from the previous mouse location * to the current mouse location, and set up prevX and prevY for the next call. * Note that in case the user drags outside of the drawing area, the values of * x and y are "clamped" to lie within this area. This avoids drawing on the color * palette or clear button. */ public void mouseDragged(MouseEvent evt) { if (dragging == false) return; // Nothing to do because the user isn't drawing.
int x = evt.getX(); // x-coordinate of mouse. int y = evt.getY(); // y-coordinate of mouse.
if (x < 3) // Adjust the value of x, x = 3; // to make sure it's in if (x > getWidth() - 57) // the drawing area. x = getWidth() - 57;
if (y < 3) // Adjust the value of y, y = 3; // to make sure it's in if (y > getHeight() - 4) // the drawing area. y = getHeight() - 4;
// *** update our data structure to reflect the new state as the user is dragging // Remember, NO DRAWING here!
} // end mouseDragged()
public void mouseEntered(MouseEvent evt) { } // Some empty routines. public void mouseExited(MouseEvent evt) { } // (Required by the MouseListener public void mouseClicked(MouseEvent evt) { } // and MouseMotionListener public void mouseMoved(MouseEvent evt) { } // interfaces).
} // end class SimplePaint
goal:
draw lines smoothly based on dragging.
allow you to change colors and draw with that new color (with the new color highlighted appropriately)
allow you to resize the panel without losing all the lines
allow you to clear the drawing by pushing the "Clear" button
ALL CODE IN JAVA
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started