Advertisement
Guest User

Desenhando

a guest
Apr 23rd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.87 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.MouseEvent;
  5. import java.awt.event.MouseListener;
  6. import java.awt.event.MouseMotionListener;
  7. import java.awt.geom.Rectangle2D;
  8. import java.awt.image.BufferedImage;
  9. import javax.swing.JButton;
  10. import javax.swing.JColorChooser;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JPanel;
  14. import javax.swing.JToolBar;
  15.  
  16. // Esta é uma ferramenta gráfica de desenho básica no qual se podem criar figuras simples.
  17.  
  18. public class Desenhando{
  19.  
  20. public static void main(String args[]){
  21.  
  22. JanelaComandos JanelaComandos = new JanelaComandos();
  23.  
  24. JanelaComandos.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25. JanelaComandos.setSize(1000,500);
  26. JanelaComandos.setVisible(true);
  27. JanelaComandos.setLocationRelativeTo(null);
  28.  
  29. }
  30. }
  31.  
  32. // Autor: João Matheus Santos Assis
  33.  
  34. class JanelaComandos extends JFrame{
  35.  
  36. // Barra de Ferramenta com os botões necessários
  37. private JToolBar Barra_De_Ferramenta = new JToolBar();
  38. private JButton Retangulo = new JButton("Retângulo");
  39. private JButton Circulo = new JButton("Circulo");
  40. private JButton Reta = new JButton("Reta");
  41. private JButton Editar_Cor = new JButton("Editar Cor");
  42.  
  43.  
  44. private Gerador_Desenho Panel_Desenho = new Gerador_Desenho();
  45.  
  46. private Color Cor_da_Figura = Color.RED;
  47. private Color Ultima_Cor = Color.RED;
  48.  
  49.  
  50. private JLabel Status = new JLabel(" Forma ativa: Retângulo");
  51.  
  52.  
  53.  
  54. public JanelaComandos() {
  55. super("Desenvolvedor Gráfico");
  56.  
  57. // Adicionando os componentes
  58. Barra_De_Ferramenta.add(Retangulo);
  59. Barra_De_Ferramenta.add(Circulo);
  60. Barra_De_Ferramenta.add(Reta);
  61. Barra_De_Ferramenta.add(Editar_Cor);
  62.  
  63. // Cor de fundo da Barra de Status
  64. Status.setBackground(Color.WHITE);
  65.  
  66. add(Barra_De_Ferramenta, BorderLayout.NORTH);
  67. add(Panel_Desenho, BorderLayout.CENTER);
  68. add(Status, BorderLayout.SOUTH);
  69.  
  70. Eventos_Desenhando Eventos = new Eventos_Desenhando();
  71. Retangulo.addActionListener(Eventos);
  72. Circulo.addActionListener(Eventos);
  73. Reta.addActionListener(Eventos);
  74. Editar_Cor.addActionListener(Eventos);
  75.  
  76. }
  77.  
  78.  
  79.  
  80. private class Eventos_Desenhando implements ActionListener{
  81.  
  82. /* A variável Forma determinará qual figura deverá ser desenhada,
  83. se seu valor for 0 será desenhado um Retângulo, caso for 1
  84. um Círculo, 2 será uma reta*/
  85. int Forma = 0;
  86.  
  87. public void actionPerformed(ActionEvent event) {
  88.  
  89.  
  90. if (event.getSource()== Retangulo){
  91. Forma=0;
  92. Status.setText(" Forma ativa: Retângulo");
  93. repaint();
  94. }
  95.  
  96. if (event.getSource() == Circulo){
  97. Forma=1;
  98. Status.setText(" Forma ativa: Circulo");
  99. repaint();
  100. }
  101.  
  102. if (event.getSource() == Reta){
  103. Forma=2;
  104. Status.setText(" Forma ativa: Reta");
  105. repaint();
  106. }
  107.  
  108. // Criando uma Caixa de Cor para mudar a cor da linha
  109. if (event.getSource() == Editar_Cor){
  110. Cor_da_Figura = JColorChooser.showDialog(JanelaComandos.this,
  111. "Editando Cor da Figura", Cor_da_Figura);
  112. if (Cor_da_Figura != null){
  113. Ultima_Cor = Cor_da_Figura;
  114. }
  115. }
  116. // Enviando a Forma a ser desenhada e a cor da linha
  117. Panel_Desenho.setForma( Forma, Ultima_Cor );
  118.  
  119. }
  120. }
  121. }
  122.  
  123.  
  124.  
  125. class Gerador_Desenho extends JPanel implements MouseListener, MouseMotionListener{
  126.  
  127. Dimension Dimensao = Toolkit.getDefaultToolkit().getScreenSize();
  128.  
  129. // Criando local onde ficará armazenadas as imagens.
  130.  
  131. private BufferedImage Buffered_da_Imagem = new BufferedImage((int)Dimensao.getWidth(),
  132. (int)Dimensao.getHeight(), BufferedImage.TYPE_INT_RGB);
  133.  
  134. private BufferedImage Buffered_da_Reta = new BufferedImage((int)Dimensao.getWidth(),
  135. (int)Dimensao.getHeight(), BufferedImage.TYPE_INT_RGB);
  136.  
  137. private int valor;
  138. private Color Ultima_Cor;
  139. private int x;
  140. private int y;
  141.  
  142.  
  143. public Gerador_Desenho(){
  144.  
  145. Graphics g_Imagem = Buffered_da_Imagem.createGraphics();
  146. g_Imagem.setColor(Color.WHITE);
  147. g_Imagem.fillRect(0, 0, Buffered_da_Imagem.getWidth(), Buffered_da_Imagem.getHeight());
  148. g_Imagem.dispose();
  149.  
  150. Graphics g_Reta = Buffered_da_Reta.createGraphics();
  151. g_Reta.setColor(Color.WHITE);
  152. g_Reta.fillRect(0, 0, Buffered_da_Reta.getWidth(), Buffered_da_Reta.getHeight());
  153. g_Reta.dispose();
  154.  
  155. if (Ultima_Cor == null)
  156. Ultima_Cor = Color.RED;
  157.  
  158. this.addMouseListener(this);
  159. this.addMouseMotionListener(this);
  160. }
  161.  
  162.  
  163.  
  164. public void paintComponent(Graphics g) {
  165. super.paintComponent(g);
  166.  
  167. g.setColor(Color.WHITE);
  168. g.fillRect(0, 0, Buffered_da_Imagem.getWidth(), Buffered_da_Imagem.getHeight());
  169. g.drawImage(Buffered_da_Reta, 0, 0, null);
  170.  
  171. g.dispose();
  172. }
  173.  
  174.  
  175.  
  176. public void setForma(int newValor, Color newCor){
  177. valor = newValor;
  178. Ultima_Cor = newCor;
  179. }
  180.  
  181.  
  182.  
  183. public void paint_retangulo(int x2, int y2){
  184.  
  185. Graphics2D g_retangulo = Buffered_da_Reta.createGraphics();
  186. g_retangulo.drawImage(Buffered_da_Imagem, 0, 0, null);
  187. g_retangulo.setColor(Ultima_Cor);
  188.  
  189. g_retangulo.setStroke(new BasicStroke(2.0f));
  190.  
  191. if (x2>x && y2>y)
  192. g_retangulo.drawRect(x,y,x2-x,y2-y);
  193. if (x2>x && y>y2)
  194. g_retangulo.drawRect(x,y2,x2-x,y-y2);
  195. if (x>x2 && y>y2)
  196. g_retangulo.drawRect(x2,y2,x-x2,y-y2);
  197. if (x>x2 && y2>y)
  198. g_retangulo.drawRect(x2,y,x-x2,y2-y);
  199.  
  200. g_retangulo.dispose();
  201. }
  202.  
  203.  
  204.  
  205.  
  206. public void paint_Circulo(int x2, int y2){
  207.  
  208. Graphics2D g_Circulo = Buffered_da_Reta.createGraphics();
  209. g_Circulo.drawImage(Buffered_da_Imagem, 0, 0, null);
  210. g_Circulo.setColor(Ultima_Cor);
  211.  
  212. g_Circulo.setStroke(new BasicStroke(2.0f));
  213.  
  214. if (x2>x && y2>y)
  215. g_Circulo.drawOval(x,y,x2-x,y2-y);
  216. if (x2>x && y>y2)
  217. g_Circulo.drawOval(x,y2,x2-x,y-y2);
  218. if (x>x2 && y>y2)
  219. g_Circulo.drawOval(x2,y2,x-x2,y-y2);
  220. if (x>x2 && y2>y)
  221. g_Circulo.drawOval(x2,y,x-x2,y2-y);
  222.  
  223. g_Circulo.dispose();
  224. }
  225.  
  226.  
  227.  
  228. public void paint_Reta(int x2, int y2){
  229.  
  230. Graphics2D g_Reta = Buffered_da_Reta.createGraphics();
  231. g_Reta.drawImage(Buffered_da_Imagem, 0, 0, null);
  232. // Definindo a Cor da linha
  233. g_Reta.setColor(Ultima_Cor);
  234.  
  235. // Definindo a espessura da linha
  236. g_Reta.setStroke(new BasicStroke(2.0f));
  237.  
  238. // Desenhando a linha
  239. g_Reta.drawLine(x, y, x2, y2);
  240.  
  241. // Atualizando a tela
  242. g_Reta.dispose();
  243. }
  244.  
  245.  
  246.  
  247.  
  248.  
  249. // Capturando os Eventos com o mouse
  250. public void mousePressed(MouseEvent e) {
  251.  
  252. // Obtendo as coordenadas do mouse
  253. x = e.getX();
  254. y = e.getY();
  255.  
  256. // Chamando o método Forma
  257. Forma(e.getX(), e.getY());
  258.  
  259. repaint();
  260. }
  261.  
  262.  
  263. public void mouseReleased(MouseEvent e) {
  264.  
  265. Forma(e.getX(), e.getY());
  266.  
  267. Graphics g_Imagem = Buffered_da_Imagem.createGraphics();
  268. g_Imagem.drawImage(Buffered_da_Reta, 0, 0, null);
  269. g_Imagem.dispose();
  270.  
  271. repaint();
  272. }
  273.  
  274.  
  275. public void mouseClicked(MouseEvent e) {
  276. x = e.getX();
  277. y = e.getY();
  278.  
  279. Forma(e.getX(), e.getY());
  280.  
  281. repaint(); // Atualiza a imagem do Jpanel
  282. }
  283.  
  284.  
  285. public void mouseEntered(MouseEvent e) {
  286. }
  287.  
  288.  
  289. public void mouseExited(MouseEvent e) {
  290. }
  291.  
  292.  
  293. public void mouseDragged(MouseEvent e) {
  294.  
  295. Forma(e.getX(), e.getY());
  296.  
  297. repaint();
  298. }
  299.  
  300.  
  301. public void mouseMoved(MouseEvent e) {
  302. }
  303.  
  304.  
  305. public void Forma(int x, int y){
  306.  
  307. if (valor==0)
  308. paint_retangulo(x, y);
  309. if (valor==1)
  310. paint_Circulo(x, y);
  311. if (valor==2)
  312. paint_Reta(x, y);
  313.  
  314. }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement