Java Graphic problem with getting correct generation on my graphic lines

27 Views Asked by At

I am trying to finish a project that I have been working on for fun on Java, and I'm genuinely trying to figure this out. Here is the code. I put a comment where the problem starts.

// Lab04vst.java
// 

import java.awt.*;
import java.applet.*;

public class Lab04vsthome extends Applet
{
    public void paint(Graphics g)
    {
        int width = 980;
        int height = 630;
        int x1 = 10;
        int y1 = 640;
        int x2 = 990;
        int y2 = 640;
        g.drawRect(10,10,width,height);

        int hdiv = height/50;
        int wdiv = width/50;

        //bottom right
        for(int k = 0; k <= 50; k++)
        {
            g.drawLine(x1,y1,x2,y2);
            y2 -= hdiv;
            x1 += wdiv;
        }   

        x1 = 10;
        y1 = 640;
        x2 = 990;
        y2 = 640;

        //bottom left
        for(int k = 0; k <= 50; k++)
        {
            g.drawLine(x1,y1,x2,y2);
            y1 -= hdiv;
            x2 -= wdiv;
        }   

        x1 = 10;
        y1 = 10;
        x2 = 990;
        y2 = 10;

        //top right
        for(int k = 0; k <= 50; k++)
        {
            g.drawLine(x1,y1,x2,y2);
            y2 += hdiv;
            x1 += wdiv;
        }   

        x1 = 10;
        y1 = 10;
        x2 = 990;
        y2 = 10;

        //top left
        for(int k = 0; k <= 50; k++)
        {
            g.drawLine(x1,y1,x2,y2);
            y1 += hdiv;
            x2 -= wdiv;
        }
        
        x1 = 10;
        y1 = 640;
        x2 = 990;
        y2 = 640;
        
        //problem starts here
        int width2 = 500;
        int height2 = 312;
        g.drawRect(250, 170, width2, height2);
        
        int x3 = 250;
        int y3 = 322;
        int x4 = 510;
        int y4 = 322;
        
        int w2div = width2 / 50;
        int h2div = height2 / 50;
        
        
        for (int k = 0; k <= 50; k++)
        {
            g.drawLine(x3,y3,x4,y4);
            y4 -= w2div;
            x3 += h2div;
        }
    }
}

It is supposed to generate the shapes on the outside of the box in the center, in the inside box. When I tried to make this happen with one of them, I can't figure out a way to move it and to stop it from going out, and I'm genuinely stumped at this point. The solution is probably obvious, but I'm newer to doing graphs and loops in Java so I'm stumped at this point. If it matters at all, I'm using BlueJ. Can anyone help?

0

There are 0 best solutions below