Como llamar un JPanel que grafique un Triangulo o un Cuadrado
Este es mi panel Como haer que me grafique
public class JPanel1 extends JPanel{
private int wid = 600, x1 = 100, x3= 210, y1 = 20, x2 = wid-x1+10, y2 = y1+80,
hLen = x2-x1, vLen = y2-y1, gap = vLen/5, n, lengthMax, textHt = 12;
private float a,b,c,d,e;
private String memFunc;
public void paint (Graphics g){
// Dibuja los ejes x
g.setColor(java.awt.Color.blue);
g.drawLine(x1, y2, x2, y2);
// Dibuja los ejes y
g.setColor(java.awt.Color.red);
g.drawLine(x3, y1, x3, y2);
// Dibuja coordenadas verticales y sus valores
g.setColor(java.awt.Color.black);
g.setFont(new Font("SansSerif", Font.PLAIN, textHt));
g.drawString("1.0", x1 - 25, y1 + gap / 4);
g.drawLine(x1 - 5, y1 + gap, x1 + 5, y1 + gap);
g.drawString("0.8", x1 - 25, y1 + gap + gap / 4);
g.drawLine(x1 - 5, y1 + gap * 2, x1 + 5, y1 + gap * 2);
g.drawString("0.6", x1 - 25, y1 + gap * 2 + gap / 4);
g.drawLine(x1 - 5, y1 + gap * 3, x1 + 5, y1 + gap * 3);
g.drawString("0.4", x1 - 25, y1 + gap * 3 + gap / 4);
g.drawLine(x1 - 5, y1 + gap * 4, x1 + 5, y1 + gap * 4);
g.drawString("0.2", x1 - 25, y1 + gap * 4 + gap / 4);
g.drawString("0.0", x1 - 25, y1 + gap * 5 + gap / 4);
if (memFunc.equals("tri")) {
g.drawLine( (int) (a + x3), y2, (int) (b + x3), y1); // /
g.drawLine( (int) (b + x3), y1, (int) (c + x3), y2); //
}
if (memFunc.equals("tra")) {
g.drawLine( (int) (a + x3), y2, (int) (b + x3), y1); // /
g.drawLine( (int) (b + x3), y1, (int) (c + x3), y1); // -
g.drawLine( (int) (c + x3), y1, (int) (d + x3), y2); //
}
if (memFunc.equals("gau")) {
int oldx1 = x3, oldy1 = y1, newx2 = x3, newy2 = y1;
for (int j = (int) a; j <= c; j++) {
newx2 = (int) ( (j + x3));
if (j == a) {
oldx1 = (int) (j + x3);
oldy1 = y2;
newy2 = (int) (y2 - Math.exp( -sqr( (j - b) / (b - a)) / 2) * vLen);
}
else if (j < b)
newy2 = (int) (y2 - Math.exp( -sqr( (j - b) / (b - a)) / 2) * vLen);
else if (j == b)
newy2 = y1;
else if ( (j > b) && (j <= c))
newy2 = (int) (y2 - Math.exp( -sqr( (j - b) / (c - b)) / 2) * vLen);
g.drawLine(oldx1, oldy1, newx2, newy2);
oldx1 = newx2;
oldy1 = newy2;
}
}
if (memFunc.equals("bel")) {
int oldx1 = x3, oldy1 = y1, newx2 = x3, newy2 = y1;
for (int j = (int) a; j <= e; j++) {
newx2 = (int) ( (j - (a + x3)));
if (j == a) {
oldx1 = (int) ( (j - a) + x3);
oldy1 = y2;
newy2 = (int) (y2 - 1 / (1 + Math.exp(2 * b * Math.log( (c - j) / (c - a)))) * vLen);
}
else if (j < c)
newy2 = (int) (y2 - 1 / (1 + Math.exp(2 * b * Math.log( (c - j) / (c - a)))) * vLen);
else if (j == c)
newy2 = y1;
else if ( (j > c) && (j <= e))
newy2 = (int) (y2 - 1 / (1 + Math.exp(2 * d * Math.log( (j - c) / (e - c)))) * vLen);
g.drawLine(oldx1, oldy1, newx2, newy2);
oldx1 = newx2;
oldy1 = newy2;
}
}
g.drawLine( x3, y2, x3+50, y1); // /
g.drawLine( x3+50, y1, x3+100, y2); //
}
public class JPanel1 extends JPanel{
private int wid = 600, x1 = 100, x3= 210, y1 = 20, x2 = wid-x1+10, y2 = y1+80,
hLen = x2-x1, vLen = y2-y1, gap = vLen/5, n, lengthMax, textHt = 12;
private float a,b,c,d,e;
private String memFunc;
public void paint (Graphics g){
// Dibuja los ejes x
g.setColor(java.awt.Color.blue);
g.drawLine(x1, y2, x2, y2);
// Dibuja los ejes y
g.setColor(java.awt.Color.red);
g.drawLine(x3, y1, x3, y2);
// Dibuja coordenadas verticales y sus valores
g.setColor(java.awt.Color.black);
g.setFont(new Font("SansSerif", Font.PLAIN, textHt));
g.drawString("1.0", x1 - 25, y1 + gap / 4);
g.drawLine(x1 - 5, y1 + gap, x1 + 5, y1 + gap);
g.drawString("0.8", x1 - 25, y1 + gap + gap / 4);
g.drawLine(x1 - 5, y1 + gap * 2, x1 + 5, y1 + gap * 2);
g.drawString("0.6", x1 - 25, y1 + gap * 2 + gap / 4);
g.drawLine(x1 - 5, y1 + gap * 3, x1 + 5, y1 + gap * 3);
g.drawString("0.4", x1 - 25, y1 + gap * 3 + gap / 4);
g.drawLine(x1 - 5, y1 + gap * 4, x1 + 5, y1 + gap * 4);
g.drawString("0.2", x1 - 25, y1 + gap * 4 + gap / 4);
g.drawString("0.0", x1 - 25, y1 + gap * 5 + gap / 4);
if (memFunc.equals("tri")) {
g.drawLine( (int) (a + x3), y2, (int) (b + x3), y1); // /
g.drawLine( (int) (b + x3), y1, (int) (c + x3), y2); //
}
if (memFunc.equals("tra")) {
g.drawLine( (int) (a + x3), y2, (int) (b + x3), y1); // /
g.drawLine( (int) (b + x3), y1, (int) (c + x3), y1); // -
g.drawLine( (int) (c + x3), y1, (int) (d + x3), y2); //
}
if (memFunc.equals("gau")) {
int oldx1 = x3, oldy1 = y1, newx2 = x3, newy2 = y1;
for (int j = (int) a; j <= c; j++) {
newx2 = (int) ( (j + x3));
if (j == a) {
oldx1 = (int) (j + x3);
oldy1 = y2;
newy2 = (int) (y2 - Math.exp( -sqr( (j - b) / (b - a)) / 2) * vLen);
}
else if (j < b)
newy2 = (int) (y2 - Math.exp( -sqr( (j - b) / (b - a)) / 2) * vLen);
else if (j == b)
newy2 = y1;
else if ( (j > b) && (j <= c))
newy2 = (int) (y2 - Math.exp( -sqr( (j - b) / (c - b)) / 2) * vLen);
g.drawLine(oldx1, oldy1, newx2, newy2);
oldx1 = newx2;
oldy1 = newy2;
}
}
if (memFunc.equals("bel")) {
int oldx1 = x3, oldy1 = y1, newx2 = x3, newy2 = y1;
for (int j = (int) a; j <= e; j++) {
newx2 = (int) ( (j - (a + x3)));
if (j == a) {
oldx1 = (int) ( (j - a) + x3);
oldy1 = y2;
newy2 = (int) (y2 - 1 / (1 + Math.exp(2 * b * Math.log( (c - j) / (c - a)))) * vLen);
}
else if (j < c)
newy2 = (int) (y2 - 1 / (1 + Math.exp(2 * b * Math.log( (c - j) / (c - a)))) * vLen);
else if (j == c)
newy2 = y1;
else if ( (j > c) && (j <= e))
newy2 = (int) (y2 - 1 / (1 + Math.exp(2 * d * Math.log( (j - c) / (e - c)))) * vLen);
g.drawLine(oldx1, oldy1, newx2, newy2);
oldx1 = newx2;
oldy1 = newy2;
}
}
g.drawLine( x3, y2, x3+50, y1); // /
g.drawLine( x3+50, y1, x3+100, y2); //
}
Hola:
Pues pintar, parece que pinta un montón de cosas. ¿Cual es el problema?
En principio debes meterlo en un JFrame y visualizar el JFrame.
JFrame v = new JFrame();
v.getContentPane().add(tuPanel);
v.pack();
v.setVisible(true);
Con esto debería salir una ventana con tu dibujo. Si sale la ventana sin dibujo, quizás tus cuentas hagan que el dibujo salga fuera de la ventana.
Se bueno
Pues pintar, parece que pinta un montón de cosas. ¿Cual es el problema?
En principio debes meterlo en un JFrame y visualizar el JFrame.
JFrame v = new JFrame();
v.getContentPane().add(tuPanel);
v.pack();
v.setVisible(true);
Con esto debería salir una ventana con tu dibujo. Si sale la ventana sin dibujo, quizás tus cuentas hagan que el dibujo salga fuera de la ventana.
Se bueno
