• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Java Swing Help

Joined
Jun 17, 2007
Messages
7,335 (1.19/day)
Location
C:\Program Files (x86)\Aphexdreamer\
System Name Unknown
Processor AMD Bulldozer FX8320 @ 4.4Ghz
Motherboard Asus Crosshair V
Cooling XSPC Raystorm 750 EX240 for CPU
Memory 8 GB CORSAIR Vengeance Red DDR3 RAM 1922mhz (10-11-9-27)
Video Card(s) XFX R9 290
Storage Samsung SSD 254GB and Western Digital Caviar Black 1TB 64MB Cache SATA 6.0Gb/s
Display(s) AOC 23" @ 1920x1080 + Asus 27" 1440p
Case HAF X
Audio Device(s) X Fi Titanium 5.1 Surround Sound
Power Supply 750 Watt PP&C Silencer Black
Software Windows 8.1 Pro 64-bit
How can I draw a circle from circle class that extends an abstract shape class?

I managed to use paintCompontent in my MainWidnow class to get a circle to draw in my Window but now I'm trying to do the same thing via a class circle that extends an abstract class shape.

I declared an abstract method draw and I have moved the actual drawing code from the paintComponent to my Circle class and adjusted the object but it won't draw on the screen, probably because I'm not using paintComponent anywhere, but I juts don't know where to put it given that I have to use draw in my circle/shape class.
MainWindow my GUI window
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class MainWindow //implements ActionListener
{
    private static final int WIDTH = 800;
    private static final int HEIGHT = 600;

    JFrame frame;
    protected JTextField textField;
    protected JTextArea textArea;
    private final static String newline = "\n";
    //JPanel jp = new JPanel();

    Color currentColor = Color.red;

    public MainWindow( ){

        frame = new JFrame("Menu Demonstration");
        frame.setSize(WIDTH, HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setLayout(new FlowLayout(-2,10,10));
       // frame.setLayout(new GridLayout(1, 3));
        //setLayout(new BoxLayout( , BoxLayout.Y_AXIS));
        //setBackground(Color.LIGHT_GRAY);;

        String[] colorBoarder = { "Red", "Green", "Blue", "Yellow", "Black", "White" };

//Adds menu and thier selections
//Select Fill
        JMenu shapeColor = new JMenu("Shape Color");
        

        JMenuItem redChoice = new JMenuItem("Fill Red");
        //redChoice.addActionListener(this);
        shapeColor.add(redChoice);

        JMenuItem whiteChoice = new JMenuItem("Fill White");
        //whiteChoice.addActionListener(this);
        shapeColor.add(whiteChoice);
        whiteChoice.setOpaque(true);

        JMenuItem blueChoice = new JMenuItem("Fill Blue");
        //blueChoice.addActionListener(this);
        shapeColor.add(blueChoice);
//Draw Shapes
        JMenu shapeType = new JMenu("Shape Type");

        JMenuItem circle = new JMenuItem("Draw Circle");
                circle.addActionListener(new ActionListener() {
            Integer index;
            int x,y,r;

            @Override
            public void actionPerformed(ActionEvent e) {
                JPanel pn = new JPanel();
                JTextField xInput = new JTextField(5);
                JTextField yInput = new JTextField(5);
                JTextField rInput = new JTextField(5);
                pn.add(xInput);
                pn.add(yInput);
                pn.add(rInput);
                int result = JOptionPane.showConfirmDialog(null, pn, "Enter the values", JOptionPane.OK_CANCEL_OPTION);
                if (result == JOptionPane.OK_OPTION) {
                   //don't forget to throw/catch exception here
                    x = Integer.parseInt(xInput.getText());
                    index = Integer.parseInt(xInput.getText());
                    y = Integer.parseInt(yInput.getText());
                    System.out.println(yInput.getText());
                    r = Integer.parseInt(rInput.getText());
                }
                toDraw(index.toString(),x,y,r);
            }
        });
        shapeType.add(circle);

        JMenuItem rectangle  = new JMenuItem("Draw Rectangle");
        //rectangle.addActionListener(this);
        shapeType.add(rectangle);

        JMenuItem rightTriangle  = new JMenuItem("Draw Right Triangle");
        //rightTriangle.addActionListener(this);
        shapeType.add(rightTriangle);

        JMenuBar bar = new JMenuBar( );
        bar.add(shapeColor);
        bar.add(shapeType);
        frame.setJMenuBar(bar);

    }
    ShapeArray shapes = new ShapeArray(4);
    void toDraw(String p, int x, int y,int r) {
       //ShapeArray d = new ShapeArray(4);
        Circle d = new Circle(p,x,y,r);
        frame.add(d);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.revalidate();
        frame.repaint();

    }

   /*public void actionPerformed(ActionEvent e)
    {
        String buttonString = e.getActionCommand( );

        //if (buttonString.equals("Fill Red"))
             //redPanel.setBackground(Color.RED);
        //else if (buttonString.equals("Fill Blue"))
            //bluePanel.setBackground(Color.BLUE);
        //if(buttonString.equals("Draw Circle"))
            //addShape(new Circle());// to add the shape when chosen
        //else
            //System.out.println("Unexpected error.");


        //String text = textField.getText();
        //textArea.append(text + newline);
        //textField.selectAll();

        //Make sure the new text is visible, even if there
        //was a selection in the text area.
        //textArea.setCaretPosition(textArea.getDocument().getLength());


    }*/
    
   /*class Draw extends JPanel {

    String inp;
    int xValue;
    int yValue;
    int radius;

    public Draw(String gn, int x, int y,int r) {
        inp = gn;
        xValue = x;
        yValue = y;
        radius = r;
    }

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

        //Graphics2D g2 = (Graphics2D) g;

        drawCircle(g, xValue, yValue, radius);
        //g2.drawString(inp, x + (x / 2), x + (x / 2));
    }
    public void drawCircle(Graphics cg, int xCenter, int yCenter, int r) {
 
        cg.drawOval(xCenter-r, yCenter-r, 2*r, 2*r);
 
    }//end drawCircle

   }*/
}
Circle Class
Code:
import java.awt.*;
public class Circle extends Shape{

    public int radius;

    public Circle(String gn, int x, int y,int r) {
        //inp = gn;
        super.xValue = x;
        super.yValue = y;
        this.radius = r;
    }
//@Override
    public void draw(Graphics g) {
        //super.paintComponent(g);

        drawCircle(g, xValue, yValue, radius);
        //g2.drawString(inp, x + (x / 2), x + (x / 2));
    }
    public void drawCircle(Graphics cg, int xCenter, int yCenter, int r) {

        cg.drawOval(xCenter-r, yCenter-r, 2*r, 2*r);

    }//end drawCircle

}
Shape Class
Code:
import java.awt.*;
import javax.swing.*;
public abstract class Shape extends JPanel{

    public int xValue, yValue,area,perimeter;
    public Color color = Color.white;
    
   public Shape(){
          xValue=0;
          yValue=0;
          area=0;
          perimeter=0;

     }


      void setColor(Color color) {
             // Set the color of this shape
         this.color = color;
      }


      abstract void draw(Graphics g);
            // Draw the shape in the graphics context g.
            // This must be overriden in any concrete subclass

}
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,444 (2.43/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
wish i could help you as it has been many years since i have written anything for swing. when i got stuck i would actually create what i wanted using netbeans and then learn from the code. i wouldn't steal the code, i would see how netbeans made it happen and that usually was enough information to help me solve the problem on my own. good luck.
 
Joined
Jun 17, 2007
Messages
7,335 (1.19/day)
Location
C:\Program Files (x86)\Aphexdreamer\
System Name Unknown
Processor AMD Bulldozer FX8320 @ 4.4Ghz
Motherboard Asus Crosshair V
Cooling XSPC Raystorm 750 EX240 for CPU
Memory 8 GB CORSAIR Vengeance Red DDR3 RAM 1922mhz (10-11-9-27)
Video Card(s) XFX R9 290
Storage Samsung SSD 254GB and Western Digital Caviar Black 1TB 64MB Cache SATA 6.0Gb/s
Display(s) AOC 23" @ 1920x1080 + Asus 27" 1440p
Case HAF X
Audio Device(s) X Fi Titanium 5.1 Surround Sound
Power Supply 750 Watt PP&C Silencer Black
Software Windows 8.1 Pro 64-bit
I totally forgot about this thread.

I've gotten really far in my code. I've managed to draw circles with abstract shape class and more :D

I basically needed a paintComponent on a JPanel and then I needed to add it to my frame.

Thanks for the tip. That would be a clever way to go about.
 

Tman

New Member
Joined
Oct 14, 2013
Messages
1 (0.00/day)
Im stuck

Hey I have the same problem in one of my assignments. Do you mind elaborating how you managed to solve it? I have values for everything but for some reason it just wont print! Please help!
 
Joined
Jun 17, 2007
Messages
7,335 (1.19/day)
Location
C:\Program Files (x86)\Aphexdreamer\
System Name Unknown
Processor AMD Bulldozer FX8320 @ 4.4Ghz
Motherboard Asus Crosshair V
Cooling XSPC Raystorm 750 EX240 for CPU
Memory 8 GB CORSAIR Vengeance Red DDR3 RAM 1922mhz (10-11-9-27)
Video Card(s) XFX R9 290
Storage Samsung SSD 254GB and Western Digital Caviar Black 1TB 64MB Cache SATA 6.0Gb/s
Display(s) AOC 23" @ 1920x1080 + Asus 27" 1440p
Case HAF X
Audio Device(s) X Fi Titanium 5.1 Surround Sound
Power Supply 750 Watt PP&C Silencer Black
Software Windows 8.1 Pro 64-bit
Hey I have the same problem in one of my assignments. Do you mind elaborating how you managed to solve it? I have values for everything but for some reason it just wont print! Please help!

Post your code and I'll try and help you out.
 

Aquinus

Resident Wat-man
Joined
Jan 28, 2012
Messages
13,147 (2.94/day)
Location
Concord, NH, USA
System Name Apollo
Processor Intel Core i9 9880H
Motherboard Some proprietary Apple thing.
Memory 64GB DDR4-2667
Video Card(s) AMD Radeon Pro 5600M, 8GB HBM2
Storage 1TB Apple NVMe, 4TB External
Display(s) Laptop @ 3072x1920 + 2x LG 5k Ultrafine TB3 displays
Case MacBook Pro (16", 2019)
Audio Device(s) AirPods Pro, Sennheiser HD 380s w/ FIIO Alpen 2, or Logitech 2.1 Speakers
Power Supply 96w Power Adapter
Mouse Logitech MX Master 3
Keyboard Logitech G915, GL Clicky
Software MacOS 12.1
Top