简体   繁体   中英

Drawing on a JPanel inside a JFrame using fillRect - rectangle sizing and position issues?

EDIT: Fixed up code:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;


public class JumpingBall extends JPanel{
@Override
public Dimension getPreferredSize()
{
    return new Dimension(300,300);
}


public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D rectangle = (Graphics2D) g;
    rectangle.setColor(Color.BLACK);
    rectangle.fillRect(0,270,300,30);
}


public static void main(String[] args) {
    JFrame frame = new JFrame("Jumping Ball");


    frame.getContentPane().add(new JumpingBall());
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
}
}

I've set myself the task of writing some code that simple makes a ball jump from the floor at the users command. Step 1 was to create a window and a floor - I noticed that the location I added my floor tended to be off screen and discovered here that frame.setSize(x,y) includes the borders and you should embed a JPanel inside the frame and size that instead. However upon attempting to make these changes my rectangle.fillRect(x,y,width,height) seems to appear as a small square top center regardless of the variables. Why could this be happening?

Code:

    import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;

public class JumpingBall extends JPanel{
    public void paint(Graphics g){
        Graphics2D rectangle = (Graphics2D) g;
        rectangle.setColor(Color.BLACK);
        rectangle.fillRect(0,0,300,30);
    }

public static void main(String[] args) {
    JFrame frame = new JFrame("Jumping Ball");
    JPanel panel = new JPanel();

    panel.setPreferredSize(new Dimension(300,300));
    panel.add(new JumpingBall());

    frame.getContentPane().add(panel);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
}
}

Here's a screenshot of my failed floor.

When you do custom painting:

  1. You should override paintComponent() not paint() and invoke super.paintComponent(g) as the first statement to make sure the background is cleared.

  2. Override the getPreferredSize() method of the class to return the size of the panel so layout managers can do there job. So in your case it should be (300, 300).

In your code you should not be setting the preferred size of any components. It is the job of the layout manager to determine the preferred size of a component.

panel.setPreferredSize(new Dimension(300,300));

You currently set the preferred size of the panel holding the jumping ball. This doesn't help because the preferred size of the jumping ball has not been set. You see a (10, 10) square because the FlowLayout of the panel determines the preferred size of your jumping ball panel is only (10, 10).

Also, there is no need to create a separate panel. You can just add the JumpingBall panel directly to the frame.

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