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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:36:19+00:00 2026-05-24T07:36:19+00:00

I have a Java class that I’m debating what I should make it into

  • 0

I have a Java class that I’m debating what I should make it into for my Android app. I’m unsure whether I should make it an Activity, a View, or both.

The reason I am unsure has to do with the it having constructors that have parameters, a component listener, and draws on the canvas.

To be clear, when attempting to make it a View, I had issues with the constructor. If an activity, I’m unsure how to interact between the two (ie component resize, color changed, etc) – perhaps with Intents? So logically, maybe the best way is a mixture of a View and Activity. I’m rather new to this thinking, so please keep that in mind.

I’m wondering how those who have created a few Android Apps go about determining what your previous Java classes should be. My class seems that it could go either way.

Below is part of my class (in Java, but I also have it converted to Android):

public class GraphDisplay extends JPanel implement Serializable {

    public static final int BLUE_RED            = 0;
    public static final int BLACK_WHITE         = 1;

    protected int displayBoard;
    protected int centerXcoord;
    protected int centerYcoord;

    private GeneralPath circlePath;
    private GeneralPath rectPath;

    private Color triangleColor;
    private Color rectColor; 

    /*
     * Create an instance of GraphDisplay using the default
     *    blue/red display
     */
    public GraphDisplay() {
        this(BLUE_RED, 0);

    }

    /*
     * Create an instance of GraphDisplay using the board value
     *    and index of 0. Throws exception if model isn't a 
     *    prederfined value
     */
    public GraphDisplay(int board) {
        this(model, 0);

    }



    public GraphDisplay(int board, int index) throws IllegalArgumentException {
        if(board == BLUE_RED || board == BLACK_WHITE) {
            displayBoard = board;
            this.index = index;

            if(displayBoard == BLUE_RED) {
                setBackground(Color.blue);
                triangleColor = Color.red;
                rectColor = Color.red;
            } else {
                setBackground(Color.black);
                triangleColor = Color.white;
                rectColor = Color.white;
            }

            compListener = new GraphListener();
            addGraphListener(compListener);

            mListener = new GraphMouseListener();
            addMouseMotionListener(mListener);

            trianglePath = new GeneralPath();
            rectPath   = new GeneralPath();

            setMinimumSize(new Dimension(400, 400));
            setPreferredSize(new Dimension(400,400));   

        } else {
             throw new IllegalArgumentException("Improper model")
        }
    }


    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;

        centerXcoord = getWidth() / 2;
        centerYcoord = getHeight() / 2;

        drawTriangle(g2);
        drawCircle(g2);

    }

    private void drawTriangle(Graphics2D g2) {
        //code for drawing triangle with lines
    }

    private void drawRectangle(Graphics2D g2) {
        //code for drawing circle 
    }

    public void setTriangleColor(Color color) {
        triangleColor = color;
        repaint();
    }

    public Color getTriangleColor() {
        return triangleColor;
    }

    //Omitted other getters/setter for brevity. 
    //The above get/set methods are a variety of what the class entails

    protected class GraphMouseMotionListener extends MouseMotionAdapter {
        public void mouseMoved(MouseEvent me) {
            int x = mouseEvent.getX();
            int y = mouseEvent.getY();

            //if in component display message
        }
    }

    protected class GraphComponentListener extends ComponentsAdapter {
        public void componentResized(ComponentEvent ce) {
            super.componentResized(ce);
        }
    }


}

So my question is, would you make it an Activity that draws on a View, a View, or something else? If it’s not too much trouble, can you elaborate on why you’d do it the way you mention? (if splitting it between an Activity and View, can you provide a brief example, it doesn’t have to involve my class – just looking to understand the concept)

  • 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-24T07:36:20+00:00Added an answer on May 24, 2026 at 7:36 am

    You would use an Activity that can use a view you create in your xml file by extending Activity for your class and then in the onCreate method you say setContentView(R.layout.your_view);

    for example:

    Your Activity Class:

    public class YourClass extends Activity {
    
        private Button exampleButton;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            exampleButton = (Button)findViewById (R.id.myButton);
    
            exampleButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {   
                    Toast.makeText(YourClass.this, "I did something", Toast.LENGTH_SHORT).show();   
                }
            });
        }
    }
    

    Your XML View:

      <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical" android:layout_width="fill_parent"
            android:layout_height="fill_parent">
    
            <TextView android:layout_width="fill_parent"
                      android:layout_height="wrap_content" 
                      android:text="Hello World" />
    
            <Button android:id="@+id/myButton"
                    android:layout_width="wrap_content" 
                    android:layout_height="wrap_content"/>
    
        </LinearLayout>
    

    So the XML is where you define your view, what components will be in it, how it is laid out and so on.

    Then the Activity Class is where you bring your view to life and add the functionality and so on. You can also programatically add components to your view but I won’t go into that right now.

    You could then look into Overlays (some examples would be map overlays to see how they draw on a canvas in the Activity) to get an idea and then apply it to your situation. For example: http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/ItemizedOverlay.html

    hope that helps.

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

Sidebar

Related Questions

So I have a java class that takes individual commands and puts them into
I'm currently working on Mangler's Android implementation. I have a java class that looks
We have a Java app that runs on Websphere. I have one class that
I have a java class that applies an xslt to all xml files in
I need your expertise once again. I have a java class that searches a
I have a really simple Java class that effectively decorates a Map with input
Suppose that I have a Java class with a static method, like so: class
In Java, say you have a class that wraps an ArrayList (or any collection)
I have a Java program that loads thirdparty class files (classes I did not
I have a java class: it.eng.ancona.view.RuoliView$TabElaborazioneFattureValidazione$ElencoDettaglioElaborazioneFattureValidazione$RigaElencoDettaglioElaborazioneFattureValidazione It's so long for multiple inner class. If

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.