Java graphics | SoloLearn: Learn to code for FREE!


Java graphics

I need help drawing a fillRect method that takes in width and height. And fills it with pixels.

2/3/2019 9:46:03 PM


3 Answers

New Answer


A example how you can do it: import java.awt.*; import javax.swing.*; class MyRec extends JPanel{ public static void main(String[] args) { MyRec mr = new MyRec(); JFrame frame = new JFrame(); frame.add(mr); frame.setSize(300,300); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void paint(Graphics g){ g.fillRect(50, 50, 70, 70); } }


can we add color in the graphic section


Yes, above fillRect you can use: g.setColor(;