方法的Java可以改变像素值 [英] Methods in Java which can change pixel values
问题描述
我工作的Java和可视化的动画样的程序。我现在用的摆动
和 AWT
库。我看着这一点,所有的人都使用油漆
组件的方法,然后重绘()十几实施
时,他们希望借助在JFrame形状。
I am working on an animation kind of program in java and for visualization. I am using the swing
and awt
libraries. I looked at a dozen implementations of this and all of them use a paint
Component method and then repaint()
when they want to draw shapes on the JFrame.
有一些,我可以从我希望可以为例如改变单一的像素值的任何类调用的方法?
Is there some method that I can call from any class that I want that can for example change a single pixel value?
我也想和形状的一个数组列表:
I also want to make an array list with shapes:
ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>();
和我很好奇,怎么事情添加到它。
我的想法是:
And I am curious how to add things to it. I was thinking:
Rectangle rect = "something"
rectangles.add(rect);
但我不知道的东西应该是什么。
我是比较新的这一点,所以如果我失去了一些东西明显的随意EX preSS的意见。
But I'm not sure what something should be. I am relatively new to this, so feel free to express your views if I'm missing something obvious.
推荐答案
显然,我是)咆哮的时候我号召大家使用重绘白痴(;这里是更好的办法。
说我想创建一个矩形或图片上的JFrame线或椭圆形,
Apparently I was ranting when I called everyone an idiot for using repaint(); here's the better way. Say I wanted to create a rectangle or image or line or oval on the jframe,
公开的JFrame帧=新的JFrame();结果
公共无效paintingHome(){结果
//设置在JFrame和东西结果
的System.out.println(这是矩形);结果
paintARectangle(frame.getGraphics(),42,256);结果
}结果
公共无效paintARectangle(图形克,INT XCOORD,诠释YCOORD,诠释的宽度,高度INT){结果
g.fillRact(XCOORD,YCOORD,宽度,高度);结果,
//这么简单结果
}结果
//你可以有你想要的结果尽可能多
公共无效paintWhatever(图形克,无论东西){结果
g.doSomethingWith(无论);结果
//这实际上不会工作,直到Oracle发布版本238249.3432.4结果
}结果
//使用frame.getGraphics键()方法的结果
//重绘是不必要的限制性
public JFrame frame = new JFrame();
public void paintingHome(){
//set up the jframe and stuff
System.out.println("This is a rectangle");
paintARectangle(frame.getGraphics(), 42, 256);
}
public void paintARectangle(Graphics g, int xCoord, int yCoord, int width, int height){
g.fillRact(xCoord, yCoord, width, height);
//simple as that
}
//and you can have as many as you want
public void paintWhatever(Graphics g, Something whatever){
g.doSomethingWith(whatever);
//that won't actually work until oracle releases version 238249.3432.4
}
//the key is using the frame.getGraphics() method
//repaint is unnecessary and restrictive
这篇关于方法的Java可以改变像素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!