import java.awt.*; import javax.swing.*; public class MyPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.RED); g.drawLine(10, 10, 100, 100); g.setColor(Color.GREEN); g.drawRect(50, 50, 100, 100); } }
import java.awt.*; public class MyPanel { public static void main(String[] args) { Frame frame = new Frame("My Panel"); frame.setSize(200, 200); frame.setVisible(true); Graphics g = frame.getGraphics(); g.setColor(Color.BLUE); g.fillRect(50, 50, 100, 100); } }In this example, we create a Frame and set its size and visibility. We then get the Graphics object from the frame using the getGraphics method, and set its color using setColor. Finally, we draw a filled rectangle using the Graphics object. The Java Graphics setColor method is part of the java.awt package library, which provides basic graphical user interface tools for Java programs.