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

  • Home
  • SEARCH
  • 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 7902699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:34:16+00:00 2026-06-03T09:34:16+00:00

This is my first time posting on stackoverflow so bear with me. I decided

  • 0

This is my first time posting on stackoverflow so bear with me.

I decided to write a program in java that reads a file that contains text such as “blue”, “green”, “red”, and then draws squares on my JFrame that are of the color they indicate, and based on where they are in the text file. I’m not sure if that makes sense to someone, but it was something that just popped up into my head and I was like “Hey, i think i’ll try this.”

Basically I’d like to have my JFrame have the first row have 3 squares (red, blue, green). Then my next row have 3 squares (blue, green, red). Then last (green, red, blue).

first my text file is like this:

red blue green

blue green red

green red blue

and now i’ll post the code. I’m not 100% sure what the error is, I’ve been running it in eclipse and it hasn’t really told me anything useful that I know what to do with.

import java.util.*;
import java.awt.*;
import java.io.File;
import javax.swing.*;

public class Test extends JFrame { 
    int currentY = 0;
    int currentX = 0;
    static Scanner squares;
    private final static Graphics graphics = null;

    Test(Graphics graphics) {
    this.setVisible(true);
    this.setSize(400, 400);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    while (squares.hasNextLine()) {
        Scanner row = new Scanner(squares.nextLine());
        while (row.hasNext()) {
            System.out.println(row.next());
            if (row.next().equals("green")) {
                graphics.setColor(Color.GREEN);
            }
            else if (row.next().equals("red")) {
                graphics.setColor(Color.RED);
            }
            else {
                graphics.setColor(Color.BLUE);
            }
            graphics.fillRect(currentX,  currentY, 20, 20);
            currentX += 20;
        }
        currentY += 20;
    }

}
public static void main(String[] args) throws Exception {
    squares = new Scanner(new File ("C:/Test/data.txt"));
    Test test = new Test(graphics);
}
}
  • 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-03T09:34:17+00:00Added an answer on June 3, 2026 at 9:34 am

    I believe your main problem is that graphics is null. The next worse problem is that each time you call next() on the scanner, the previous string gets eaten. Instead use something like String color = row.next(), and use “color” in the rest of the loop.

    You can get some ideas here: http://content.gpwiki.org/index.php/Java:Tutorials:Graphics

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

Sidebar

Related Questions

That's my first time posting on stackoverflow. I've been finding usefull answers on this
This is my first time posting so bear with me. I have a file
this is my first time posting on StackOverflow but this site is awesome. Thanks
use this website a lot but first time posting. My program creates a number
This is my first time posting to stackoverflow, but these threads have helped me
This is the first time I am posting a question on stackoverflow, so please
This is my first time posting on Stackoverflow, but I've been reading through many
this is my first time posting here, I have a question which I have
this is my first time posting on here and have read alot of helpful
First time posting here, will try to be succinct. This is a classic 'can't

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.