简体   繁体   中英

Java Graphics trying to draw rectangle

My goal is to make a class that contains rectangle and then use it and change it in other classes. I tried to write this code and make an object Rect rect = new Rect(); but when i start the program the rectangle doesn't show up. I also tried to add it with window.add(rect); but had same problem i'm sure im doing something wrong but i don't really know what.

One more thing that i tried was calling method from other class Rect.drawRect(g); but then it asks for "Argument" and if i add Argument g like i had in method drawRect it says "g cannot be resolved to a variable"

I hope someone can explain and tell me what did i do wrong, also would be great to know how to make rectangle or a circle and later use it in other classes and maybe change its color and size, I only found how to do it in one class.

import javax.swing.JFrame;

public class Main extends Rect{

    public static void main(String[] args ) {

        JFrame window = new JFrame("test");
        window.setSize(1000, 800);
        window.setVisible(true);
        window.setResizable(false);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Rect rect = new Rect();
    }   
}

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Rect extends JPanel{

    public void drawRect(Graphics g){
        g.setColor(Color.RED);
        g.fillRect(100, 100, 200, 200); 
    }
}

UDP: You need override void paintComponent(Graphics g) instead void drawRect(Graphics g) and call super.paintComponent(g) inside method. Then you can use window.add(rect); . Thanks @FredK for correction

The most important thing is that you need to write some code to do the painting. This is done by overriding the paintComponent method inside your Rect class a bit like this:

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.RED);
        g.fillRect(100, 100, 200, 200); 
    }

Your second issue is that you want to be able to change the colour and size of your rectangle from other classes. For a simple example, this can easily be done by adding some static values inside your Rect class:

    public static Color myColor = Color.RED;
    public static int myX = 100;
    public static int myY = 100;
    public static int myWidth = 200;
    public static int myHeight = 200;

Now update your paint method to use the static values:

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(myColor);
        g.fillRect(myX, myY, myWidth, myHeight); 
    }

Now, whenever or wherever you use the Rect panel it will now show the rectangle according to the static values.

For example, below is a simple and working program, note how it uses the following:

    //create Rect
    Rect rect = new Rect();
    //set the size of the new panel
    rect.setPreferredSize(new Dimension(800, 600));
    //add the rect to your JFrame
    window.add(rect);
    //now you can change the color for all Rect instances
    //Note how I use Rect instead of rect, however, both will work.
    Rect.myColor = Color.BLUE;
    //And dont forget to repaint it if you want to see the changes immediatly
    rect.repaint();

Full example, main class:

import javax.swing.JFrame;
import java.awt.Color;

public class Main{

    //Note how we don't need to extend the Rect class (It just adds confusion)
    public static void main(String[] args ) {

        JFrame window = new JFrame("test");
        window.setSize(1000, 800);
        window.setVisible(true);
        window.setResizable(false);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //create Rect
        Rect rect = new Rect();
        //set the size of the new panel
        rect.setPreferredSize(new Dimension(800, 600));
        //add the rect to your JFrame
        window.add(rect);


        //now you can change the color for all Rect instances
        //Note how I use Rect instead of rect, however both will work.
        Rect.myColor = Color.BLUE;
        //And dont forget to update it
        rect.repaint();
    }
}

And the Rect class:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Rect extends JPanel{
    public static Color myColor = Color.RED;
    public static int myX = 10;
    public static int myY = 10;
    public static int myWidth = 200;
    public static int myHeight = 200;

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(myColor);
        g.fillRect(myX, myY, myWidth, myHeight); 
    }
}

Note, if you don't want to call Rect.repaint() every time you make a color/size change then just make a new method that changes each value and include repaint() , for example:

public void changeWidth(int width){
    myWidth  = width;
    repaint();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM