我知道,这是一个非常基本的问题,但我不知道为什么我的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());
}}
Class 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();
}
我已经尝试在另一个类中使用paintComponent
方法,但这没有改变任何内容。