import java.awt.AlphaComposite;
创新互联专注于江源企业网站建设,响应式网站设计,商城系统网站开发。江源网站建设公司,为江源等地区提供建站服务。全流程专业公司,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务
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);//旋转
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 练习题;
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的代码配合决定旋转的速度
degress += 1;
degress %= 360;
repaint();
try
{
if (degress == 0)
{
//绕一周后等待的时间在这里设置
Thread.sleep(3 * 1000);
}
else
{
//考虑到视觉平滑,这里不应大约40。
Thread.sleep(30);
}
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void paint(Graphics graphics)
{
super.paint(graphics);
//获取旋转指定角度后的图片。为了避免累计误差,这里是用原始图像旋转的
rotatedImg = rotateImage(img, degress);
//绘制旋转后的图片
graphics.drawImage(rotatedImg, 0, 0, this);
}
/**
* 旋转图片为指定角度。
* 注意:1、这个方法实现了图像的基于中点的旋转,要想绕指定点,需要配合Matrix类
* 2、为避免图像被裁切,结果图片的尺寸也需要动态计算
* 3、现在旋转后有黑色背景,如果不需要这个效果,需要设置结果图片的Alpha模式
*
* @param bufferedimage
* 目标图像
* @param degree
* 旋转角度
* @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());
不然你上一次画的还会呈现出来
这样呢,就会闪烁,需要使用双缓冲来解决了。
需要请追问。