| 
	
		|  Autor | Tema: Cambiar color al pintar (JComponent)  (Leído 2,709 veces) |  
	| 
			| 
					
						| estudianteuv 
								
								 Desconectado 
								Mensajes: 10
								
								
								
								
								
								   | 
 
Sinceramente soy malo en JAVA. Tengo una duda, ¿cómo podría cambiar de color a la hora de pintar en el canvas? Algo así como una paleta de colores. No sé como se podría hacer, ayuda! package com.mycompany.mavenproject3;
 import java.awt.Color;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.Point;
 import java.awt.Shape;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseMotionAdapter;
 import java.awt.geom.Line2D;
 import java.util.ArrayList;
 import javax.swing.JComponent;
 import javax.swing.JFrame;
 
 
 public class NewClass extends JComponent  {
 
 private Point inicioArrastre;
 private Point finArrastre;
 private ArrayList<Shape> lineas = new ArrayList<Shape>();
 
 public NewClass() {
 super();
 
 addMouseListener(new MouseAdapter() {
 
 public void mousePressed(MouseEvent e) { // cuando se presiona el mouse
 inicioArrastre = new Point(e.getX(), e.getY());
 repaint();
 }
 
 public void mouseReleased(MouseEvent e) { // cuando se deja de presionar el mouse
 finArrastre = new Point(e.getX(), e.getY());
 Shape linea = crearLinea(inicioArrastre.x, inicioArrastre.y, finArrastre.x, finArrastre.y);
 lineas.add(linea);
 repaint();
 }
 });
 addMouseMotionListener(new MouseMotionAdapter() {
 
 public void mouseDragged(MouseEvent e) { // cuando se esta arrastrando el mouse
 finArrastre = new Point(e.getX(), e.getY());
 Shape linea = crearLinea(inicioArrastre.x, inicioArrastre.y, finArrastre.x, finArrastre.y);
 lineas.add(linea);
 inicioArrastre = new Point(finArrastre.x, finArrastre.y);
 repaint();
 }
 });
 
 }
 
 public void paint(Graphics g) {
 Graphics2D g2 = (Graphics2D) g;
 g2.setColor(Color.BLUE);
 for (Shape linea : lineas) { // dibuja todos las elipses
 g2.draw(linea);
 }
 }
 
 private Line2D.Float crearLinea(int x1, int y1, int x2, int y2) {
 return new Line2D.Float(x1, y1, x2, y2);
 }
 
 public static void main(String[] a3d) {
 
 JFrame ventana = new JFrame("Dibujar");
 ventana.setSize(800, 800);
 ventana.setLocationRelativeTo(null);
 ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 ventana.add(new NewClass());
 ventana.setVisible(true);
 
 
 }
 
 
 
 }
 
 |  
						| 
								|  |  
								| « Última modificación: 13 Mayo 2019, 02:57 am por estudianteuv » |  En línea | 
 
 |  |  |  | 
			| 
					
						| rub'n 
								       
								
								 Desconectado 
								Mensajes: 1.223
								
								 
								(e ->  λ("live now")); tatuar -> λ("α");
								
								
								
								
								
								     | 
 
* Linea 30 a 32, contiene el listener del JButton para abrir la paleta y obtener el color* Linea 64 se le setea el color obtenido al metodo  g2.setColor(Color c)Acostumbrate a usar GeShi
 package com.mycompany.mavenproject3; import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionAdapter;import java.awt.geom.Line2D;import java.util.ArrayList;import javax.swing.*;  /* * @author */     private Point inicioArrastre;    private Point finArrastre;    private final ArrayList<Shape> lineas = new ArrayList<>();      public NewClass() {        super();         /**         * Listener del boton para abrir la paleta de colores         */        jButton.addActionListener(e -> {        });              public void mousePressed(MouseEvent  e) { // cuando se presiona el mouse                inicioArrastre = new Point( e.getX() , e.getY());                repaint();            }             public void mouseReleased(MouseEvent  e) { // cuando se deja de presionar el mouse                finArrastre = new Point( e.getX() , e.getY());                Shape linea =  crearLinea( inicioArrastre.x , inicioArrastre.y , finArrastre.x , finArrastre.y);                lineas.add(linea);                repaint();            }        });             public void mouseDragged(MouseEvent  e) { // cuando se esta arrastrando el mouse                finArrastre = new Point( e.getX() , e.getY());                Shape linea =  crearLinea( inicioArrastre.x , inicioArrastre.y , finArrastre.x , finArrastre.y);                lineas.add(linea);                inicioArrastre = new Point( finArrastre.x , finArrastre.y);                repaint();            }        });     }     @Override        g2.setColor(this.color);        for (Shape linea :  lineas) { // dibuja todos las elipses            g2.draw(linea);        }    }     private Line2D.Float crearLinea(int  x1, int  y1, int  x2, int  y2) {    }     public static void main(String[]  a3d) {         try {            ex.printStackTrace();        }            ventana.setSize(800, 800);            ventana.setLocationRelativeTo(null);            ventana.setDefaultCloseOperation(JFrame .EXIT_ON_CLOSE);             jButton.setContentAreaFilled(false);             jPanel.add(Box .createVerticalStrut(20));            jPanel.add(new NewClass());            ventana.add(jPanel);            ventana.setVisible(true);        }).start();    }}
 
 
 |  
						| 
								|  |  
								| « Última modificación: 15 Mayo 2019, 02:53 am por rub'n » |  En línea | 
 
 If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen king |  |  |  |  |  
 
	
 
 
				
					
						| Mensajes similares |  
						|  | Asunto | Iniciado por | Respuestas | Vistas | Último mensaje |  
						|   |   | Cambiar color Diseño Gráfico
 | alkan | 4 | 4,837 |  4 Enero 2006, 20:26 pm por alkan
 |  
						|   |   | Cambiar color de ojos (pro)
							« 1 2 » Diseño Gráfico
 | Azielito | 12 | 7,755 |  22 Julio 2007, 21:44 pm por Lauta
 |  
						|   |   | [c] Cambiar de color? Programación C/C++
 | Dacan | 7 | 21,386 |  19 Marzo 2013, 06:41 am por MessageBoxA
 |  
						|   |   | cambiar el color Desarrollo Web
 | bengy | 0 | 2,229 |  28 Octubre 2016, 00:56 am por bengy
 |  
						|   |   | Cambiar color c++ Programación C/C++
 | Pool9 | 0 | 2,186 |  3 Mayo 2018, 05:35 am por Pool9
 |    |