Java 油漆组件don';不行。我能';我找不到原因
我知道,这是一个非常基本的问题,但我不知道为什么我的Java 油漆组件don';不行。我能';我找不到原因,java,paintcomponent,Java,Paintcomponent,我知道,这是一个非常基本的问题,但我不知道为什么我的paintComponent什么都不做。 它的主要功能应该是绘制图像,但它甚至不绘制矩形或圆形。 所以请帮助我-- 使用JFrame初始化: public class JuliaView extends JFrame implements Observer{ //:::: ACTIONS public final String ACTION_ENDE = "Ende"; public final String ACTION_PAINT = "
paintComponent
什么都不做。
它的主要功能应该是绘制图像,但它甚至不绘制矩形或圆形。
所以请帮助我--
使用JFrame
初始化:
public class JuliaView extends JFrame implements Observer{
//:::: ACTIONS
public final String ACTION_ENDE = "Ende";
public final String ACTION_PAINT = "Paint";
public final String ACTION_COMPLEX = "+a.x+b.x";
public final String ACTION_LINK = "Link";
public final String ACTION_CLEAR = "Clear";
//:::: Components
//private JFrame frame;
private Image image;
private JButton btEnde;
private JButton btPaint;
private JButton btClear;
private JPanel content;
public JuliaPanel drawArea; //Bereich für Paint
//private JButton btAutoPaint;
private JTextField tfComplex;
private JTextField tfLink;
//:::: Observer
private JuliaModel model;
private JuliaController controller;
public JuliaView(String titel,JuliaModel model){
//frame = new JFrame(titel);
super(titel);
this.model = model;
image = null;
drawArea = new JuliaPanel(model);
content = new JPanel();
}
public void makeView() { //Zur Zeit ausser Funktion
// Fenster
addWindowListener( controller);
initForm();
resetView();
}
/**
* Anordnen der Komponenten im GridBag
*/
public void initForm(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());;
this.setBounds(200, 200, 800, 600);
content.setBounds(0, 0, 200, 600);
content.setBackground(Color.BLACK);
content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
drawArea.setOpaque(true);
//content.add(tfComplex,BorderLayout.CENTER)
this.getContentPane().add(content,BorderLayout.WEST);
this.getContentPane().add(drawArea,BorderLayout.CENTER);
//left outs adding to content witch works fine
setVisible( true);
pack();
drawArea.testImage(); //Should fill the image
image = drawArea.getImage();
drawArea.paintComponents(getGraphics()); //Paint the image
//this.paintComponents(getGraphics());
}}
类别witch持有JPanel以供使用:
public class JuliaPanel extends JPanel {
public final int darstellungsBreite = 600;
public final int darstellungsHoehe = 600;
private JuliaModel model;
private BufferedImage image;
public JuliaPanel(JuliaModel model){
super();
this.setOpaque(true);
this.setBackground(Color.WHITE);
image = null;
this.model = model;
image = new BufferedImage(darstellungsBreite, darstellungsHoehe, BufferedImage.TYPE_INT_RGB);
setPreferredSize(new Dimension(darstellungsBreite,darstellungsHoehe));
}
public JuliaPanel(){
super();
image = null;
image = new BufferedImage(darstellungsBreite, darstellungsHoehe, BufferedImage.TYPE_INT_RGB);
setPreferredSize(new Dimension(darstellungsBreite,darstellungsHoehe));
}
private Color createColor(int in) //in[0,255]
{
float rgb = (float)in/256; //in[0.0,1.0]
if(rgb<=0.5){
Color c = new Color(rgb,(float) (rgb*1.5),rgb*2); //mal schauen was da bei rauskommt
return c;
}
else{
Color c = new Color(rgb,rgb,rgb);
return c;
}
}
public void testImage(){
System.out.println("Methode suc. called");
image.createGraphics().setColor(Color.BLACK);
image.createGraphics().fillRect(300, 300, 100, 100);
if(image == null){System.out.println("Image is null");}
}
public void createImage(){
for(int j=0;j<darstellungsBreite;j++)
for(int i=0;i<darstellungsHoehe;i++)
{
image.createGraphics().setColor(createColor(model.getFarbwert(j, i)));
image.createGraphics().drawRect(j, i, 1, 1);
image.createGraphics().fillRect(j, i, 1, 1);
}
}
public void clearImage(){
image.createGraphics().setColor(Color.WHITE );
image.createGraphics().clearRect(getX(), getY(), darstellungsBreite, darstellungsHoehe);
}
public BufferedImage getImage(){return image;}
public void paintComponents(Graphics g)
{
super.paintComponents(g);
//g.setColor(Color.WHITE);
//g.fillRect(0, 0, getWidth(), getHeight());
Graphics2D g2d = (Graphics2D)g.create();
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, 500, 500);
g2d.drawImage(image,0,0,this);
g2d.dispose();
}
公共类JuliaPanel扩展了JPanel{
公共最终int darstellungsBreite=600;
公共最终int darstellungsHoehe=600;
私有JuliaModel模型;
私有缓冲图像;
公共JuliaPanel(JuliaModel模型){
超级();
此.set不透明(true);
这个.背景(颜色.白色);
image=null;
this.model=模型;
图像=新的BuffereImage(darstellungsBreite、darstellungsHoehe、BuffereImage.TYPE_INT_RGB);
设置首选尺寸(新尺寸(darstellungsBreite,darstellungsHoehe));
}
公共图书馆{
超级();
image=null;
图像=新的BuffereImage(darstellungsBreite、darstellungsHoehe、BuffereImage.TYPE_INT_RGB);
设置首选尺寸(新尺寸(darstellungsBreite,darstellungsHoehe));
}
专用颜色createColor(int-in)//in[0255]
{
float rgb=(float)in/256;//in[0.0,1.0]
如果(rgbOverridepaintComponent
notpaintComponents
&不要显式调用它,让Swing这样做好的,我做了,现在它的绘制:)但是当你在我的Panel类中查看testImage()方法时。矩形在正确的位置,它是白色的,不是我输入的任何其他颜色