踪而透明的JPanel画 [英] Leaving a trace while painting on a transparent JPanel

查看:156
本文介绍了踪而透明的JPanel画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中相对较新的图形程序员,这里是一个简单的程序,我想。下面是完整的code:分为3类。

I am relatively new graphics programmer in Java and here is a simple program I was trying. Here is the full code: broken into 3 classes.

第1类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyPanelB extends JPanel
{
 int x=0;
    int y=0;
    public void paintComponent(Graphics g)
    {

    x=x+1;
    y=y+1;

   setOpaque(false);
   //setBackground(Color.cyan);


    g.setColor(Color.red);
    g.fillRect(x,y,x+1,y+1);



    }
}

2级:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyFrameB implements ActionListener
{
MyPanelB p1;

    public void go()
    {

    JFrame f1= new JFrame();
    f1.setLocation(150,50);
    f1.setSize(800,700);
    f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel p0= new JPanel();
    p0.setBackground(Color.yellow);
    p0.setLayout(new BorderLayout());
    f1.add(p0);

    p1= new MyPanelB();
    p0.add(p1);

    f1.setVisible(true);

    Timer t = new Timer(200,this);
    t.start();

    }

    public void actionPerformed(ActionEvent ev)
    {

    p1.repaint();


    }
}

3级(主类):

Class 3(Main Class):

public class MyBMain
{
    public static void main(String[] args)
    {

    MyFrameB m1= new MyFrameB();
    m1.go();

    }
}

如果我注释掉staement
setOpaque(假);
在1班,我得到(红色扩大矩形)痕迹,但不是黄色的背景。
否则,我没有得到一个痕迹,但我得到一个黄色的背景。
我想无论是黄色背景和跟踪。
这样我就获得了跟踪和黄色的背景,请随意修改我的code。
我公司提供的全code使人们可以轻松地检查出的输出。

If I comment out the staement setOpaque(false); in class 1, I get a trace(of red expanding rectangles) but not a yellow background. Otherwise I do not get a trace, but I do get a yellow background. I want both the yellow background and the trace. Please feel free to modify my code so that I get both a trace and a yellow background. I provided the full code so one can easily check out the output.

推荐答案

基本上你需要每次的paintComponent()方法被调用时重绘整个组件。这意味着你需要从0开始,遍历到x的当前值。

Basically you need to repaint the entire component every time the paintComponent() method is called. This means you need to start from 0 and iterate up to the current value of x.

所以,你的paintComponent()方法看起来应该是这样的:

So your paintComponent() method should look something like:

public void paintComponent(Graphics g)
{
    super.paintComponent(g);

    x=x+1;
    y=y+1;

    for    (int i = 0; i < x; i++)
    {

        g.setColor(getForeground());
        //g.fillRect(x,y,x+1,y+1);
        g.fillRect(i,i,i+1,i+1);
    }
}

这意味着你不需要你的panel0。我也改变了code创建PANEL1:

This means you don't need your panel0. I also change the code for creating panel1:

p1= new MyPanelB();
p1.setForeground(Color.RED);
p1.setBackground(Color.YELLOW);
f1.add(p1);

即使是这样code我贴你是不正确的。在x / y值不应该在的paintComponent()方法被更新。尝试,而你的code正在执行明白为什么调整框架?

Even this code I posted for you is not correct. The x/y values should NOT be updated in the paintComponent() method. Try resizing the frame while your code is executing to see why?

您的ActionListener应该调用的方法在你的类PANEL1更新类的属性来告诉的paintComponent()多少次迭代和油漆的广场被调用时。在该的paintComponent()方法应该在我创建了循环引用这个属性。

Your ActionListener should invoke a method in your panel1 class to update property of the class to tell the paintComponent() how many times to iterate and paint the square when it is invoked. The the paintComponent() method should reference this property in the loop that I created.

这篇关于踪而透明的JPanel画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆