Winchester
Winchester

Reputation: 9

Drawing to a GUI From the main using another class In Java

I'm a beginner programmer, so I don't know all the vocab, but I understand some of the basics of java.

So I'm trying to Draw in a GUI from the main using another class. I know I'm not being very specific but here's my code, and I'll try to explain what I'm trying to do.

This is my main

    import javax.swing.*;
    import java.awt.*;

public class ThisMain {


public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame theGUI = new JFrame();
    theGUI.setTitle("GUI Program");
    theGUI.setSize(600, 400);
    theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    ColorPanel panel = new ColorPanel(Color.white);
    Container pane = theGUI.getContentPane();
    pane.add(panel);
    theGUI.setVisible(true);



    }
}

This is my other class

import javax.swing.*;
import java.awt.*;
public class ColorPanel extends JPanel {
    public ColorPanel(Color backColor){
    setBackground(backColor);

}

public void paintComponent(Graphics g){
    super.paintComponent(g);

}
}

I'm trying to use the line

 ColorPanel panel = new ColorPanel(Color.white);

Or something like it to use things like

 drawRect();

In the main and have it draw in the GUI.

This is the code I used that i think came closest to working

import javax.swing.*;
import java.awt.*;

public class ThisMain {


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame theGUI = new JFrame();
        theGUI.setTitle("GUI Program");
        theGUI.setSize(600, 400);
        theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        //I'm trying to draw a string in the JFrame using ColorPanel but i'm            Trying to do it from the main
        ColorPanel panel = new ColorPanel(){
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                //This is the line I need to work using the ColorPanel in anyway
                g.drawString("Hello world!", 20, 20);
        };
        Container pane = theGUI.getContentPane();
        //The errors occur here
        pane.add(panel);
        theGUI.setVisible(true);


    //and on these brackets
    }
}

Upvotes: 0

Views: 829

Answers (2)

Evelyn Zayas
Evelyn Zayas

Reputation: 1

The reason why you are not able to compile is caused by this line:

ColorPanel panel = new ColorPanel(Color.white);

because the class ColorPanel is not included in the swing library so you have to code it. This code extends JPanel and includes a constructor that takes a Color parameter. The constructor runs when the panel is instantiated and sets its background color:

import javax.swing.*;
import java.awt.*;

public class ColorPanel extends JPanel {
   public ColorPanel(Color backColor) {
    setBackground(backColor);
  }
}

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

I'm not really sure what exactly you're trying to do (you haven't replied to my comment to your question for clarification), but I would:

  • Give your ColorPanel a List<Shape>
  • Give it a public void addShape(Shape s) method that allows outside classes to insert shapes into this List, and then calls repaint()
  • And in the ColorPanel's paintComponent method, iterate through the List, painting each Shape item using your Graphics2D object.

Note that Shape is an interface, and so your List can hold Ellipse2D objects, Rectangle2D objects, Line2D objects, Path2D objects, and a whole host of other objects that implement the interface. Also, if you want to associate each Shape with a Color, you could store the items in a Map<Shape, Color>.

Upvotes: 2

Related Questions