Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Beginner Java graphics problem

  • 10-01-2015 06:12PM
    #1
    Registered Users, Registered Users 2 Posts: 363 ✭✭


    Hi, I am having some difficulty with Java. All I want to do is draw a square centered on a JFrame. I have to use a inner class by extending JPanel.

    All this code displays is the JFrame with no shape?

    package course1;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class GrowAndShrinkSquareGUI{
    JFrame frame;

    class SquareDrawPanel extends JPanel{

    public void paintComponent(Graphics g){
    g.setColor(Color.green);
    g.fillRect(25,25,100,100);
    }
    } /* inner class ends */

    public static void main (String[] args){
    SimpleGUI test = new SimpleGUI();
    test.go();
    }

    public void go(){
    frame = new JFrame();
    frame.setSize(500,500);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    }


Comments

  • Registered Users, Registered Users 2 Posts: 363 ✭✭silver campaign


    I have tried this also, but it only displays a JFrame aswell.

    package course1;
    import java.awt.Color;
    import java.awt.Graphics;

    import javax.swing.JFrame;
    import javax.swing.JPanel;


    public class test{
    JFrame frame;

    public class SquareDrawPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private int diameter = 100;

    public void paintComponent(Graphics g) {

    int width = this.getWidth();
    int height = this.getHeight();

    g.setColor(Color.WHITE);
    g.fillRect(0, 0, width, height);

    int x = (width - diameter) / 2;
    int y = (height - diameter) / 2;

    g.setColor(Color.GREEN); // J
    g.fillRect(x, y, diameter, diameter);
    } /* inner class ends */

    }

    public static void main (String[] args){
    SimpleGUI test = new SimpleGUI();
    test.go();
    }

    public void go(){
    frame = new JFrame();
    frame.setSize(500,500);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    }


  • Registered Users, Registered Users 2 Posts: 306 ✭✭yes there


    Think about the structure of swing and component placement. You are only missing one line. Also need to rethink the center coordinates.


  • Registered Users, Registered Users 2 Posts: 363 ✭✭silver campaign


    Thanks, I;ll have a look in half an hour. think I need another cup of tea first!!


  • Registered Users, Registered Users 2 Posts: 363 ✭✭silver campaign


    No Sorry folks. I'm still not getting it. Any more ideas?


  • Registered Users, Registered Users 2 Posts: 1,469 ✭✭✭Anesthetize


    No Sorry folks. I'm still not getting it. Any more ideas?
    You've already declared a class for the square shape as an inner class. You now need to create an object of this class and add it to the JFrame.


  • Advertisement
Advertisement