Professional Documents
Culture Documents
Tarea de Graficacion
Tarea de Graficacion
import java.awt.Graphics;
import javax.swing.JPanel;
super.paintComponent(g);
int u1 = 0;
int u2 = 0;
int v1 = 0;
int v2 = 0;
while (drawCounter2 <= 15){
u2 = 250;
g.drawLine(u1, v1, u2, v2);
u1 = u1 + 15;
v2 = v2 + 15;
drawCounter2++;
}
int a1 = 0;
int a2 = 0;
int b1 = 0;
int b2 = 0;
}
}
}
Here is my runner class
package GUIstuf;
import javax.swing.JFrame;
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize (250, 250);
application.setVisible(true);
}
I have a the lines in the bottom left and the upper right but when I try to subtract from x I just
get lines going a crossed the whole box.
Abajo
When doing custom painting you should override the getPreferredSize() method so the panel
can be displayed at its preferred size.
When you draw the lines two variable are the same and two variables differ. Use the
width/height variable when appropriate instead of hardcoding a number. In the example below I
did the left and bottom sides. The bottom side shows how to subtract. I'll let you figure out the
pattern for the other two side.
Also, I made the panel a little more dynamic so it will be easy to configure the number of lines
you want painted and the gap between the lines.
import java.awt.*;
import javax.swing.*;
@Override
public Dimension getPreferredSize()
{
int size = lines * lineGap;
return new Dimension(size, size);
}
@Override
public void paintComponent(Graphics g)
{
int width = getWidth();
int height = getHeight();
x = 0;
y = height - lineGap;