Part One Question: Figure I Want: My Current Code: import javax.swing.*; import
ID: 3534513 • Letter: P
Question
Part One Question:
Figure I Want:
My Current Code:
import javax.swing.*;
import java.awt.Graphics;
public class Lab13Part1 extends JPanel
{
public void paintComponent( Graphics g )
{
super.paintComponent( g );
int width = getWidth();
int height = getHeight();
int number, x, y, dx, dy;
x = 0;
y = height;
number = 15;
dx = width / number;
dy = height / number;
for( int i = 1; i < number; i++ )
{
x += dx;
y -= dy;
g.drawLine( 0, 0, x, y );
g.drawLine(width, height, x, y);
}
}
public static void main(String[] args)
{
JFrame app = new JFrame();
Lab13Part1 panel = new Lab13Part1();
app.add(panel);
app.setTitle("Lab13Part1");
app.setSize(300,300);
app.setLocationRelativeTo(null);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
}
My Current Figure:
Part Two Question:
Figure I Want:
My Current Code:
import javax.swing.*;
import java.awt.Graphics;
public class Lab13Part2 extends JPanel
{
public void paintComponent( Graphics g )
{
super.paintComponent( g );
int width = getWidth();
int height = getHeight();
int number, dx, dy;
number = 15;
dx = width / number;
dy = height / number;
for( int i = 0; i <= number; i++ )
{
g.drawLine(0,i*dy,(i+1)*dx,height);
}
}
public static void main(String[] args)
{
JFrame app = new JFrame();
Lab13Part2 panel = new Lab13Part2();
app.add(panel);
app.setTitle("Lab13Part2");
app.setSize(300,300);
app.setLocationRelativeTo(null);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
}
My Current Figure:
Explanation / Answer
import java.awt.*;
import javax.swing.*;
public class Lab13Part1 extends JPanel
{
public void paintComponent( Graphics g )
{
super.paintComponent( g );
int width = getWidth();
int height = getHeight();
int number, x, y, dx, dy;
x = 0;
y = height;
number = 15;
dx = width / number;
dy = height / number;
for( int i = 1; i < number; i++ )
{
x += dx;
y -= dy;
g.drawLine( 0, 0, x, y );
g.drawLine(width, height, x, y);
}
x = width;
y = height;
dx = width / number;
dy = height / number;
for( int i = 1; i < number; i++ )
{
x -= dx;
y -= dy;
g.drawLine( width, 0, x, y );
g.drawLine(0, height, x, y);
}
}
public static void main(String[] args)
{
JFrame app = new JFrame();
Lab13Part1 panel = new Lab13Part1();
app.add(panel);
app.setTitle("Lab13Part1");
app.setSize(300,300);
app.setLocationRelativeTo(null);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
}
Note: Working on Part 2, i will post it, as soon as i finish it