import java.awt.AlphaComposite;
創(chuàng)新互聯(lián)專注于江源企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計,商城系統(tǒng)網(wǎng)站開發(fā)。江源網(wǎng)站建設(shè)公司,為江源等地區(qū)提供建站服務(wù)。全流程專業(yè)公司,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import com.sun.awt.AWTUtilities;
public class demo extends JPanel
{
public demo()
{
//super();
setVisible(true);
setBackground(Color.BLACK);
setForeground(Color.WHITE);
setBounds(0, 0, 600, 600);
}
public static void main(String[] args)
{
JFrame f = new JFrame();
AWTUtilities.setWindowOpacity(f, 0.8f);
f.setSize(1024, 768);
f.add(new demo());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
@Override
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2d=(Graphics2D)g;
int width = getWidth()/2;
int height = getHeight()/2;
g2d.setStroke(new BasicStroke(6));//粗細
g2d.setColor(Color.red);
g2d.drawArc (5, 5, 500, 750, 45, 90+45); //圓弧
AlphaComposite alphaComposite=AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.42f);
g2d.setComposite(alphaComposite);//透明度
AffineTransform affineTransform=new AffineTransform();
affineTransform.setToTranslation(0, 0);
affineTransform.setToRotation(Math.PI/10);//旋轉(zhuǎn)
g2d.transform(affineTransform);
ImageIcon imageIcon= new ImageIcon(getClass().getResource("mac.jpg"));
g2d.drawImage(imageIcon.getImage(),0,0,null);//背景
Paint paint=new GradientPaint(0, 0, Color.RED, 222,222, Color.green, true);
g2d.setPaint(paint);
g2d.fillRoundRect(188, 188, 300, 300, 33, 33);//矩形
g2d.setClip(50,50,300, 300);
g2d.setColor(Color.blue);
g2d.fillPolygon(new int[] {0,200,400},new int[] {333,0,333},3);//三角形
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PaintOval {
public static void main(String[] args) {
JFrame frame=new JFrame();
frame.setSize(1024, 768);
MyPanel panel=new MyPanel();
frame.add(panel);
Thread thread =new Thread(panel);
thread.start();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MyPanel extends JPanel implements Runnable{
int x=30,y=30;
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.red);
g.drawOval(x, y, 50, 50);
}
public void run(){
while(true){
x++;
if(x1034){
x=0;
}
try{
Thread.sleep(20);
}catch(Exception e){
e.printStackTrace();
}
repaint();
}
}
}
我不知道你要什么方向移動的圓,我就畫了向右移動的圓
public class Exam
{
public static void main(String[] args)
{
Circle c=new Circle(3,4,5);
System.out.printf("圓心:(%f,%f),半徑:%f,面積:%f",c.x,c.y,c.r,c.countArea());
}
}
class Circle
{
public Circle()
{
this(0,0,0);
}
public Circle(double x,double y,double r)
{
this.x=x;
this.y=y;
this.r=r;
}
public double countArea()
{
return Math.PI*r*r;
}
/*private*/public double x,y,r;
}
//我給你動畫的代碼。然后你可以參考它寫出自己想要的效果。
package 練習(xí)題;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Graphics extends JFrame {
Thread th1 = null;
Graphics offScreen = null;
private int i=0;
private JPanel NewPanel=new NewPanel();
public Graphics() {
add(NewPanel);
Timer timer=new Timer(1000,new TimerListener());
timer.start();
}
public static void main(String[] args) {
Graphics frame = new Graphics();
frame.setTitle("Test");
frame.setSize(500, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class NewPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
protected void paintComponent(java.awt.Graphics g) {
super.paintComponent(g);
int Xcenter = getWidth() / 2;
int Ycenter = getHeight() / 2;
int radius = (int) (Math.min(getWidth(), getHeight()) * 0.4);
int x = Xcenter - radius;
int y = Ycenter - radius;
g.fillArc(x, y, 2 * radius, 2 * radius, 0 + i, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 90 + i, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 270 + i, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 180 + i, 30);
g.setColor(Color.red);
g.fillArc(x, y, 2 * radius, 2 * radius, 45 + i, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 135 + i, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 315 + i, 30);
g.fillArc(x, y, 2 * radius, 2 * radius, 225 + i, 30);
}
}
class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
i++;
NewPanel.repaint();
}
}
}
import?java.awt.Canvas;
import?java.awt.Graphics;
import?java.awt.Graphics2D;
import?java.awt.RenderingHints;
import?java.awt.image.BufferedImage;
/**
*?@author?ZhengYesheng
*/
public?class?RotateImageCanvas?extends?Canvas?implements?Runnable
{
private?static?final?long?serialVersionUID?=?-1997487731464495923L;
BufferedImage?img;
BufferedImage?rotatedImg;
int?degress?=?0;
public?RotateImageCanvas(BufferedImage?img)
{
super();
this.img?=?img;
new?Thread(this).start();
}
@Override
public?void?run()
{
while?(true)
{
//A,與B的代碼配合決定旋轉(zhuǎn)的速度
degress?+=?1;
degress?%=?360;
repaint();
try
{
if?(degress?==?0)
{
//繞一周后等待的時間在這里設(shè)置
Thread.sleep(3?*?1000);
}
else
{
//考慮到視覺平滑,這里不應(yīng)大約40。
Thread.sleep(30);
}
}
catch?(InterruptedException?e)
{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
}
}
@Override
public?void?paint(Graphics?graphics)
{
super.paint(graphics);
//獲取旋轉(zhuǎn)指定角度后的圖片。為了避免累計誤差,這里是用原始圖像旋轉(zhuǎn)的
rotatedImg?=?rotateImage(img,?degress);
//繪制旋轉(zhuǎn)后的圖片
graphics.drawImage(rotatedImg,?0,?0,?this);
}
/**
*?旋轉(zhuǎn)圖片為指定角度。
*?注意:1、這個方法實現(xiàn)了圖像的基于中點的旋轉(zhuǎn),要想繞指定點,需要配合Matrix類
*??????????2、為避免圖像被裁切,結(jié)果圖片的尺寸也需要動態(tài)計算
*??????????3、現(xiàn)在旋轉(zhuǎn)后有黑色背景,如果不需要這個效果,需要設(shè)置結(jié)果圖片的Alpha模式
*?
*?@param?bufferedimage
*????????????目標(biāo)圖像
*?@param?degree
*????????????旋轉(zhuǎn)角度
*?@return
*/
private?BufferedImage?rotateImage(BufferedImage?bufferedimage,?int?degree)
{
int?w?=?bufferedimage.getWidth();
int?h?=?bufferedimage.getHeight();
int?type?=?bufferedimage.getColorModel().getTransparency();
BufferedImage?img;
Graphics2D?graphics2d;
(graphics2d?=?(img?=?new?BufferedImage(w,?h,?type)).createGraphics())
.setRenderingHint(RenderingHints.KEY_INTERPOLATION,?RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(degree),?w?/?2,?h?/?2);
graphics2d.drawImage(bufferedimage,?0,?0,?null);
graphics2d.dispose();
return?img;
}
}
畫之前加上g.clearRect(0, 0, getWidth(), getHeight());
不然你上一次畫的還會呈現(xiàn)出來
這樣呢,就會閃爍,需要使用雙緩沖來解決了。
需要請追問。