GradientPaint |
package javaswing; import java.awt.Color; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.SwingUtilities; /** * * @web http://java-buddy.blogspot.com/ */ public class JavaTestSwing { static JFrameWin jFrameWindow; public static class MyComponent extends JComponent{ @Override protected void paintComponent(Graphics g) { //g.drawImage(bufferedImage, 0, 0, null); Graphics2D graphics2D = (Graphics2D)g; int width = getWidth(); int height = getHeight(); float x1 = width/4; float y1 = height/4; float x2 = width/4 + width/2; float y2 = height/4 + height/2; Color color1 = Color.RED; Color color2 = Color.BLUE; GradientPaint gradientPaint = new GradientPaint(x1, y1, color1, x2, y2, color2); graphics2D.setPaint(gradientPaint); Rectangle2D.Double rectangle = new Rectangle2D.Double(width/4, height/4, width/2, height/2); graphics2D.fill(rectangle); } } public static class JFrameWin extends JFrame{ public JFrameWin(){ this.setTitle("java-buddy.blogspot.com"); this.setSize(300, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyComponent myComponent = new MyComponent(); this.add(myComponent); } } public static void main(String[] args){ Runnable doSwingLater = new Runnable(){ @Override public void run() { jFrameWindow = new JFrameWin(); jFrameWindow.setVisible(true); } }; SwingUtilities.invokeLater(doSwingLater); } }
No comments:
Post a Comment