Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在一个面板中的不同类实例中使用repaint()_Java_Graphics_Jpanel_Repaint - Fatal编程技术网

Java 在一个面板中的不同类实例中使用repaint()

Java 在一个面板中的不同类实例中使用repaint(),java,graphics,jpanel,repaint,Java,Graphics,Jpanel,Repaint,我有一个draw类,它扩展了JPanel,并有一个名为drawing的void方法,其中包含repaint() public class draw extends JPanel { public draw(int position_x, int position_y, int width, int height) { positionx = position_x; positiony = position_y; this.width =

我有一个draw类,它扩展了JPanel,并有一个名为drawing的void方法,其中包含
repaint()

public class draw extends JPanel {

    public draw(int position_x, int position_y, int width, int height) {
        positionx = position_x;
        positiony = position_y;
        this.width = width;
        this.height = height;
    }

    public void drawing() {

        repaint();
    }

    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.fillRect(positionx, positiony, width, height);
    }
}
所以,我的目的是在一个JPanel中创建很多这样的矩形,这样他们就可以创建一个图形栏

public class coin_launcher {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Coin Launcher");
        frame.setVisible(true);
        frame.setSize(1920, 1080);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        draw object = new draw(2,2,100,2);
        frame.add(object);
        object.drawing();

        draw object2 = new draw(2,6,200,2);
        frame.add(object2);
        object2.drawing();
    }
}
问题是,当我在两个对象中调用
drawing()
时,只会绘制一个对象

如果使用调试器,它只是第一个,如果我不使用,它只是第二个。我需要制作100个条形图,但每次它都会重新绘制
JPanel
,如何在一个
JPanel
中添加不同的绘制类,而不删除其他类?

JFrame
默认情况下使用
边框布局,这意味着只添加了最后一个组件(到默认的中心位置)将由布局管理

有关更多详细信息,请参阅

一个直接的解决方案可能是使用不同的布局管理器,但我认为这是错误的解决方案

相反,您应该有一个组件,它能够基于“模型”中的可用数据绘制多个条形图

这将数据源与数据表示分离,允许生成这些数据的多个不同实现,这不应该破坏其他实现

在这种情况下,“视图”不应该关心数据是如何获得或浸泡的,只应该它符合指定的契约,就像wise不关心数据是如何呈现的一样


有关更多详细信息,请参见

此答案是一个非常基本和简化的

“您应该有一个组件,可以绘制多个 条形图,基于“模型”中的可用数据

引自

导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.Rectangle;
导入java.util.array;
导入java.util.List;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类CoinLauncher{
公共静态void main(字符串[]args){
JFrame=新JFrame(“硬币发射器”);
框架设置尺寸(920480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//要更好地实现,请使用
//封装数据
矩形[]条={
新矩形(2,2100,2),
新矩形(2,6200,2),
};
绘制对象=新绘制(Arrays.asList(bar));
帧。添加(对象);
frame.setVisible(true);
}
}
类Draw扩展了JPanel{
私人最终名单栏;
绘制(列表栏){
这个。巴=巴;
}
@凌驾
公共组件(图形g){
超级组件(g);
g、 setColor(Color.BLUE);
用于(矩形条:条){
g、 fillRect(条形图x、条形图y、条形图宽度、条形图高度);
}
}
}
实现一个模型来封装视图所需的数据,可以简单到:

public class CoinLauncher {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Coin Launcher");
        frame.setSize(920, 480);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Model model = new Model();
        model.addBar( new Rectangle(2,2,100,2) );
        model.addBar( new Rectangle(2,6,200,2) );

        Draw object = new Draw(model);
        frame.add(object);
        frame.setVisible(true);
    }
}

class Draw extends JPanel {

    private final List<Rectangle> bars;

     Draw(Model model) {
         bars = model.getBars();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        for(Rectangle bar : bars){
            g.fillRect(bar.x, bar.y, bar.width, bar.height);
        }
    }
}

class Model {

    private final List<Rectangle> bars;

    Model(){
        bars = new ArrayList<>();
    }

    void addBar(Rectangle rectangle){
        bars.add(rectangle);
    }

    List<Rectangle> getBars() { return bars; }
}
公共类CoinLauncher{
公共静态void main(字符串[]args){
JFrame=新JFrame(“硬币发射器”);
框架设置尺寸(920480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
模型=新模型();
addBar模型(新矩形(2,2100,2));
addBar(新矩形(2,6200,2));
绘制对象=新绘制(模型);
帧。添加(对象);
frame.setVisible(true);
}
}
类Draw扩展了JPanel{
私人最终名单栏;
绘制(模型){
bars=model.getBars();
}
@凌驾
公共组件(图形g){
超级组件(g);
g、 setColor(Color.BLUE);
用于(矩形条:条){
g、 fillRect(条形图x、条形图y、条形图宽度、条形图高度);
}
}
}
类模型{
私人最终名单栏;
模型(){
bars=新的ArrayList();
}
void addBar(矩形){
添加(矩形);
}
List getbar(){return bar;}
}

为了使您的代码易于阅读,请缩进代码,然后按照下面的说明进行操作。
public class CoinLauncher {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Coin Launcher");
        frame.setSize(920, 480);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Model model = new Model();
        model.addBar( new Rectangle(2,2,100,2) );
        model.addBar( new Rectangle(2,6,200,2) );

        Draw object = new Draw(model);
        frame.add(object);
        frame.setVisible(true);
    }
}

class Draw extends JPanel {

    private final List<Rectangle> bars;

     Draw(Model model) {
         bars = model.getBars();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        for(Rectangle bar : bars){
            g.fillRect(bar.x, bar.y, bar.width, bar.height);
        }
    }
}

class Model {

    private final List<Rectangle> bars;

    Model(){
        bars = new ArrayList<>();
    }

    void addBar(Rectangle rectangle){
        bars.add(rectangle);
    }

    List<Rectangle> getBars() { return bars; }
}