Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. public void paint(Graphics g){
  2. g.drawLine(......
  3. ....
  4. //along with all of my other draw commands
  5. }
  6.  
  7. boolean drawHello = true;
  8. boolean drawWorld = false;
  9.  
  10. protected void paintComponent(Graphics g) {
  11. super.paintCompoent(g);
  12. if (drawHello)
  13. g.drawString("Hello", 50, 50);
  14.  
  15. if (drawWorld)
  16. g.drawString("World", 10, 10);
  17. }
  18.  
  19. public void actionPerformed(ActionEvent e) {
  20. drawWorld = true;
  21. repaint();
  22. }
  23.  
  24. import java.awt.BorderLayout;
  25. import java.awt.Color;
  26. import java.awt.Dimension;
  27. import java.awt.Graphics;
  28. import java.awt.event.ActionEvent;
  29. import java.awt.event.ActionListener;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. import javax.swing.JButton;
  33. import javax.swing.JFrame;
  34. import javax.swing.JPanel;
  35. import javax.swing.SwingUtilities;
  36.  
  37. public class AddSquares {
  38.  
  39. private int R = 0;
  40. private int G = 0;
  41. private int B = 0;
  42. private int xLoc = 0;
  43. private int yLoc = 0;
  44.  
  45. List<Square> squares = new ArrayList<>();
  46. private JButton addSquare = new JButton("Add Square");
  47. private RectsPanel panel = new RectsPanel();
  48.  
  49. public AddSquares() {
  50.  
  51. addSquare.addActionListener(new ActionListener(){
  52. @Override
  53. public void actionPerformed(ActionEvent e) {
  54. Color color = new Color(R, G, B);
  55. squares.add(new Square(xLoc, yLoc, color));
  56. panel.repaint();
  57. R += 10;
  58. G += 20;
  59. B += 30;
  60. xLoc += 20;
  61. yLoc += 20;
  62.  
  63. }
  64. });
  65.  
  66. JFrame frame = new JFrame("Draw Squares");
  67. frame.add(panel, BorderLayout.CENTER);
  68. frame.add(addSquare, BorderLayout.SOUTH);
  69. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  70. frame.pack();
  71. frame.setVisible(true);
  72. }
  73.  
  74. private class RectsPanel extends JPanel {
  75.  
  76. @Override
  77. protected void paintComponent(Graphics g) {
  78. super.paintComponent(g);
  79. for (Square square : squares) {
  80. square.drawSquare(g);
  81. }
  82. }
  83.  
  84. @Override
  85. public Dimension getPreferredSize() {
  86. return new Dimension(250, 250);
  87. }
  88. }
  89.  
  90. private class Square {
  91. int x = 0;
  92. int y = 0;
  93. Color color;
  94.  
  95. public Square(int x, int y, Color color) {
  96. this.x = x;
  97. this.y = y;
  98. this.color = color;
  99. }
  100.  
  101. public void drawSquare(Graphics g) {
  102. g.setColor(color);
  103. g.fillRect(x, y, 75 ,75);
  104. }
  105. }
  106.  
  107. public static void main(String[] args) {
  108. SwingUtilities.invokeLater(new Runnable() {
  109. @Override
  110. public void run() {
  111. AddSquares addSquares = new AddSquares();
  112. }
  113. });
  114. }
  115. }
  116.  
  117. import javax.swing.*;
  118. import java.awt.*;
  119. import java.awt.image.*;
  120. import java.awt.event.*;
  121.  
  122. class PaintAnyTime {
  123. public static void main(String[] args) {
  124. EventQueue.invokeLater(new Runnable() {
  125. @Override
  126. public void run() {
  127. new PaintAnyTime();
  128. }
  129. });
  130. }
  131.  
  132. final BufferedImage image = (
  133. new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB)
  134. );
  135.  
  136. final JFrame frame = new JFrame();
  137.  
  138. final JLabel label = new JLabel(new ImageIcon(image));
  139.  
  140. final MouseAdapter drawer = new MouseAdapter() {
  141. Graphics2D g2D;
  142.  
  143. @Override
  144. public void mousePressed(MouseEvent me) {
  145. g2D = image.createGraphics();
  146. g2D.setColor(Color.BLACK);
  147. }
  148.  
  149. @Override
  150. public void mouseDragged(MouseEvent me) {
  151. g2D.fillRect(me.getX(), me.getY(), 3, 3);
  152. label.repaint();
  153. }
  154.  
  155. @Override
  156. public void mouseReleased(MouseEvent me) {
  157. g2D.dispose();
  158. g2D = null;
  159. }
  160. };
  161.  
  162. PaintAnyTime() {
  163. label.setPreferredSize(
  164. new Dimension(image.getWidth(), image.getHeight())
  165. );
  166.  
  167. label.addMouseListener(drawer);
  168. label.addMouseMotionListener(drawer);
  169.  
  170. frame.add(label);
  171.  
  172. frame.pack();
  173. frame.setResizable(false);
  174. frame.setLocationRelativeTo(null);
  175. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  176. frame.setVisible(true);
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement