Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.42 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.FlowLayout;
  5. import java.awt.Graphics;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.MouseEvent;
  9. import java.awt.event.MouseMotionListener;
  10.  
  11. import javax.swing.BorderFactory;
  12. import javax.swing.JButton;
  13. import javax.swing.JColorChooser;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JPanel;
  18. import javax.swing.JSpinner;
  19. import javax.swing.SpinnerNumberModel;
  20. import javax.swing.event.ChangeEvent;
  21. import javax.swing.event.ChangeListener;
  22.  
  23.  
  24. public class MainClass2{
  25. static JLabel colorlabel = new JLabel(" Color ");
  26. public static JLabel xlab = new JLabel("0");
  27. public static JLabel ylab = new JLabel("0");
  28.  
  29. static JFrame frame1 = new JFrame();
  30. static JButton quitbutton = new JButton("Quit");
  31. static JButton infobutton = new JButton("Info");
  32. static JButton clearbutton = new JButton("Clear");
  33. static JButton savebutton = new JButton("Save");
  34. private static int stroke1 = 4;
  35. static SpinnerNumberModel spinnervals = new SpinnerNumberModel(stroke1,1,15,1);
  36.  
  37. static JSpinner spinnerbrush = new JSpinner(spinnervals);
  38.  
  39. static JButton selectcolor = new JButton("Select Color");
  40. static JButton selectbg = new JButton("Select Background");
  41. public static Color col1 = Color.WHITE;
  42. private static Color col2 = Color.WHITE;
  43.  
  44. static AuxClass4 panel1 = new AuxClass4(col1, col2, stroke1);
  45.  
  46. static JPanel panel2 = new JPanel();
  47. static JPanel panel3 = new JPanel();
  48. static JPanel panel4 = new JPanel();
  49.  
  50. private static void addPanel1(){
  51.  
  52. panel1.setPreferredSize(new Dimension(800,500));
  53. }
  54. private static void addPanel2(){
  55. ButtonAction inst1 = new ButtonAction();
  56. xlab.setMinimumSize(new Dimension(30,30));
  57. ylab.setMinimumSize(new Dimension(30,30));
  58. xlab.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  59. ylab.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  60. panel2.add(new JLabel(" X: "));
  61. panel2.add(xlab);
  62. panel2.add(new JLabel(" Y: "));
  63. panel2.add(ylab);
  64.  
  65. panel2.add(savebutton);
  66. panel2.add(clearbutton);
  67. panel2.add(quitbutton);
  68. panel2.add(infobutton);
  69. panel2.setLayout(new FlowLayout());
  70.  
  71. quitbutton.addActionListener(inst1);
  72. infobutton.addActionListener(inst1);
  73. clearbutton.addActionListener(inst1);
  74. savebutton.addActionListener(inst1);
  75.  
  76. selectcolor.addActionListener(inst1);
  77. selectbg.addActionListener(inst1);
  78. }
  79. //add to panel3
  80. private static void addPanel3(){
  81. StrokeAction inst2 = new StrokeAction();
  82. spinnerbrush.addChangeListener(inst2);
  83. panel3.add(new JLabel("Stroke size"));
  84. panel3.add(spinnerbrush);
  85. panel3.add(selectcolor);
  86. panel3.add(new JLabel("Current color:"));
  87. colorlabel.setSize(20, 20);
  88. colorlabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  89. colorlabel.setOpaque(true);
  90. colorlabel.setBackground(Color.WHITE);
  91. colorlabel.setForeground(Color.WHITE);
  92. panel3.add(colorlabel);
  93. panel3.add(selectbg);
  94.  
  95. }
  96.  
  97. private static class ButtonAction implements ActionListener{
  98.  
  99. public void actionPerformed(ActionEvent arg0) {
  100. if (arg0.getSource()==quitbutton){
  101. System.exit(0);
  102. }
  103. else if(arg0.getSource()==infobutton){
  104. JOptionPane.showMessageDialog(frame1, "Paint Toolbox designed in Java");
  105. }
  106. else if(arg0.getSource()==selectcolor){
  107.  
  108. panel1.changeColor();
  109.  
  110. }
  111. else if(arg0.getSource()==selectbg){
  112. panel1.changeBg();
  113. }
  114. else if(arg0.getSource()==clearbutton){
  115. panel1.colfg = panel1.colbg;
  116. colorlabel.setBackground(panel1.colfg);
  117. colorlabel.setForeground(panel1.colfg);
  118. panel1.setForeground(null);
  119.  
  120. }
  121. else if(arg0.getSource()==savebutton){
  122. panel1.saveImage();
  123. }
  124.  
  125. }
  126.  
  127. }
  128.  
  129. private static class StrokeAction implements ChangeListener{
  130.  
  131. public void stateChanged(ChangeEvent arg0) {
  132. if (arg0.getSource()==spinnerbrush){
  133. Object o1 = spinnervals.getValue();
  134. Integer int1 = (Integer) o1;
  135. stroke1 = int1.intValue();
  136. panel1.changeStroke(stroke1);
  137. }
  138.  
  139. // TODO Auto-generated method stub
  140. }
  141.  
  142. }
  143.  
  144. public static void main(String args[]){
  145.  
  146. addPanel1();
  147. addPanel2();
  148. addPanel3();
  149. frame1.add(panel1, BorderLayout.NORTH);
  150. frame1.add(panel2, BorderLayout.SOUTH);
  151. frame1.add(panel3, BorderLayout.CENTER);
  152. frame1.setTitle("PaintBox v2.2");
  153. frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  154. frame1.setSize(800,600);
  155. frame1.setResizable(false);
  156. frame1.setLocationRelativeTo(null);
  157. frame1.setVisible(true);
  158.  
  159. }
  160.  
  161. }
  162.  
  163. class AuxClass4 extends JPanel implements MouseMotionListener{
  164.  
  165. /**
  166. *
  167. */
  168. private static final long serialVersionUID = 1L;
  169. private int xval;
  170. private int yval;
  171. public Color colfg;
  172. public Color colbg;
  173. public int strokesize;
  174. public int fileorder;
  175.  
  176.  
  177. public AuxClass4(Color input1, Color input2, int input3){
  178. colfg=input1;
  179. colbg=input2;
  180. strokesize = input3;
  181. this.setBorder(BorderFactory.createLineBorder(Color.CYAN, 10));
  182. this.setBackground(colbg);
  183. //this.setMaximumSize(new Dimension(500,380));
  184. this.addMouseMotionListener(this);
  185.  
  186. }
  187.  
  188. public void paintComponent(Graphics g1){
  189. super.paintComponent(g1);
  190. g1.setColor(colfg);
  191. g1.fillRect(xval, yval, strokesize, strokesize);
  192.  
  193. }
  194.  
  195. public void mouseDragged(MouseEvent arg0) {
  196. xval = arg0.getX();
  197. yval = arg0.getY();
  198. repaint(xval, yval, strokesize,strokesize);
  199.  
  200. // TODO Auto-generated method stub
  201.  
  202. }
  203.  
  204. public void mouseMoved(MouseEvent arg0) {
  205. xval = arg0.getX();
  206. yval = arg0.getY();
  207. String xvalret = Integer.toString(xval);
  208. String yvalret = Integer.toString(yval);
  209. MainClass2.xlab.setText(" " + xvalret + " ");
  210. MainClass2.ylab.setText(" " + yvalret + " ");
  211.  
  212. }
  213. public void changeColor(){
  214. colfg = JColorChooser.showDialog(this, "Select color", colfg);
  215. MainClass2.colorlabel.setBackground(colfg);
  216. MainClass2.colorlabel.setForeground(colfg);
  217. }
  218.  
  219. public void changeBg(){
  220. colbg=JColorChooser.showDialog(this, "Select color", Color.WHITE);
  221. colfg=colbg;
  222. this.setBackground(colbg);
  223. //MainClass1.colorlabel.setBackground(colfg);
  224. // MainClass1.colorlabel.setForeground(colfg);
  225.  
  226.  
  227. }
  228.  
  229. public void changeStroke(int inputstroke){
  230. strokesize=inputstroke;
  231. }
  232.  
  233. public void saveImage(){
  234. final String dir = System.getProperty("user.dir");
  235. JOptionPane.showMessageDialog(this, "File saved under " + dir + "/saveimage" + fileorder +".png");
  236. fileorder+=1;
  237.  
  238. }
  239.  
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement