Screenshot
lets rock !
b. create a new project and name it PanelGradient
c. create a new Java Class and name ClassGradient
d. create new JFrame Form and give it name MainForm
e. insert the following syntax in the source code ClassGradient.java
package panelgradient;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
/**
*
* @author CodeMaster
*/ // MASUNDUH II
public class ClassGradient extends JPanel {
private BufferedImage gambarGradient;
private Color warna1 = Color.BLACK;
private Color warna2 = Color.BLUE;
public ClassGradient() {
super();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
if(isOpaque()){
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
setUpGradient();
g2d.drawImage(gambarGradient, 0, 0, getWidth(), getHeight(), null);
int lebar = getWidth();
int tinggi = getHeight() * 5 / 100;
Color light = new Color(1F, 1F, 1F, 0.5F);
Color dark = new Color(1F, 1F, 1F, 0.0F);
GradientPaint paint = new GradientPaint(0, 0, light, 0, tinggi, dark);
GeneralPath path = new GeneralPath();
path.moveTo(0, 0);
path.lineTo(0, tinggi);
path.curveTo(0, tinggi, lebar / 2, tinggi / 2, lebar, tinggi);
path.lineTo(lebar, 0);
path.closePath();
g2d.setPaint(paint);
g2d.fill(path);
paint = new GradientPaint(0, getHeight(), light, 0, getHeight() - tinggi, dark);
path = new GeneralPath();
path.moveTo(0, getHeight());
path.lineTo(0, getHeight() - tinggi);
path.curveTo(0, getHeight() - tinggi, lebar / 2, getHeight()
- tinggi / 2, lebar, getHeight() - tinggi);
path.lineTo(lebar, getHeight());
path.closePath();
g2d.setPaint(paint);
g2d.fill(path);
g2d.dispose();
}
}
/**
* membuat background gradient
*/
private void setUpGradient() {
gambarGradient = new BufferedImage(1, getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = (Graphics2D) gambarGradient.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint paint = new GradientPaint(0, 0, warna1, 0,getHeight(), warna2);
g2.setPaint(paint);
g2.fillRect(0, 0, 1, getHeight());
g2.dispose();
}
}
f. Clean and Build or press Shift + F11g.drag and drop design tab page mainForm.java
h. Set JFrame layout mainForm.java, right click select setLayout and select BorderLayout
i. Running the program or press Shift + F6
0 Response to "tutorial java create Panel Gradient"
Post a Comment