batiguason_11
Bovino adicto
- Desde
- 27 Jun 2009
- Mensajes
- 703
- Tema Autor
- #1
Que tal hermanos bakunos, vengo pidiendo ayuda en este pequeño "pasatiempo". Verán es un programa que permite dibujar figuras simples en un JFrame, actualmente lo estoy probando con un triangulo para lo cual uso un fillpolygon. Para crear varios objetos voy creando primero los objetos de tipo triangulo y los almaceno en un arraylist, cuando es momento de pintar los saco y con un 'cast' los paso de nuevo a objetos triangulo y empiezo a "pintar", el problema de esto es que no pinta en lo más mínimo. Supongo el problema debe estar en la parte del lienzo o en como pinto... Unos compañeros lograron resolver el problema pero usando arreglos y es lo que quiero evitar. Así que si me pueden ayudar se los agradecería infinitamente =D
Frame principal
Lienzo de dibujo
Objeto triangulo
Y claro el main.... hahaahahaha
Saludos ^-^
Frame principal
Código:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class frame extends JFrame implements ActionListener{
private JButton rec,tri,cir;
private JPanel botones;
Dibujo dibu;
static ArrayList alT=new ArrayList();
static ArrayList alR=new ArrayList();
static ArrayList AlC=new ArrayList();
frame(){
super("Panel de dibujo");
setSize(1280,800);
setResizable(false);
botones=new JPanel(new FlowLayout());
setLayout(new BorderLayout());
dibu=new Dibujo();
dibu.setBackground(Color.WHITE);
rec=new JButton("Rectángulo");
tri=new JButton("Triángulo");
cir=new JButton("Círculo");
botones.add(rec);
botones.add(tri);
botones.add(cir);
add(botones,BorderLayout.NORTH);
rec.addActionListener(this);
tri.addActionListener(this);
cir.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Object source=e.getSource();
if(source==rec)
dibu.rectangulo();
else if(source==tri)
dibu.triangulo();
else if(source==cir)
dibu.circulo();
}
}
Lienzo de dibujo
Código:
package figuras;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class frame extends JFrame implements ActionListener{
private JButton rec,tri,cir;
private JPanel botones;
Dibujo dibu;
static ArrayList alT=new ArrayList();
static ArrayList alR=new ArrayList();
static ArrayList AlC=new ArrayList();
frame(){
super("Panel de dibujo");
setSize(1280,800);
setResizable(false);
botones=new JPanel(new FlowLayout());
setLayout(new BorderLayout());
dibu=new Dibujo();
dibu.setBackground(Color.WHITE);
rec=new JButton("Rectángulo");
tri=new JButton("Triángulo");
cir=new JButton("Círculo");
botones.add(rec);
botones.add(tri);
botones.add(cir);
add(botones,BorderLayout.NORTH);
rec.addActionListener(this);
tri.addActionListener(this);
cir.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Object source=e.getSource();
if(source==rec)
dibu.rectangulo();
else if(source==tri)
dibu.triangulo();
else if(source==cir)
dibu.circulo();
}
}
Objeto triangulo
Código:
package figuras;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JOptionPane;
public class Triangulo {
int largo,alto;
int x[]=new int[3];
int y[]=new int[3];
Triangulo(){}
public void dibujar(Graphics g){
g.setColor(Color.orange);
g.fillPolygon(x,y,3);
g.setColor(Color.ORANGE);
}
public boolean asignar(int largo,int alto,int x,int y){
if(validaL(largo,x) && validaA(alto,y)){
this.largo=largo;
this.alto=alto;
this.x[0]=x;
this.y[0]=y;
this.x[1]=x+(largo/2);
this.y[1]=this.x[1]+alto;
this.x[2]=x+largo;
this.y[2]=y;
return false;
}
return true;
}
private boolean validaX(int x){
if(x>1280 || x<0){
JOptionPane.showMessageDialog(null,"Medida de x sobrepasa el limite!!");
return false;
}
return true;
}
private boolean validaY(int y){
if(y>800 || y<0){
JOptionPane.showMessageDialog(null,"Medida de y sobrepasa el limite!!");
return false;
}
return true;
}
private boolean validaL(int L,int x){
if(validaX(x))
if(L+x<=1280 || L>0)
return true;
JOptionPane.showMessageDialog(null,"Largo sobrepasa el limite!!");
return false;
}
private boolean validaA(int H,int y){
if(validaY(y))
if(H+y<=800 || H>0)
return true;
JOptionPane.showMessageDialog(null,"Altura sobrepasa el limite!!");
return false;
}
}
Y claro el main.... hahaahahaha
Código:
package figuras;
public class Main {
public static void main(String[] args) {
frame fr=new frame();
fr.setVisible(true);
}
}
Saludos ^-^