Allen Anand
Allen Anand

Reputation: 13

error with drawing rectangle in java swing

The program compiles but I can't see the rectangle on the window, Can someone help me, I am new to this. My goal is just to draw three rectangles on the window. for a traffic light program.

import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Color;
import java.awt.Canvas;
import java.lang.String;
import java.awt.Graphics;

class traffic extends Canvas implements ActionListener{
static JRadioButton b1,b2,b3;
  static JPanel jp = new JPanel();
static JFrame win= new JFrame("Traffic light");
traffic(){
  b1= new JRadioButton("red");
  b2= new JRadioButton("green");
  b3= new JRadioButton("yellow");
  jp.add(b1);
  jp.add(b2);
  jp.add(b3);

  win.add(jp);
    win.setLayout(new FlowLayout());
  win.setSize(500,500);
  win.setVisible(true);

  win.setDefaultCloseOperation(win.DISPOSE_ON_CLOSE);
  b1.addActionListener(this);
  b2.addActionListener(this);
  b3.addActionListener(this);

}
public void actionPerformed(ActionEvent e) throws ArithmeticException
{ }
public void paint(Graphics g){

   g.setColor(Color.RED);
      g.fillRect(130, 30,100, 80);
}
public static void main(String[] args)
{    traffic tr= new traffic();
     tr.repaint();
}
}

Upvotes: 0

Views: 179

Answers (2)

Allen Anand
Allen Anand

Reputation: 13

import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Color;
import java.awt.Canvas;
import java.lang.String;
import java.awt.Graphics;

  class traffic extends JPanel implements ActionListener{
static JRadioButton b1,b2,b3;
static JLabel l1;
traffic(){

JFrame win= new JFrame("Traffic light");
  l1= new JLabel("my name");
  b1= new JRadioButton("red");
  b2= new JRadioButton("green");
  b3= new JRadioButton("yellow");
  this.getPreferredSize();
  l1.setBounds(40,100,60,50);
  win.setSize(500,500);
  b1.setBounds(70,100,60,50);
  b2.setBounds(150,100,60,50);
  b3.setBounds(140,150,60,50);
  this.add(b1);
  this.add(b2);
  this.add(b3);
  this.add(l1);
  win.add(this);

  win.setVisible(true);
  win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  b1.addActionListener(this);
  b2.addActionListener(this);
  b3.addActionListener(this);

}
@Override
public Dimension getPreferredSize() {
   return new Dimension(500,500);
}
public void actionPerformed(ActionEvent e)
{     if(e.getSource()==b1)
  {     b2.setSelected(false);
        b3.setSelected(false);
        this.repaint();
  }
     else if(e.getSource()==b2)
     { this.repaint();
       b1.setSelected(false);
       b3.setSelected(false);
     }

     else if(e.getSource()==b3)
     {  this.repaint();
       b1.setSelected(false);
       b2.setSelected(false);
     }

}
@Override
public void paintComponent(Graphics g) {
   super.paintComponent(g);
 if(b1.isSelected())
 {
   g.setColor(Color.RED);
   g.fillRect(150, 60,100, 100);
 }
 else if(b2.isSelected())
 { g.setColor(Color.GREEN);
  g.fillRect(150, 60,100, 100);
}
else if(b3.isSelected())
{
  g.setColor(Color.YELLOW);
  g.fillRect(150, 60,100, 100);

}
else{
  g.setColor(Color.WHITE);
  g.fillRect(150, 60,100, 100);
}
}
public static void main(String[] args)
{
      traffic tr= new traffic();



}





}

Upvotes: 0

WJS
WJS

Reputation: 40057

  • Don't extend Canvas (or even use it), but do extend JPanel.
  • add the JPanel to the JFrame - (win.add(this))
  • Your button's are filling the panel, hiding the background. Give them a size
  • Add them to the JPanel just using add(b1) etc
  • Don't override paint, but do override paintComponent. And do it as follows:
@Override
public void paintComponent(Graphics g) {
   super.paintComponent(g);
   // your stuff here
  • Don't set the size of the JFrame. Set the size of the JPanel. Otherwise your JFrame border absorbs some of the size, making your panel smaller than you may want. Do it as follows.
@Override
public Dimension getPreferredSize() {
   return new Dimension(500,500);
}

You still have other logic to work out but this should get you started.

Style corrections

These are not critical to the execution of your code but important to learn.

  • By convention, classes begin with an upper case character.
  • Use the class name, not an instance when referring to static values.
win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

Upvotes: 1

Related Questions