Program Graphic – Java


Fonts.java :


//Fonts.java

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

public class Fonts extends JFrame {

//
public Fonts()
{
super("Using Fonts");

setSize( 420, 125 );
setVisible( true );

}

public void paint( Graphics g )
{
super.paint( g );

g.setFont( new Font("Serif", Font.BOLD, 12));
g.drawString("Serif 12 point bold. " , 20, 50);

g.setFont( new Font("Monospaced", Font.ITALIC, 24));
g.drawString("Monospaced 24 point italic. " , 20, 70);

g.setFont( new Font("SansSerif", Font.PLAIN, 14));
g.drawString("SansSerif 14 point plain. " , 20, 90);

g.setColor( Color.RED );
g.setFont( new Font("Serif", Font.BOLD + Font.ITALIC, 18));
g.drawString(g.getFont().getName()+ " " + g.getFont().getSize() + "point bold italic. " , 20, 110);;

}

public static void main( String arg[] )
{
Fonts application = new Fonts ();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
}
}

LinesRectsOvals.java :


//LinesRectsOvals.java

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

public class LinesRectsOvals extends JFrame {

//
public LinesRectsOvals()
{
super("Drawing lines, rectangles and ovals ");

setSize( 400, 165 );
setVisible( true );

}

public void paint( Graphics g )
{
super.paint( g );

//Garis
g.setColor( Color.RED );
g.drawLine( 5, 30, 350, 30 );

//Box
g.setColor( Color.BLUE );
g.drawRect( 5, 40, 90, 55 );

//box warna penuh
g.fillRect( 100, 40, 90, 55 );

//Box Round
g.setColor( Color.CYAN );
g.fillRoundRect( 195, 40, 90, 55, 50, 50 );
g.drawRoundRect( 290, 40, 90, 55, 20, 20 );

//
g.setColor( Color.YELLOW );
g.draw3DRect( 5, 100, 90, 55, true );
g.fill3DRect( 100, 100, 90, 55, false );

g.setColor( Color.MAGENTA );
g.drawOval( 195, 100, 90, 55 );
g.fillOval( 290, 100, 90, 55 );

}

public static void main( String arg[] )
{
LinesRectsOvals application = new LinesRectsOvals ();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
}
}

ShowColor.java :


//ShowColor.java

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

public class ShowColors extends JFrame {

//
public ShowColors()
{
super("Using Color");

setSize( 450, 140 );
setVisible( true );

}

public void paint( Graphics g )
{
super.paint( g );

g.setColor( new Color(255, 0, 0));
g.fillRect( 25,25,100,20 );
g.drawString("Current RGB: " + g.getColor(), 130, 40);

g.setColor( new Color(0.0f, 1.0f, 0.0f));
g.fillRect( 25,50,100,20 );
g.drawString("Current RGB: " + g.getColor(), 130, 65);

g.setColor( Color.BLUE);
g.fillRect( 25,75,100,20 );
g.drawString("Current RGB: " + g.getColor(), 130, 90);

Color color = Color.MAGENTA;
g.setColor( color );
g.fillRect( 25,100,100,20 );
g.drawString("Current RGB: " + color.getRed() + ". " + color.getGreen() + ". " + color.getBlue(), 130, 115);

}

public static void main( String arg[] )
{
ShowColors application = new ShowColors ();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
}
}

ShowColor2.java :


//ShowColor2.java

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

public class ShowColors2 extends JFrame {

private JButton changeColorButton;
private Color color = Color.LIGHT_GRAY;
private Container container;

//
public ShowColors2()
{
super("Using JColorChooser");

container = getContentPane();
container.setLayout( new FlowLayout() );

changeColorButton = new JButton( "Change Color");
changeColorButton.addActionListener(

new ActionListener() {

public void actionPerformed ( ActionEvent event )
{
color = JColorChooser.showDialog(
ShowColors2.this, "Choose a color", color);

color = Color.LIGHT_GRAY;

container.setBackground( color );
}
}
);

container.add ( changeColorButton );

setSize( 400, 130 );
setVisible( true );

}

public void paint( Graphics g )
{
super.paint( g );

// g.setColor( color );
g.fillRect( 25,75,100,20 );
g.drawString("Current RGB: " + g.getColor(), 130, 90);

}

public static void main( String arg[] )
{
ShowColors2 application = new ShowColors2 ();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
}
}

Metrics.java :


//Metrics.java

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

public class Metrics extends JFrame {

//
public Metrics()
{
super("Demontrating FontMetrics");

setSize( 520, 210 );
setVisible( true );

}

public void paint( Graphics g )
{
super.paint( g );

g.setFont( new Font("Serif", Font.BOLD, 12));
FontMetrics metrics = g.getFontMetrics();
g.drawString("Current font: " + g.getFont(),10,40);
g.drawString("Ascent: " + metrics.getAscent(),10,55);
g.drawString("Descent: " + metrics.getDescent(),10,70);
g.drawString("Height: " + metrics.getHeight(),10,85);
g.drawString("Leading: " + metrics.getLeading(),10,100);

g.setFont( new Font("Serif", Font.ITALIC, 14));
metrics = g.getFontMetrics();
g.drawString("Current font: " + g.getFont(),10,130);
g.drawString("Ascent: " + metrics.getAscent(),10,145);
g.drawString("Descent: " + metrics.getDescent(),10,160);
g.drawString("Height: " + metrics.getHeight(),10,175);
g.drawString("Leading: " + metrics.getLeading(),10,190);

}

public static void main( String arg[] )
{
Metrics application = new Metrics ();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
}
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s