Skip to content

Instantly share code, notes, and snippets.

@teguhteja
Created February 6, 2020 06:53
Show Gist options
  • Save teguhteja/d091348f261d0c121fa8d53dbf415683 to your computer and use it in GitHub Desktop.
Save teguhteja/d091348f261d0c121fa8d53dbf415683 to your computer and use it in GitHub Desktop.
overridden paintComponent(Graphics g) or paint(Graphics g)
/**
*
* @author teguhteja
*/
import java.awt.*;
import javax.swing.*;
// Extend JPanel to override its paintComponent() method:
class NewPanel extends JPanel {
// Set background in constructor.
public NewPanel ()
{
this.setBackground (Color.cyan);
}
// Override paintComponent():
public void paintComponent (Graphics g)
{
// Always call super.paintComponent (g):
super.paintComponent(g);
// drawString() is a Graphics method.
// Draw the string "Hello World" at location 100,100
g.drawString ("Hello World!", 100, 100);
// Let's find out when paintComponent() is called.
System.out.println ("Inside paintComponent");
}
}
class TestSwing4 {
public static void main (String[] argv)
{
// Create a frame
JFrame f = new JFrame ();
// Set the title and other parameters.
f.setTitle ("Hello World Test");
f.setResizable (true);
// Background is going to be Panel's background.
// f.getContentPane().setBackground (Color.cyan);
f.setSize (500, 300);
// Add the panel using the default BorderLayout
NewPanel panel = new NewPanel ();
f.getContentPane().add (panel);
// Show the frame.
f.setVisible (true);
}
}
import java.awt.*;
import javax.swing.*;
// Extend JPanel to override paintComponent ()
class NewPanel1 extends JPanel {
public NewPanel1 ()
{
this.setBackground (Color.cyan);
}
public void paintComponent (Graphics g)
{
super.paintComponent (g);
// Draw a Square:
g.drawRect (50,50,50,50);
g.drawString ("Square", 50, 115);
// Circle:
g.drawOval (200,50,50,50);
g.drawString ("Circle", 200, 115);
// Rounded rectangle:
g.drawRoundRect (350,50,75,50,20,20);
g.drawString ("Rectangle", 350, 115);
// Draw a line across the middle:
g.drawLine (0,150,500,150);
// Now draw some filled shapes:
// Square:
g.fillRect (50,200,50,50);
g.drawString ("Square", 50, 265);
// Circle:
g.fillOval (200,200,50,50);
g.drawString ("Circle", 200, 265);
// Rounded rectangle:
g.fillRoundRect (350,200,75,50,20,20);
g.drawString ("Rectangle", 350, 265);
}
}
// Create a new Frame class.
class NewFrame extends JFrame {
public NewFrame ()
{
// Set the title and other parameters.
this.setTitle ("Some Geometric Figures");
this.setResizable (true);
this.setSize (500, 300);
// Create and add the panel.
NewPanel1 panel = new NewPanel1 ();
this.getContentPane().add (panel);
// Show the frame.
this.setVisible (true);
}
}
class TestSwing6 {
public static void main (String[] argv)
{
// Simply fire up the whole application.
NewFrame f = new NewFrame ();
}
}
import java.awt.*;
import javax.swing.*;
public class OvalPaint extends JPanel {
public void paintComponent(Graphics g) {
g.setColor(Color.orange);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.red);
g.fillOval(getWidth()/4, getHeight()/4,
getWidth()/2, getHeight()/2);
}
public static void main(String args[]) {
JFrame frame = new JFrame("OvalPaint");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
OvalPaint panel = new OvalPaint();
frame.add(panel);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
public class SmileyApp extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.YELLOW);
g.fillOval(10, 10, 200, 200);
// draw Eyes
g.setColor(Color.BLACK);
g.fillOval(55, 65, 30, 30);
g.fillOval(135, 65, 30, 30);
// draw Mouth
g.fillOval(50, 110, 120, 60);
// adding smile
g.setColor(Color.YELLOW);
g.fillRect(50, 110, 120, 30);
g.fillOval(50, 120, 120, 40);
}
public static void main(String[] args) {
SmileyApp smiley = new SmileyApp();
JFrame app = new JFrame("Smiley App");
app.add(smiley, BorderLayout.CENTER);
app.setSize(300, 300);
app.setLocationRelativeTo(null);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
}
@teguhteja
Copy link
Author

Screenshot_2020-02-06_14-55-54
Screenshot_2020-02-06_14-55-24
Screenshot_2020-02-06_14-55-03

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment