简体   繁体   English

Java没有显示矩形?

[英]Java isn't displaying rectangle?

I'm trying it to display a rectangle at the specified location but it isn't showing up. 我正在尝试在指定位置显示一个矩形,但它没有显示出来。 The background is magenta but the rectangle is not there. 背景是品红色但矩形不存在。

Also: How can I access more colors besides the "Color.(insert very few options here)" 另外:除了“颜色之外,如何访问更多颜色。(此处插入极少数选项)”

import javax.swing.*;
import java.awt.*;


class Screensaver {
    private final static int FRAME_HEIGHT = 600;
    private final static int FRAME_WIDTH = 600;
    public static void main(String[] args){
        JFrame win;
        Container contentPane;
        Graphics g;


        win = new JFrame();
        win.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        win.setVisible(true);
        contentPane = win.getContentPane();
        contentPane.setBackground(Color.MAGENTA);
        g = contentPane.getGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(80, 350, 400, 250);

    }
}

You shouldn't be painting in main(); 你不应该在main()中绘画; it would be better to extend JPanel, change paintComponent(), and add the panel to the JFrame. 最好扩展JPanel,更改paintComponent(),并将面板添加到JFrame。

public class PaintPanel extends JPanel {

    public PaintPanel() {
        setBackground(Color.MAGENTA);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g); // This paints the background

        g.setColor(Color.BLACK);
        g.fillRect(80, 350, 400, 250);
    }
}

And in main(): 在main()中:

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.add(new PaintPanel());
    frame.setVisible(true);
}

If you want to make your own Colors, you can use the new Color(int red, int green, int blue) constructor. 如果要创建自己的颜色,可以使用新的Color(int red,int green,int blue)构造函数。

Try this : 尝试这个 :

import javax.swing.*;
import java.awt.*;

class Screensaver {

    private final static int FRAME_HEIGHT = 600;
    private final static int FRAME_WIDTH = 600;

    public static void main(String[] args) {
        JFrame win;
        Container contentPane;
        win = new JFrame();
        win.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        win.setVisible(true);
        Component comp = new Component();
        contentPane = (Container) win.getContentPane().add(comp);


    }
    }

    class Component extends JComponent {

    @Override
   public void paintComponent(Graphics g) {
    super.paintComponent(g);
        g.setColor(Color.magenta);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.BLACK);
        g.fillRect(80, 350, 400, 250);
    }
}

and about the color , you van create new Color and set the RED,GREEN,BLUE as you want , Try this : 关于颜色,你可以创建新颜色并根据需要设置RED,GREEN,BLUE,试试这个:

g.setColor(new Color(red, green, blue));

If you're going to draw stuff create a class that inherits from a Swing container, JComponent, JPanel, etc. and override the paint(Graphics g) method. 如果您要绘制东西,则创建一个继承自Swing容器,JComponent,JPanel等的类,并覆盖paint(Graphics g)方法。 If you see the magenta background then contentPane must have been added. 如果您看到洋红色背景,则必须添加contentPane。 What's probably happening is its painting the magenta background over your rectangle, but that's just a guess. 可能发生的是它在你的矩形上绘制洋红色背景,但这只是猜测。 Try this... 尝试这个...

public class ContentPane extends JComponent {
     public ContentPane() {

     }

     @Override
     public void paint(Graphics g) {
          g.setColor(Color.MAGENTA);
          g.fillRect(0, 0, getWidth(), getHeight());

          g.setColor(Color.BLACK);
          g.fillRect(80, 350, 400, 250);

     }
}

Then in your main class instantiate an object of the ContentPane class and add it to your JFrame. 然后在您的主类中实例化ContentPane类的对象并将其添加到您的JFrame。 The call to repaint() is probably unnecessary but that will make sure it gets painted. 对repaint()的调用可能是不必要的,但这将确保它被绘制。 You can also try messing around with the paintComponent(Graphics g) method, there is a difference between the two, I believe its the order they're called from the update method but I'm probably wrong about that, however that should solve your problem. 你也可以尝试使用paintComponent(Graphics g)方法,两者之间有区别,我相信它是从更新方法中调用的顺序,但我可能错了,但是这应该解决你的问题问题。

As for colors, consult the API. 至于颜色,请参考API。 You can pass RGB values into the Color constructor to create all sorts of colors. 您可以将RGB值传递到Color构造函数中以创建各种颜色。 Color color = new Color(int red, int green, int blue). Color color = new Color(int red,int green,int blue)。 I think that is the easiest way to create custom colors but like I said its all in the API. 我认为这是创建自定义颜色的最简单方法,但就像我在API中所说的一样。 Hope this helps. 希望这可以帮助。

The rectangle is drawn once, however every time the JFrames repaint() method is called it erases it and draws the basic Components. 矩形被绘制一次,但每次调用JFrames repaint()方法时,它都会擦除它并绘制基本组件。 To add custom drawing in JFrames you have to override the paint method. 要在JFrame中添加自定义绘图,您必须覆盖绘制方法。 Here I improved your code slightly to get you started down that path. 在这里,我稍微改进了你的代码,让你开始沿着这条路走下去。 As you can see you want to draw the box in the Paint method. 如您所见,您想在Paint方法中绘制框。 I made a Container element that does your drawing and removed the background color, adding it to the paint method as well. 我制作了一个Container元素,用于绘制并删除背景颜色,并将其添加到paint方法中。

try this 尝试这个

import javax.swing.*;
import java.awt.*;

public class JavaApplication10 {
    private final static int FRAME_HEIGHT = 600;
    private final static int FRAME_WIDTH = 600;
    public static void main(String[] args){
        JFrame win = new JFrame();
        win.setContentPane(new MyBoxContainer());
        win.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        win.setVisible(true);
    }

    private static class MyBoxContainer extends Container {
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            g.setColor(Color.MAGENTA);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.BLACK);
            g.fillRect(80, 350, 400, 250);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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