Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

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);
    }

    }


Welcome!

It looks like you're new here. Sign in or register to get started.

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,467 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

Welcome!

It looks like you're new here. Sign in or register to get started.
Advertisement