Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.29 KB | None | 0 0
  1. package paint2;
  2.  
  3. import javax.swing.*;
  4. import javax.swing.event.ChangeEvent;
  5. import javax.swing.event.ChangeListener;
  6.  
  7. import java.awt.event.*;
  8. import java.awt.*;
  9. import java.awt.geom.*;
  10. import java.text.DecimalFormat;
  11. import java.util.*;
  12.  
  13. @SuppressWarnings("serial")
  14. public class Paint extends JFrame
  15. {
  16.  
  17. JButton brushBut, lineBut, ellipseBut, rectBut, strokeBut, fillBut;
  18.  
  19. // Slider used to change the transparency
  20.  
  21. JSlider transSlider;
  22.  
  23. JLabel transLabel;
  24.  
  25. // Makes sure the float for transparency only shows 2 digits
  26.  
  27. DecimalFormat dec = new DecimalFormat("#.##");
  28.  
  29. // Contains all of the rules for drawing
  30.  
  31. Graphics2D graphSettings;
  32.  
  33. // Homework use graphSettings.setStroke(new BasicStroke(5F));
  34. // To change the stroke dynamically with a component
  35.  
  36. // Going to be used to monitor what shape to draw next
  37.  
  38. int currentAction = 1;
  39.  
  40. // Transparency of the shape
  41.  
  42. float transparentVal = 1.0f;
  43.  
  44. // Default stroke and fill colors
  45.  
  46. Color strokeColor=Color.BLACK, fillColor=Color.BLACK;
  47.  
  48. public static void main(String [] args)
  49. {
  50. new Paint();
  51. }
  52.  
  53. public Paint()
  54. {
  55. // Define the defaults for the JFrame
  56.  
  57. this.setSize(800, 600);
  58. this.setTitle("Java Paint");
  59. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  60.  
  61. JPanel buttonPanel = new JPanel();
  62.  
  63. // Swing box that will hold all the buttons
  64.  
  65. Box theBox = Box.createHorizontalBox();
  66.  
  67. // Make all the buttons in makeMeButtons by passing the
  68. // button icon.
  69.  
  70. brushBut = makeMeButtons("./src/brush.png", 1);
  71. lineBut = makeMeButtons("./src/Line.png", 2);
  72. ellipseBut = makeMeButtons("./src/Ellipse.png", 3);
  73. rectBut = makeMeButtons("./src/Rectangle.png", 4);
  74.  
  75. // Make all the buttons in makeMeColorButton by passing the
  76. // button icon and true for stroke color or false for fill
  77.  
  78. strokeBut = makeMeColorButton("./src/Stroke.png", 5, true);
  79. fillBut = makeMeColorButton("./src/Fill.png", 6, false);
  80.  
  81. // Add the buttons to the box
  82.  
  83. theBox.add(brushBut);
  84. theBox.add(lineBut);
  85. theBox.add(ellipseBut);
  86. theBox.add(rectBut);
  87. theBox.add(strokeBut);
  88. theBox.add(fillBut);
  89.  
  90. // Add the transparent label and slider
  91.  
  92. transLabel = new JLabel("Transparent: 1");
  93.  
  94. // Min value, Max value and starting value for slider
  95.  
  96. transSlider = new JSlider(1, 99, 99);
  97.  
  98. // Create an instance of ListenForEvents to handle events
  99.  
  100. ListenForSlider lForSlider = new ListenForSlider();
  101.  
  102. // Tell Java that you want to be alerted when an event
  103. // occurs on the slider
  104.  
  105. transSlider.addChangeListener(lForSlider);
  106.  
  107. theBox.add(transLabel);
  108. theBox.add(transSlider);
  109.  
  110. // Add the box of buttons to the panel
  111.  
  112. buttonPanel.add(theBox);
  113.  
  114. // Position the buttons in the bottom of the frame
  115.  
  116. this.add(buttonPanel, BorderLayout.SOUTH);
  117.  
  118. // Make the drawing area take up the rest of the frame
  119.  
  120. this.add(new DrawingBoard(), BorderLayout.CENTER);
  121.  
  122. // Show the frame
  123.  
  124. this.setVisible(true);
  125. }
  126.  
  127. // Spits out buttons based on the image supplied
  128. // actionNum represents each shape to be drawn
  129.  
  130. public JButton makeMeButtons(String iconFile, final int actionNum){
  131. JButton theBut = new JButton();
  132. Icon butIcon = new ImageIcon(iconFile);
  133. theBut.setIcon(butIcon);
  134.  
  135. // Make the proper actionPerformed method execute when the
  136. // specific button is pressed
  137.  
  138. theBut.addActionListener(new ActionListener() {
  139.  
  140. public void actionPerformed(ActionEvent e) {
  141. currentAction = actionNum;
  142.  
  143. }
  144. });
  145.  
  146. return theBut;
  147. }
  148.  
  149. // Spits out buttons based on the image supplied and
  150. // whether a stroke or fill is to be defined
  151.  
  152. public JButton makeMeColorButton(String iconFile, final int actionNum, final boolean stroke){
  153. JButton theBut = new JButton();
  154. Icon butIcon = new ImageIcon(iconFile);
  155. theBut.setIcon(butIcon);
  156.  
  157. theBut.addActionListener(new ActionListener() {
  158.  
  159. public void actionPerformed(ActionEvent e) {
  160.  
  161. if(stroke){
  162.  
  163. // JColorChooser is a popup that lets you pick a color
  164.  
  165. strokeColor = JColorChooser.showDialog(null, "Pick a Stroke", Color.BLACK);
  166. } else {
  167. fillColor = JColorChooser.showDialog(null, "Pick a Fill", Color.BLACK);
  168. }
  169.  
  170. }
  171. });
  172.  
  173. return theBut;
  174. }
  175.  
  176. private class DrawingBoard extends JComponent
  177. {
  178.  
  179. // ArrayLists that contain each shape drawn along with
  180. // that shapes stroke and fill
  181.  
  182. ArrayList<Shape> shapes = new ArrayList<Shape>();
  183. ArrayList<Color> shapeFill = new ArrayList<Color>();
  184. ArrayList<Color> shapeStroke = new ArrayList<Color>();
  185. ArrayList<Float> transPercent = new ArrayList<Float>();
  186.  
  187. Point drawStart, drawEnd;
  188.  
  189. // Monitors events on the drawing area of the frame
  190.  
  191. public DrawingBoard()
  192. {
  193.  
  194. this.addMouseListener(new MouseAdapter()
  195. {
  196.  
  197. public void mousePressed(MouseEvent e)
  198. {
  199.  
  200. if(currentAction != 1){
  201.  
  202. // When the mouse is pressed get x & y position
  203.  
  204. drawStart = new Point(e.getX(), e.getY());
  205. drawEnd = drawStart;
  206. repaint();
  207.  
  208. }
  209.  
  210.  
  211. }
  212.  
  213. public void mouseReleased(MouseEvent e)
  214. {
  215.  
  216. if(currentAction != 1){
  217.  
  218. // Create a shape using the starting x & y
  219. // and finishing x & y positions
  220.  
  221. Shape aShape = null;
  222.  
  223. if (currentAction == 2){
  224. aShape = drawLine(drawStart.x, drawStart.y,
  225. e.getX(), e.getY());
  226. } else
  227.  
  228. if (currentAction == 3){
  229. aShape = drawEllipse(drawStart.x, drawStart.y,
  230. e.getX(), e.getY());
  231. } else
  232.  
  233. if (currentAction == 4) {
  234.  
  235. // Create a new rectangle using x & y coordinates
  236.  
  237. aShape = drawRectangle(drawStart.x, drawStart.y,
  238. e.getX(), e.getY());
  239. }
  240.  
  241.  
  242. // Add shapes, fills and colors to there ArrayLists
  243.  
  244. shapes.add(aShape);
  245. shapeFill.add(fillColor);
  246. shapeStroke.add(strokeColor);
  247.  
  248. // Add transparency value to ArrayList
  249.  
  250. transPercent.add(transparentVal);
  251.  
  252. drawStart = null;
  253. drawEnd = null;
  254.  
  255. // repaint the drawing area
  256.  
  257. repaint();
  258.  
  259. }
  260.  
  261. }
  262. } );
  263.  
  264. this.addMouseMotionListener(new MouseMotionAdapter()
  265. {
  266.  
  267. public void mouseDragged(MouseEvent e)
  268. {
  269.  
  270. // If this is a brush have shapes go on the screen quickly
  271.  
  272. if(currentAction == 1){
  273.  
  274. int x = e.getX();
  275. int y = e.getY();
  276.  
  277. Shape aShape = null;
  278.  
  279. // Make stroke and fill equal to eliminate the fact that this is an ellipse
  280.  
  281. strokeColor = fillColor;
  282.  
  283. aShape = drawBrush(x,y,5,5);
  284.  
  285. shapes.add(aShape);
  286. shapeFill.add(fillColor);
  287. shapeStroke.add(strokeColor);
  288.  
  289. // Add the transparency value
  290.  
  291. transPercent.add(transparentVal);
  292. }
  293.  
  294. // Get the final x & y position after the mouse is dragged
  295.  
  296. drawEnd = new Point(e.getX(), e.getY());
  297. repaint();
  298. }
  299. } );
  300. }
  301.  
  302.  
  303. public void paint(Graphics g)
  304. {
  305. // Class used to define the shapes to be drawn
  306.  
  307. graphSettings = (Graphics2D)g;
  308.  
  309. // Antialiasing cleans up the jagged lines and defines rendering rules
  310.  
  311. graphSettings.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  312. RenderingHints.VALUE_ANTIALIAS_ON);
  313.  
  314. // Defines the line width of the stroke
  315.  
  316. graphSettings.setStroke(new BasicStroke(4));
  317.  
  318. // Iterators created to cycle through strokes and fills
  319. Iterator<Color> strokeCounter = shapeStroke.iterator();
  320. Iterator<Color> fillCounter = shapeFill.iterator();
  321.  
  322. // Iterator for transparency
  323.  
  324. Iterator<Float> transCounter = transPercent.iterator();
  325.  
  326. for (Shape s : shapes)
  327. {
  328.  
  329. // Sets the shapes transparency value
  330.  
  331. graphSettings.setComposite(AlphaComposite.getInstance(
  332. AlphaComposite.SRC_OVER, transCounter.next()));
  333.  
  334. // Grabs the next stroke from the color arraylist
  335. graphSettings.setPaint(strokeCounter.next());
  336.  
  337. graphSettings.draw(s);
  338.  
  339. // Grabs the next fill from the color arraylist
  340. graphSettings.setPaint(fillCounter.next());
  341.  
  342. graphSettings.fill(s);
  343. }
  344.  
  345. // Guide shape used for drawing
  346. if (drawStart != null && drawEnd != null)
  347. {
  348. // Makes the guide shape transparent
  349.  
  350. graphSettings.setComposite(AlphaComposite.getInstance(
  351. AlphaComposite.SRC_OVER, 0.40f));
  352.  
  353. // Make guide shape gray for professional look
  354.  
  355. graphSettings.setPaint(Color.LIGHT_GRAY);
  356.  
  357. Shape aShape = null;
  358.  
  359. if (currentAction == 2){
  360. aShape = drawLine(drawStart.x, drawStart.y,
  361. drawEnd.x, drawEnd.y);
  362. } else
  363.  
  364. if (currentAction == 3){
  365. aShape = drawEllipse(drawStart.x, drawStart.y,
  366. drawEnd.x, drawEnd.y);
  367. } else
  368.  
  369. if (currentAction == 4) {
  370.  
  371. // Create a new rectangle using x & y coordinates
  372.  
  373. aShape = drawRectangle(drawStart.x, drawStart.y,
  374. drawEnd.x, drawEnd.y);
  375. }
  376.  
  377.  
  378. graphSettings.draw(aShape);
  379. }
  380. }
  381.  
  382. private Rectangle2D.Float drawRectangle(
  383. int x1, int y1, int x2, int y2)
  384. {
  385. // Get the top left hand corner for the shape
  386. // Math.min returns the points closest to 0
  387.  
  388. int x = Math.min(x1, x2);
  389. int y = Math.min(y1, y2);
  390.  
  391. // Gets the difference between the coordinates and
  392.  
  393. int width = Math.abs(x1 - x2);
  394. int height = Math.abs(y1 - y2);
  395.  
  396. return new Rectangle2D.Float(
  397. x, y, width, height);
  398. }
  399.  
  400. // The other shapes will work similarly
  401. // More on this in the next tutorial
  402.  
  403. private Ellipse2D.Float drawEllipse(
  404. int x1, int y1, int x2, int y2)
  405. {
  406. int x = Math.min(x1, x2);
  407. int y = Math.min(y1, y2);
  408. int width = Math.abs(x1 - x2);
  409. int height = Math.abs(y1 - y2);
  410.  
  411. return new Ellipse2D.Float(
  412. x, y, width, height);
  413. }
  414.  
  415. private Line2D.Float drawLine(
  416. int x1, int y1, int x2, int y2)
  417. {
  418.  
  419. return new Line2D.Float(
  420. x1, y1, x2, y2);
  421. }
  422.  
  423. private Ellipse2D.Float drawBrush(
  424. int x1, int y1, int brushStrokeWidth, int brushStrokeHeight)
  425. {
  426.  
  427. return new Ellipse2D.Float(
  428. x1, y1, brushStrokeWidth, brushStrokeHeight);
  429.  
  430. }
  431.  
  432. }
  433.  
  434. // Implements ActionListener so it can react to events on components
  435.  
  436. private class ListenForSlider implements ChangeListener{
  437.  
  438. // Called when the spinner is changed
  439.  
  440. public void stateChanged(ChangeEvent e) {
  441.  
  442. // Check if the source of the event was the button
  443.  
  444. if(e.getSource() == transSlider){
  445.  
  446. // Change the value for the label next to the spinner
  447. // Use decimal format to make sure only 2 decimals are ever displayed
  448.  
  449. transLabel.setText("Transparent: " + dec.format(transSlider.getValue() * .01) );
  450.  
  451. // Set the value for transparency for every shape drawn after
  452.  
  453. transparentVal = (float) (transSlider.getValue() * .01);
  454.  
  455. }
  456.  
  457. }
  458.  
  459. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement