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 6879991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:55:03+00:00 2026-05-27T04:55:03+00:00

Below I have a simple method for painting a set of objects onto an

  • 0

Below I have a simple method for painting a set of objects onto an java.awt.applet.Applet. I thought this was very straightforward, but it ends up only painting the objects as a single pixel at the top-left corner of the applet. The idea is that the Display class takes in a set of fairly lightweight objects that extend GameObject and contain information about their location, size, and appearance on the screen, and then draws them pixel-by-pixel onto the applet, stretching and positioning them proportionally depending on the specified display height and width. In testing this, I set the width and height of the Display to 128, and pass two objects to the Display, which are both 32-pixel squares (both return 32 for getWidth() and getHeight()), one is red and returns 24 for getX() and getY(), and the other is blue and returns 16 for getX() and getY(). I put the Display in a javax.swing.JFrame and use a java.awt.BorderLayout to ensure it fills the frame (I use add(display, java.awt.BorderLayout.CENTER); within the aforementioned javax.swing.JFrame).

As far as I can tell, this should be paining a blue 32-pixel square that’s 16 pixels from the top and left edges and a red 32-pixel square that is either obscured by or obscuring part of the other one. However, all I get is a single red or blue pixel in the top-left corner of the Display. This is consistent no matter how big or small the window is.

Code


public class Display extends java.awt.applet.Applet
{
  private int w,h;
  private ArrayPP<GameObject> gameObjects;//ArrayPP is my way of making a dynamically expanding array. It is similar to Vector, but has many more useful methods I use elsewhere.

  public Display(int width, int height)
  {
    w = width;
    h = height;
  }

  public void addGameObject(GameObject go)
  {
    gameObjects.add(go);
  }

  public void refresh(java.awt.Graphics g)
  {
    int x, y, w, h;
    final int W = getWidth(), H = getHeight();
    for (GameObject go : gameObjects)//Draw all objects
      for (x = go.getX(), y = go.getY(), w = go.getWidth() + x, h = go.getHeight() + y; y < h; y++)//Draw all lines of the object
        for (x = go.getX(); x < w; x++)//Draw all the pixels on this line of the object
        {
          g.setColor(go.getColorForPixel(x, y));
          g.fillRect((x / this.w) * W, (y / this.h) * H, w/W, h/H);
        }
  }
}

 

public interface GameObject
{
  public int getX();
  public int getY();
  public int getWidth();
  public int hetHeight();
  public java.awt.Color getColorForPixel(int x, int y);
}

Question


Why is java.awt.Graphics.fillRect(int x, int y, int width, int height) only painting the topleft corner of the applet?

Solution


The solution lies in the line that reads

          g.fillRect((x / this.w) * W, (y / this.h) * H, w/W, h/H);

wherein integer calculations cause all values to be 0. The solution is as follows:

          g.fillRect((int)(((float)x/(float)this.w) *W),
                     (int)(((float)y/(float)this.h) *H),
                     (int)((float)W/(float)w),
                     (int)((float)H/(float)h));
  • 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-05-27T04:55:03+00:00Added an answer on May 27, 2026 at 4:55 am

    The problem lies with

    (x / this.w) * W
    

    If x and this.w are both integers and x

    Convert one or more of x and w to float to force a floting point division.

    (int)(((float)x/(float)this.w) *W)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below I have a very simple example of what I'm trying to do. I
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
I have a very simple Winsock2 TCP client - full listing below - which
The below code is very simple. I have a jQuery autocomplete bound to an
I have written a very simple WCF service, that worked fine (code below), then
This is my first question on S.O. I have a very odd problem. Below
I have some very simple code to generate an assembly and invoke a method
I have a simple requirement as mentioned below: A ListView or any control displays
I have a simple bit of jQuery which displays the row below the current
below i have a code that runs in most of my simple programs ..

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.