Results 1 to 12 of 12
- 06-06-2011, 01:57 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
Need Help Resizing a Line in Java Paint... (Spanish Code)
So far, and with a lot of effort I have managed to get this code:
I just want to get the lines that I draw to Move and to Resize, but I have no idea, It's gotta do something with the arrays I'm using, but I cannot find the problem.Java Code:import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; import java.util.*; public class ElenaCorel extends JFrame implements MouseListener, ActionListener { Color micolor=Color.red; Lienzo areaD; ButtonGroup grupo; JToolBar tools; JMenuBar barra; JMenu MenuArchivo,MenuEdicion,MenuAyuda; JMenuItem eNuevo,eAbrir,eGuardar,eSalir,eDeshacer,eRehacer,eCopiar,eCortar,ePegar,eColores, eAcercade; JToggleButton seleccion,linea,rectangulo,circulo, poligono, rotacion, texto, colores; int x1=0, y1=0, x2=0, y2=0; public static void main(String[] args) {new ElenaCorel();}//termina main public ElenaCorel() { barra=new JMenuBar(); MenuArchivo=new JMenu("Archivo"); eNuevo= new JMenuItem("Nuevo"); eAbrir= new JMenuItem("Abrir"); eGuardar=new JMenuItem("Guardar"); eSalir= new JMenuItem("Salir"); MenuEdicion=new JMenu("Edicion"); eDeshacer= new JMenuItem("Deshacer"); eRehacer = new JMenuItem("Rehacer"); eCopiar = new JMenuItem("Copiar"); eCortar = new JMenuItem("Cortar"); ePegar = new JMenuItem("Pegar"); eColores = new JMenuItem("Colores"); MenuAyuda=new JMenu("Ayuda"); eAcercade= new JMenuItem("Acerca de.."); tools= new JToolBar("Herramientas"); seleccion = new JToggleButton(new ImageIcon("sel.gif")); linea = new JToggleButton(new ImageIcon("line.gif")); rectangulo= new JToggleButton(new ImageIcon("rectangulo.gif")); circulo = new JToggleButton(new ImageIcon("circulo.gif")); poligono = new JToggleButton(new ImageIcon("poligono.gif")); rotacion = new JToggleButton(new ImageIcon("rotacion.gif")); colores = new JToggleButton(new ImageIcon("paleta.gif")); texto = new JToggleButton(new ImageIcon("text.gif")); grupo= new ButtonGroup(); grupo.add(seleccion); grupo.add(circulo); grupo.add(linea); grupo.add(poligono); grupo.add(rectangulo); grupo.add(rotacion); grupo.add(colores); grupo.add(texto); tools.add(seleccion); tools.add(circulo); tools.add(linea); tools.add(poligono); tools.add(rectangulo); tools.add(rotacion); tools.add(colores); tools.add(texto); barra.add(MenuArchivo); MenuArchivo.add(eNuevo); MenuArchivo.add(eAbrir); MenuArchivo.add(eGuardar); MenuArchivo.addSeparator(); MenuArchivo.add(eSalir); barra.add(MenuEdicion); MenuEdicion.add(eDeshacer); MenuEdicion.add(eRehacer); MenuEdicion.addSeparator(); MenuEdicion.add(eCopiar); MenuEdicion.add(eCortar); MenuEdicion.add(ePegar); MenuEdicion.addSeparator(); MenuEdicion.add(eColores); barra.add(MenuAyuda); MenuAyuda.add(eAcercade); add(tools,BorderLayout.NORTH); setJMenuBar(barra); areaD= new Lienzo(); areaD.addMouseListener(this); eColores.addActionListener(this); eAbrir.addActionListener(this); eGuardar.addActionListener(this); eSalir.addActionListener(this); eAcercade.addActionListener(this); eNuevo.addActionListener(this); eDeshacer.addActionListener(this); eRehacer.addActionListener(this); eCopiar.addActionListener(this); eCortar.addActionListener(this); ePegar.addActionListener(this); add(areaD); setTitle("ProyectoFinal: Corel de Elena"); setSize(800,500); //setSize(Toolkit.getDefaultToolkit().getScreenSize()); setVisible(true); }//termina el constructor public void mouseClicked(MouseEvent me) {if(seleccion.isSelected()) {areaD.selecciona(new Point(me.getX(),me.getY())); areaD.repaint();}//if } public void mouseEntered(MouseEvent me) {} public void mouseExited(MouseEvent me) {} public void mousePressed(MouseEvent me) {x1=me.getX(); y1=me.getY(); } public void mouseReleased(MouseEvent me) // cuando se suelta el boton del mouse {x2=me.getX(); y2=me.getY(); Figura figNueva= new Figura(); if (linea.isSelected()) {figNueva.nombre="Linea"; figNueva.color=(micolor); figNueva.coords.add(new Point(x1,y1)); figNueva.coords.add(new Point(x2,y2));}//if areaD.figuras.add(figNueva); areaD.repaint(); } public void actionPerformed(ActionEvent ae) {if(ae.getSource()==eSalir)//---------------------------------------------------------------------------------------------Salir.................=Listo {System.exit(0);} if(ae.getSource()==eAbrir)//---------------------------------------------------------------------------------------------Abrir................= 1/2 {JFileChooser jfc= new JFileChooser(); jfc.showOpenDialog(this); File F=jfc.getSelectedFile(); try{FileInputStream fos= new FileInputStream(F.getPath()); ObjectInputStream cos= new ObjectInputStream(fos); areaD.figuras=(ArrayList)cos.readObject(); areaD.repaint();}//try catch(Exception e) {JOptionPane.showMessageDialog(null, "Cuidado!, Cancelaste Abrir un archivo! :o");} }//if abrir if(ae.getSource()==eGuardar)//-------------------------------------------------------------------------------------------Guardar...............= 1/2 {JFileChooser jfc= new JFileChooser(); jfc.showSaveDialog(this); File F=jfc.getSelectedFile(); try{FileOutputStream fos= new FileOutputStream(F.getPath()); ObjectOutputStream cos= new ObjectOutputStream(fos); cos.writeObject(areaD.figuras);}//try catch(Exception e) {JOptionPane.showMessageDialog(null, "Cuidado!, No se terminaste el Proceso de Guardar! :( ??");}//catch }//if guardar if(ae.getSource()==eColores)//------------------------------------------------------------------------------------------Colores...............=Listo {JColorChooser jcc=new JColorChooser(); micolor=jcc.showDialog(this,"Elige un color", Color.red);}//if if(ae.getSource()==eAcercade)//-----------------------------------------------------------------------------------------A cerca de............=Listo //if(eAcercade.isSelected()) {JOptionPane.showMessageDialog(null, "Informaciòn del Proyecto: \n Este programa es designado como proyecto final\n para la materia de Metodos de Programacion 2 \n el cual consiste en un programa para dibujar \n con una interface del tipo PAINT. \n \n Desarrollado por la alumna Elena Flores,\n estudiante de IDGD.\n\n ----------------------------=(^.^)=----------------------------");} if(ae.getSource()==eNuevo)//--------------------------------------------------------------------------------------------Nuevo Doc ............=Listo {areaD.figuras.clear(); areaD.repaint();} if(ae.getSource()==eDeshacer)//-----------------------------------------------------------------------------------------Deshacer {} if(ae.getSource()==eRehacer)//------------------------------------------------------------------------------------------Rehacer {} if(ae.getSource()==eCopiar)//-------------------------------------------------------------------------------------------Copiar {} if(ae.getSource()==eCortar)//-------------------------------------------------------------------------------------------Cortar {} if(ae.getSource()==ePegar)//--------------------------------------------------------------------------------------------Pegar {} }//action performance }//termina la clase ElenaCorel class Lienzo extends JPanel {int a=0,b=0,c=0,d=0; ArrayList figuras=new ArrayList();//guarda atributos de figuras public Lienzo() {setBackground(Color.white);}//termina el constructor public void paint(Graphics g) {Graphics2D g2=(Graphics2D)g; super.paintComponent(g2); g2.setColor(Color.red); BasicStroke bs= new BasicStroke(5.03f); g2.setStroke(bs); for(int x=0;x<figuras.size();x++) {Point uno=new Point(); Point dos=new Point(); Figura temp=(Figura)figuras.get(x); g2.setColor(temp.color); if (temp.nombre.equals("Linea")) {uno=(Point)temp.coords.get(0); dos=(Point)temp.coords.get(1); g2.drawLine(uno.x,uno.y,dos.x,dos.y); if(temp.seleccionado==true) {g2.setColor(Color.black); g2.drawRect(uno.x-4,uno.y-4,6,6); g2.drawRect(dos.x-4,dos.y-4,6,6);}//if }//if }//for }//public void paint public void selecciona(Point punto) {int cont=0; while(cont<figuras.size()) {Point pt1=new Point (), pt2=new Point(); Figura temp=(Figura)figuras.get(cont);//sus coordenadas estan en coords if(temp.coords.size()>1&&temp!=null) { pt1=(Point)temp.coords.get(0); pt2=(Point)temp.coords.get(1);} if(puntoEnLinea(punto,pt1,pt2)) //comprobacion {temp.seleccionado=true;}//if else temp.seleccionado=false;cont ++; }//while }//selecciona public boolean puntoEnLinea(Point p, Point pt1, Point pt2) {double x1=pt1.x, y1=pt1.y, x2=pt2.x, y2=pt2.y, x3=p.x, y3=p.y; double d1=Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2)); double d2=Math.sqrt(Math.pow(x3-x1,2)+Math.pow(y3-y1,2)); double d3=Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2)); if((d2+d3)-d1<0.1) return true; else return false; //FALTA }//punto en linea }//termina la clase Lienzo class Figura implements Serializable {Color color= Color.magenta; String nombre=""; ArrayList coords= new ArrayList(); boolean seleccionado=false; }
Can anyone help? Thank you in advance...
- 06-06-2011, 02:10 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,146
- Rep Power
- 5
I have no idea what your problem is, but the following:
should be:Java Code:public void paint(Graphics g) {Graphics2D g2=(Graphics2D)g; super.paintComponent(g2);
Java Code:public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2=(Graphics2D)g;
- 06-06-2011, 02:20 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
This code is a Paint like application... I have a button that says 'linea' which is a code to draw a line within a given x, y coordinates.
It draws a line. There's a part called 'selecciona' that means select. I want to know if there's a way so when I select 'selecciona'
that I can resize and move the line I drew in the JPanel.
- 06-06-2011, 02:42 AM #4
Does that mean changing the x,y pairs that define the line?f there's a way so when I select 'selecciona' that I can resize and move the line
What is "selecciona"? a button, an area on the JPanel or ?
How do you select it? Button press, mouse move or drag ?
- 06-06-2011, 02:48 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
It's a JToggleButton. You press it with the other JToggleButtons. These are the "tools" for paint. Poligono is Polygon Drawing, Colores is Color and so on.Java Code:[B]seleccion = new JToggleButton(new ImageIcon("sel.gif")); [/B] linea = new JToggleButton(new ImageIcon("line.gif")); rectangulo= new JToggleButton(new ImageIcon("rectangulo.gif")); circulo = new JToggleButton(new ImageIcon("circulo.gif")); poligono = new JToggleButton(new ImageIcon("poligono.gif")); rotacion = new JToggleButton(new ImageIcon("rotacion.gif")); colores = new JToggleButton(new ImageIcon("paleta.gif")); texto = new JToggleButton(new ImageIcon("text.gif"));
I would like to know if there's a way, after I've drawn a line on the class "Lienzo" (which means canvas in Spanish and is a JPanel), that I click on the 'seleccion' JToggleButton, and then be able to resize and move the line on the JPanel.
EDIT: I can make it so when I press 'seleccion', I can select the line which I've drawn on the JPanel. But I can't do anything with it.Last edited by Andyx1337; 06-06-2011 at 02:51 AM.
- 06-06-2011, 02:57 AM #6
Sorry, I don't read Spanish. I am unable to user your GUI as I don't understand the texts.
An idea: Add a ResourceBundle to the code to allow mulitple language texts.
You need to add: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Last edited by Norm; 06-06-2011 at 02:59 AM.
- 06-06-2011, 03:07 AM #7
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
It's okay, I'm trying to translate the code so you could help me as well :)
- 06-06-2011, 03:08 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
What is the user supposed to do to move a line? At the moment when the user has the seleccion button selected and they click on a line then that figura is marked as seleccionado. Then the repaint causes the line to be shown as selected to the user. But other than that there is no user gesture that says "move this line".
Perhaps you should do the line detection when the mouse is depressed (beep if there is no line there!) then when the mouse is released have el lienzo update the end points of la figura seleccionada by (x2-x1,y2-y1). There are other ways like detecting drags and updating continuously but a single end-start would be the easiest to implement.
-----
It strikes me that your class has a lot of code that might be getting in the way. Perhaps you could start the drawing program simply with just lines. (Another thing that would make the code more easy to follow would be to use text rather than images on the buttons - after all you can make the interface pretty later on.)
I also wonder if Figura should be an interface with concrete classes for each of the shapes. The interface should declare methods for "hit detection" (like selecciona(Point punto) but returning a boolean) and a method to update the coordinate array based on a supplied translation (this may or may not be as simple as translating each coordinate depending on the particular type of figura). Figura could also do some of the work of repainting - ie have a method that is called by el lienzo. Basically I'm saying move some of the code out of Lienzo and into Figura that way different types of shape can be accomodated later.
- 06-06-2011, 03:12 AM #9
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
- 06-06-2011, 03:35 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
In terms of posting here you could simplify the code by removing all of the menus and their associated handler code. And half the buttons. This (together with using text on the remaining buttons) would eliminate more than half the code but still leave the problem. And leave it in a way that others can run and test. This cutting back (often referred to as a SSCCE) involves a "step backwards" but is a great help in diagnosing and comunicating a problem.What could be simplified? Are you telling me that some of my code is making me struggle?
The biggest problem with the code is that the methods are getting too big. It's not so much removing code, as splitting it up into smaller units each of which can be described and tested. Don't use "this" as a handler for everything: give each action (new/open/save etc) their own handler. And move code out of Lienzo and into Figura if it is specific to the shape.
Unless I have missed something your code doesn't provide the user with any way of indicating where they want to line moved to. Did you understand what I said about moving the line selecting code so that it occurs when the mouse is depressed raher than clicked? And updating the coordinates when the mouse is released?I just want the linea to move around the JPanel.
- 06-06-2011, 04:09 AM #11
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
I am just starting so I don't know that much, all I know is I think my teacher doesn't know that much.
Either way, I guess, I have to ask... Is there way so I can draw a line, select the line after it has been drawn and then be able to resize the line or move it?
- 06-06-2011, 01:34 PM #12
Yes, once you get the x,y pairs for the end of the line, add code to paintComponent to draw the lineIs there way so I can draw a line,
A line can be only a pixel wide. It could be hard for a user to click exactly on the line to select it.select the line after it has been drawn
How would you want the user to "select" a line? Perhaps put all the lines in a list. Go into select mode and flash the lines one at a time and have the user press a button when you are flashing the one he wants to select.
Or perhaps select a line by dragging the mouse across it.
Again a problem with the width of the line. What mouse motion to move it? What mouse motion to resize it?then be able to resize the line or move it?
Once selected if the mouse click is very near an end, go into resize mode (change the cursor) and move the end of the line in synch with the mouse motion. It the mouse click is near the middle, go into move mode and move the line in x and y directions in synch with the mouse motion.
Similar Threads
-
Constructors pls explan the code line by line in comments
By vibaviattigala in forum New To JavaReplies: 1Last Post: 02-19-2011, 04:03 AM -
Flicker with resizing and need general code examination
By MrFreeweed in forum Java 2DReplies: 4Last Post: 01-13-2011, 05:30 PM -
New to Java, Line of code is stumping me
By Maverrique in forum New To JavaReplies: 4Last Post: 12-14-2010, 07:52 PM -
java spanish translator needed for jni
By Nicholas Jordan in forum Jobs OfferedReplies: 5Last Post: 08-11-2009, 03:43 PM -
Forum Java in spanish
By koko10ar in forum Reviews / AdvertisingReplies: 1Last Post: 08-12-2008, 11:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks