Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7996713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:46:30+00:00 2026-06-04T14:46:30+00:00

I have written a code that draws grids and a triangle inside one grid

  • 0

I have written a code that draws grids and a triangle inside one grid cell. The grid size is increased/decreased when the window is maximized or minimized.

My requirement is that the triangle size should also increase/decrease to fit the grid cell each time the grid size is increased/decreased.

My code is as follows:

import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Grid extends JPanel {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Grid g = new Grid();
        JFrame f = new JFrame("Application GUI Window");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        f.getContentPane().add("Center", g);
        f.pack();
        f.setSize(new Dimension(450,400));
        f.show();
    }

    public void paint(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;
        Dimension d = getSize();
        g2d.setBackground(getBackground());
        g2d.clearRect(0, 0, d.width, d.height);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        drawGrid(d.width, d.height, g2d);
        int[] xPoints = {20,15,25}; 
        int[] yPoints = {15,25,25};
        int n = 3;

        Polygon triangle = new Polygon(xPoints, yPoints, n);
        g.fillPolygon(triangle);
    }

    private void drawGrid(int width, int height, Graphics2D g2d) {

       /* BasicStroke border = new BasicStroke(3, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 0, new float[]{0,1,0,1}, 0);
        g2d.setStroke(border);
        g2d.drawRect(3,3,width-6,height-6);*/

        //horizontal lines
        int cellheight = height/10;
        int cellwidth = width/5;
        for (int j=0;j<height;j=j+cellheight)
        {
            BasicStroke line = new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 0, new float[]{0,1,0,1}, 0);
               g2d.setStroke(line);
               g2d.drawLine(0, j, cellwidth*5, j);

        }

        //vertical lines

        for (int i=0;i<width;i=i+cellwidth)
        {
            BasicStroke line = new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 0, new float[]{0,1,0,1}, 0);
               g2d.setStroke(line);
               g2d.drawLine(i, 0, i, cellheight*10);

        }

    }
}

Thanks in advance for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:46:32+00:00Added an answer on June 4, 2026 at 2:46 pm

    You can tie the coordinates of the triangle to the dimensions of a grid cell. Here is a snippet based on your example:

    int cellHeight = d.height/10;
    int cellWidth = d.width/5;
    
    int xOffset = cellWidth/6;
    int yOffset = cellHeight/6;
    
    int[] xPoints = {cellWidth/2, xOffset, cellWidth - xOffset}; 
    int[] yPoints = {yOffset, cellHeight - yOffset, cellHeight - yOffset};
    
    Polygon triangle = new Polygon(xPoints, yPoints, xPoints.length);
    

    Note that in Swing you usually should override paintComponent() rather than paint(), unless in some special cases. See A Closer Look at the Paint Mechanism for more details.

    Also note that show() is deprecated in favor of setVisible(). You can replace it with: setVisible(true)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written code that opens 16 figures at once. Currently, they all open
I have written some code to ensure that items on an order are all
I have written code to perform a function that could take a while to
I have written a PHP function to take a video embed code that has
I have written a cascading drop down list using JQuery. The code that I
I have written a code for adding multiple keys for a dictionary for that
I'have written a simple code, that gets accelerometer&orientation meter and display some graphics based
I have written some WebGL code, actually I am playing with the examples that
I have a c++ source code that was written in linux/unix environment by some
I have written a very simple WCF service, that worked fine (code below), then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.