Reputation: 35
I'm supposed to draw this in Java
This is what I have so far:
DrawPanel.java
package drawpaneltest;
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
int d = 0;
while (d < 301) {
g.drawLine(0, d, width, height);
d += 15;
}
}
}
DrawPanelTest.java
package drawpaneltest;
import javax.swing.JFrame;
public class DrawPanelTest {
public static void main(String[] args) {
DrawPanel panel = new DrawPanel();
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel); // add the panel to the frame
application.setSize(250, 250); // set the size of the frame
application.setVisible(true); // make the frame visible
}
}
The above code sample currently displays the following:
What did I missed out? How should I add the curved line and the vertical line?
Upvotes: 3
Views: 5825
Reputation: 451
Here's my approach:
package drawpaneltest;
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
int x1 = 0, y1 = 0,
x2 = 0, y2 = height;
while (y1 < height) {
g.drawLine(x1, y1, x2, y2);
y1+=15; //You should modify this if
x2+=15; //it's not an equal square (like 250x250)
}
}
}
However in JPanel, coordinate (0,0) begin on the left top corner, not on left bottom corner as usual. And it still can not do well on a (m x n) squares, you have to do a lot more than this.
Upvotes: 1
Reputation: 1100
You should also change the to
coordinates (d
replacing width
):
g.drawLine(0, d, d, height);
Upvotes: 0