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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:09:52+00:00 2026-05-12T21:09:52+00:00

So here’s the class and the super class, question to follow: TestDraw: package project3;

  • 0

So here’s the class and the super class, question to follow:

TestDraw:

package project3;

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class TestDraw extends MyShape
{
    public static void main(String[] args) 
    {
        DrawPanel panel = new DrawPanel();
        JFrame application = new JFrame();



        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.add(panel);
        application.setSize(300,300);
        application.setVisible(true);   
        JLabel southLabel = new JLabel(toString());
        application.add(southLabel, BorderLayout.SOUTH);
    }
}

MyShape:

package project3;

import java.awt.Color;

public class MyShape 
{
    private int x1, y1, x2, y2;
    private Color myColor;
    public MyShape()
    {
        setX1(1);
        setY1(1);
        setX2(1);
        setY2(1);
        setMyColor(Color.BLACK);
    }
    public MyShape(int x1, int y1, int x2, int y2, Color myColor)
    {
        setX1(x1);
        setY1(y1);
        setX2(x2);
        setY2(y2);
        setMyColor(myColor);
    }
    public void setX1(int x1)
    {
        if(x1 >= 0 && x1 <= 300)
        {
            this.x1 = x1;
        }
        else
        {
            this.x1 = 0;
        }
    }
    public int getX1()
    {
        return x1;
    }
    public void setY1(int y1)
    {
        if(y1 >= 0 && y1 <= 300)
        {
            this.y1 = y1;
        }
        else
        {
            this.y1 = 0;
        }
    }
    public int getY1()
    {
        return y1;
    }
    public void setX2(int x2)
    {
        if(x2 >= 0 && x2 <= 300)
        {
            this.x2 = x2;
        }
        else
        {
            this.x2 = 0;
        }
    }
    public int getX2()
    {
        return x2;
    }
    public void setY2(int y2)
    {
        if(y2 >= 0 && y2 <= 300)
        {
            this.y2 = y2;
        }
        else
        {
            this.y2 = 0;
        }
    }
    public int getY2()
    {
        return y2;
    }
    public void setMyColor(Color myColor)
    {
        this.myColor = myColor;
    }
    public Color getMyColor()
    {
        return myColor;
    }
    public String toString()
    {
        return String.format("X1: %d, X2: %d, Y1: %d, Y2: %d, Color: %s", getX1(), getX2(),
                getY1(), getY2(), getMyColor());
    }
}

In the class TestDraw, I was trying to put the toString from MyShape into a text box on a window, but when I did the “JLabel southLabel = new JLabel(toString());” it told my toString() needs to be static. That’s all fine and dandy except when you make toString static it wants to make the gets in that string static, which is bad…any ideas?

I’ve tried putting the toString() in the superclass but it gives the same issue, tried asking the teacher but he says “Look in the book” Well…been two hours of reading the chapter and I’ve yet to find an example after my third read through.

Thank you in advance!

PS: Answer is nice, but explanation is preferred!

  • 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-12T21:09:52+00:00Added an answer on May 12, 2026 at 9:09 pm

    Make an instance of your class.
    TestDraw testDraw = new TestDraw();
    and call the toString() method on it.
    While in the main method, you are in a static context – that is, you don’t have an object of type TestDraw, which also means you don’t have any of its fields or methods.

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

Sidebar

Related Questions

here is a debatable question which i want to achieve not sure how .
Here is my persistence.xml : <?xml version=1.0 encoding=UTF-8?> <persistence xmlns=http://java.sun.com/xml/ns/persistence xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd version=1.0>
Here's my Manifest: <?xml version=1.0 encoding=utf-8?> <manifest xmlns:android=http://schemas.android.com/apk/res/android package=com.mappp.mobile android:versionCode=1 android:versionName=1.0 > <supports-screens android:largeScreens=true
Here is the stacktrace: 04-10 18:09:36.393: E/AndroidRuntime(26592): FATAL EXCEPTION: main 04-10 18:09:36.393: E/AndroidRuntime(26592): java.lang.RuntimeException:
Here is my simplified data structure: Object1.h template <class T> class Object1 { private:
Here a simple question : What do you think of code which use try
Here is my question. I am having this simple menu. <div id=menu> <ul> <li>
Here is my code: echo '<table class=class1><tbody>'; while ($row1=mysql_fetch_array($result1)){ echo '<tr><td>'.$row1['firstname'].'</td><td>'.$row1['lastname'].'</td></tr>'; } echo '</tbody></table>';
Here is my problem...I have a page that loads a list of clients and
Here is some code I made :) @echo off set source=R:\Contracts\ set destination=R:\Contracts\Sites\ ROBOCOPY

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.