Java question - Not NSFW

Eoin

The Bug Hunter
Moderator
Donor
Feb 21, 2017
1,231
4,814
Does anybody know how to get an isosceles triangle to get to a 90 degree angle in the top right corner?

Fuck that, I meant how do I make a right angled triangle in Java using Graphics?

You don't have permission to view the spoiler content. Log in or register now.

This is my method so far:

Code:
public void paint(Graphics g)
    {
        super.paint(g);

        int xpoints[] = {250, 200, 100, 250, 200};
        int ypoints[] = {175, 250, 125, 125, 200};
        int npoints = 3;

        //g.drawOval(399,419,407,450);
        g.drawPolygon(xpoints,ypoints,npoints);
       //g.fillOval(399,219,407,450);
    }
This is the output of that method in a class:
isoTriangle.PNG

Should I cut down on the number of points in the arrays, or should I leave it be?

Thanks.
 

Eoin

The Bug Hunter
Moderator
Donor
Feb 21, 2017
1,231
4,814
Right, so I fine-tuned this a bit. Turns out I don't really need the last two points in x and y.

Here's my updated code:
Code:
public void paint(Graphics g)
    {
        super.paint(g);

        int xpoints[] = {250, 200, 100};
        int ypoints[] = {175, 250, 125};
        int npoints = 3;
 
        g.drawPolygon(xpoints,ypoints,npoints);
       
    }
 

Eoin

The Bug Hunter
Moderator
Donor
Feb 21, 2017
1,231
4,814
Just as I posted the last reply, I figured it out, with help from a friend.

Here's the entire class:
Code:
//Authored by: Eoin
//Date: 4/12/17

//Program is to make 2 boats with the fill methods

import javax.swing.*;
import java.awt.*;

public class Boats extends JFrame
{

    public void paint(Graphics g)
    {

        super.paint(g);

        //This is the 1st boat, black
        int xpoints[] = {50,150,150,200,200,280,280,100,50};
        int ypoints[] = {200,200,150,150,200,200,250,250,200};
        int npoints = xpoints.length;

        g.fillPolygon(xpoints,ypoints,npoints);

        //This is the 2nd boat, red
        int xpoints2[] = {200,300,300,350,350,430,430,250,200};
        int ypoints2[] = {350,350,300,300,350,350,400,400,350};
        int npoints2 = xpoints2.length;

        g.setColor(Color.RED);
        g.fillPolygon(xpoints2,ypoints2,npoints2);

    }//end paint


    public Boats()
    {

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(1280,720);
        setVisible(true);

    }//end method

    public static void main(String[] arshe)
    {

        new Boats();

    }//end main

}//end class
 

Cyan

Member
Jul 25, 2017
126
551
I probably wouldn't have done it like that. Using hardcoded coordinates on a forced 1280x720 is going to look weird using any other sizes, even outside of 16:10.

I recommend creating anchor points and transforming the scale (assuming same aspect ratio) to make it fit with a wider range of sizes. Of course, I don't know the specifics of what you're creating or how you're creating it, so you could have already figured that small problem out.
 

Eoin

The Bug Hunter
Moderator
Donor
Feb 21, 2017
1,231
4,814
I probably wouldn't have done it like that. Using hardcoded coordinates on a forced 1280x720 is going to look weird using any other sizes, even outside of 16:10.

I recommend creating anchor points and transforming the scale (assuming same aspect ratio) to make it fit with a wider range of sizes. Of course, I don't know the specifics of what you're creating or how you're creating it, so you could have already figured that small problem out.
So for setSize, I could set it to be 50%?

I usually set it to be 600*600, or 800*800, but 1280*720 was just for testing.
 

Cyan

Member
Jul 25, 2017
126
551
So for setSize, I could set it to be 50%?

I usually set it to be 600*600, or 800*800, but 1280*720 was just for testing.
You could.

What I like to do for game elements/windows for example, is to use a very small scale - say 640x480 contrasted with a high 7860x4320 resolution (no one would actually use a resolution that high), simply to make sure there's nothing fishy going on with scaling/cuttoffs.

That way, a triangle looks exactly the same on-screen, regardless of the size of the resolution.

triangle1.jpg

vs

triangle2.png

This second image is what happens when you scale up the resolution 1280x720 -> 3840->2160 when using hardcoded values for an element. The triangles in both images are 500x200 pixels. Obviously the solution is a lot easier with actual images, since you just make sure the base image is at least as high (in pixels) as your highest possible supported resolution.

For manually drawn graphics (like triangles), there are a number of different ways to do it - I assume you're using java, but I am unaware which version, or if you are using any custom modules that have been created to make handling elements like this much, much simpler.

AfflineTransform I believe is a class that is natively supported by java.

P.S. - I kinda rambled there for a bit sorry, hope you understand what I was talking about with the anchors/resolution being weird
 
  • Like
Reactions: Eoin

Eoin

The Bug Hunter
Moderator
Donor
Feb 21, 2017
1,231
4,814
You could.

What I like to do for game elements/windows for example, is to use a very small scale - say 640x480 contrasted with a high 7860x4320 resolution (no one would actually use a resolution that high), simply to make sure there's nothing fishy going on with scaling/cuttoffs.

That way, a triangle looks exactly the same on-screen, regardless of the size of the resolution.

View attachment 46894

vs

View attachment 46895

This second image is what happens when you scale up the resolution 1280x720 -> 3840->2160 when using hardcoded values for an element. The triangles in both images are 500x200 pixels. Obviously the solution is a lot easier with actual images, since you just make sure the base image is at least as high (in pixels) as your highest possible supported resolution.

For manually drawn graphics (like triangles), there are a number of different ways to do it - I assume you're using java, but I am unaware which version, or if you are using any custom modules that have been created to make handling elements like this much, much simpler.

AfflineTransform I believe is a class that is natively supported by java.

P.S. - I kinda rambled there for a bit sorry, hope you understand what I was talking about with the anchors/resolution being weird
Yeah, I understand. Thanks for the info!