Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.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 无法在另一个矩形上绘制_Java_Swing_Graphics_Jpanel_Drawrect - Fatal编程技术网

Java 无法在另一个矩形上绘制

Java 无法在另一个矩形上绘制,java,swing,graphics,jpanel,drawrect,Java,Swing,Graphics,Jpanel,Drawrect,在计时器的每个滴答声之后 填充矩形(Color.ORANGE),直到整个矩形着色,然后运行矩形 又空了。“停止”按钮将停止绘画。但是,当代码绑定到慢慢使矩形变为白色时,矩形将完全变为白色 如何在橙色矩形上绘制? import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyFrame extends JFrame implements ActionListener { privat

在计时器的每个滴答声之后 填充矩形(Color.ORANGE),直到整个矩形着色,然后运行矩形 又空了。“停止”按钮将停止绘画。但是,当代码绑定到慢慢使矩形变为白色时,矩形将完全变为白色

如何在橙色矩形上绘制?

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

public class MyFrame extends JFrame implements ActionListener
{ 
    private JPanel beerPannel;
    private JButton startBtn;
    private JButton stopBtn;
    private Timer beerTimer;
    private int timeCountUp = 0;
    private int timeCountDown = 22;


    public MyFrame() {
        setLayout(new FlowLayout()); 

        // create textPlane
        beerPannel = new JPanel();
        beerPannel.setPreferredSize(new Dimension(250, 300)); 
        beerPannel.setBackground(Color.WHITE);

        // create startBtn
        startBtn = new JButton("Start");
        startBtn.addActionListener(this);

        // create stopBtn
        stopBtn = new JButton("Stop");
        stopBtn.addActionListener(this);

        // make timer
        beerTimer = new Timer(1000, this);

        // add to the screen
        add(beerPannel); add(startBtn); add(stopBtn);

        // config pop-up window 
        setSize(300, 380); 
        setVisible(true); 
        setDefaultCloseOperation(EXIT_ON_CLOSE); 
    }

    public void actionPerformed(ActionEvent event) {

        // start with a empty glass of beer
        Graphics beerGlass = beerPannel.getGraphics(); // set beerGlass ass writePanel
        // x -> y -> width -> height
        beerGlass.drawRect(50, 50, 150, 220); 

        // onClick startBtn
        if(event.getSource() == startBtn) {
            // start Timer
            beerTimer.start();
        }
        // onClick stopBtn          
        if(event.getSource() == stopBtn) {
            // stop Timer   
            beerTimer.stop();   
        }
        // als beer timer is gestart
        if(event.getSource() == beerTimer) {
            // count timeClicks
            int telUp = timeCountUp++;

            // standardt vars
            int width = 148;
            int x = 51;

            // start with a empty glass of beer

            // set color
            Graphics fillBeerGlass = beerPannel.getGraphics();

            if(telUp <= 22) { 
                // set height
                int height = 10 * telUp - 2;
                // set y
                int y = 259 - height + 10;      

                fillBeerGlass.setColor(Color.ORANGE); 
                fillBeerGlass.drawRect(x, y, width, height); 
                fillBeerGlass.fillRect(x, y, width, height); 

            } else {
                // count timeClicks
                int telDown = timeCountDown--;
                // verklein inhoud
                // set height
                int height = 10 * telDown - 2;
                // set y
                int y = 259 - height + 10;      

                // set color
                fillBeerGlass.setColor(Color.WHITE); 
                fillBeerGlass.drawRect(x, y, width, height); 
                fillBeerGlass.fillRect(x, y, width, height); 


                // if count 0, stop timer 
                if(telDown == 0) {
                    beerTimer.stop();
                }
            }
        }
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类MyFrame扩展JFrame实现ActionListener
{ 
私人JPanel beerPannel;
私人JButton startBtn;
私人JButton stopBtn;
私人定时器;
私有int timeCountUp=0;
私人整数时间倒数=22;
公共MyFrame(){
setLayout(新的FlowLayout());
//创建文本平面
beerPanel=新的JPanel();
BeerPanel.setPreferredSize(新尺寸(250300));
蜂窝状退根(颜色:白色);
//创建startBtn
startBtn=新的JButton(“开始”);
startBtn.addActionListener(此);
//创建stopBtn
stopBtn=新的JButton(“停止”);
stopBtn.addActionListener(此);
//制造计时器
beerTimer=新计时器(1000,此);
//添加到屏幕上
添加(beerPannel);添加(startBtn);添加(stopBtn);
//配置弹出窗口
设置大小(300380);
setVisible(真);
setDefaultCloseOperation(关闭时退出);
}
已执行的公共无效操作(操作事件){
//先喝一杯空啤酒
Graphics beerGlass=BeerPanel.getGraphics();//设置beerGlass ass writePanel
//x->y->宽度->高度
beerGlass.drawRect(50,50,150,220);
//onClick startBtn
if(event.getSource()==startBtn){
//启动计时器
beerTimer.start();
}
//点击停止按钮
if(event.getSource()==stopBtn){
//停止计时器
beerTimer.stop();
}
//艾尔斯啤酒定时器是盖斯特
if(event.getSource()==beerTimer){
//计算时间点击次数
int telUp=timeCountUp++;
//标准变量
整数宽度=148;
int x=51;
//先喝一杯空啤酒
//定色
Graphics fillBeerGlass=beerPanel.getGraphics();

如果(telUp如mKorbel在评论中所述,这不是尝试在Swing中绘制的正确方法。相反,您需要创建一个具有状态的组件并整体重新绘制自身。然后使用一个更改状态的
计时器(例如,可能从50%满到55%满),并强制重新绘制。计时器可以使用相同的方法上下移动状态。

正如mKorbel在评论中提到的,这不是尝试在Swing中绘制的正确方法。相反,您需要创建一个具有状态的组件,并整体重新绘制自身。然后使用一个更改状态的
计时器(例如,可能从50%满到55%满),并强制重新绘制。计时器可以使用相同的方法上下移动状态。

我在这里没有看到问题。你想问什么?getGraphics();这不是在SwingI中绘制的正确方法。在这里看不到问题。你想问什么?getGraphics();这不是在秋千上绘画的正确方法