0

Can anyone give me some information what did I do wrong? Program runs but drowing a rectangle or anything else doesnt work. There is just empty space on the middle. I tried open the program from terminal and eclipse on kubuntu 15 and windows. Always with the same result.

I m just starting my adventure with java so please be patient.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyGUI {
JFrame frame;
JLabel label;

public static void main(String[] args)
{
    MyGUI gui = new MyGUI();
    gui.go();
}

public void go()
{
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton labelButton = new JButton("Change label");
    labelButton.addActionListener(new LabelListener());

    JButton colorButton = new JButton("Change color");
    colorButton.addActionListener(new ColorListener());

    label = new JLabel("LABEL");
    DrawSmth2 drawPanel = new DrawSmth2();

    frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
    frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
    frame.getContentPane().add(BorderLayout.EAST, labelButton);
    frame.getContentPane().add(BorderLayout.WEST, label);

    frame.setSize(680,480);
    frame.setVisible(true);
}

class LabelListener implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        label.setText("DONE");
    }
}

class ColorListener implements ActionListener
{
    public void actionPerformed(ActionEvent e) {
        frame.repaint();        
    }
}
public class DrawSmth2 extends JPanel{

    public void PaintComponent(Graphics g)
    {
    g.setColor(Color.blue);
    g.fillRect(0, 0, getWidth(), getHeight());
    }
}
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Please don't change your question in a way it will invalidate already posted answers. If you have followup question ask it in new post. – Pshemo Jan 04 '16 at 22:12
  • Take 2 or 3 months and study the [Oracle Swing tutorial](http://docs.oracle.com/javase/tutorial/uiswing/). – Gilbert Le Blanc Jan 06 '16 at 15:53

2 Answers2

1

Java is case-sensitive which means that PaintComponent is not the same as paintComponent. To avoid such problems always add @Override annotation to methods you want to override (if you are not overriding existing method compiler will inform you about it - more info: When do you use Java's @Override annotation and why?).

Also don't forget to call super.paintComponent(g); at start of your own implementation of paintComponent to let Swing perform standard operations needed to properly paint this component.

Community
  • 1
  • 1
Pshemo
  • 122,468
  • 25
  • 185
  • 269
0

Just add gloabal variable count;

int count=0;
class ColorListener implements ActionListener
{
    public void actionPerformed(ActionEvent e) {
  count++;
        frame.repaint();        
    }
}
public class DrawSmth2 extends JPanel{

    public void paintComponent(Graphics g)
    {
 if(count%2==1) 
    g.setColor(Color.blue);

    g.fillRect(0, 0, getWidth(), getHeight());
 
    }
}
kurumkan
  • 2,635
  • 3
  • 31
  • 55