import javax.swing.*;
import java.awt.*;
import java.io.*;
class ColoreRectangle
{
private int width;
private int height;
private int x;
private int y;
private Color colorone;
JFrame windows;
ColoreRectangle(int w,int h, int x,int y)
{
width=w;
height=h;
this.x=x;
this.y=y;
colorone=Color.RED;
windows=new JFrame("ColoreRectangle");
windows.setSize(width,height);
windows.setVisible(true);}
void paint()
{
Graphics g=windows.getGraphics();
g.setColor(colorone);
g.fillRect(x, y, 50, 50);}
void erase()
{
Graphics g=windows.getGraphics();
g.clearRect(x,y,50,50);
}}
public class MyColor {
public static void main(String [] args) throws IOException
{
ColoreRectangle one=new ColoreRectangle(200,200,50,50);one.paint();one.erase();
System.out.println("(press Enter to exit)");
try
{
System.in.read();
}
catch(IOException e)
{
return;
}
}
}
程序可以运行,但是我要渲染的图形有时有,有时又没有,请各们大侠看一下,谢谢
import java.awt.*;
import java.io.*;
class ColoreRectangle
{
private int width;
private int height;
private int x;
private int y;
private Color colorone;
JFrame windows;
ColoreRectangle(int w,int h, int x,int y)
{
width=w;
height=h;
this.x=x;
this.y=y;
colorone=Color.RED;
windows=new JFrame("ColoreRectangle");
windows.setSize(width,height);
windows.setVisible(true);}
void paint()
{
Graphics g=windows.getGraphics();
g.setColor(colorone);
g.fillRect(x, y, 50, 50);}
void erase()
{
Graphics g=windows.getGraphics();
g.clearRect(x,y,50,50);
}}
public class MyColor {
public static void main(String [] args) throws IOException
{
ColoreRectangle one=new ColoreRectangle(200,200,50,50);one.paint();one.erase();
System.out.println("(press Enter to exit)");
try
{
System.in.read();
}
catch(IOException e)
{
return;
}
}
}
程序可以运行,但是我要渲染的图形有时有,有时又没有,请各们大侠看一下,谢谢
jvm会自动调用,这样当程序重绘组件时就会执行你的代码
你使用的方法是手动重绘,当jvm更新组件时,你画的东西会被擦掉的