麻烦高手帮我看一下下面的Java程序到底错在哪里
importjava.awt.*;importjava.net.URL;importjavax.swing.JFrame;importjavax.swing.JPanel...
import java.awt.*;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class rotataimage extends JFrame{
Image img;
mycanvas canvaspanel=null;
public rotataimage(){
initialize();
}
public void initialize(){
URL imgurl=rotataimage.class.getResource("src/img.jpg");
img=Toolkit.getDefaultToolkit().getImage(imgurl);
canvaspanel=new mycanvas();
this.setBounds(100,100,300,300);
add(canvaspanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("图片旋转");
this.setVisible(true);
while(true){
try{
Thread.sleep(10);
repaint();
}
catch(Exception e){
e.printStackTrace();}
}
}
public static void main(String[] args) {
new rotataimage().setVisible(true);
}
}
class mycanvas extends JPanel{
int rotate=0;
public void paint(Graphics g){
Graphics2D g2=(Graphics2D)g;
g2.rotate(Math.toRadians(++rotate),150,150);
g2.drawImage(img, 0, 0, this);
}
}
下面的截图是图片存放的位置 展开
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class rotataimage extends JFrame{
Image img;
mycanvas canvaspanel=null;
public rotataimage(){
initialize();
}
public void initialize(){
URL imgurl=rotataimage.class.getResource("src/img.jpg");
img=Toolkit.getDefaultToolkit().getImage(imgurl);
canvaspanel=new mycanvas();
this.setBounds(100,100,300,300);
add(canvaspanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("图片旋转");
this.setVisible(true);
while(true){
try{
Thread.sleep(10);
repaint();
}
catch(Exception e){
e.printStackTrace();}
}
}
public static void main(String[] args) {
new rotataimage().setVisible(true);
}
}
class mycanvas extends JPanel{
int rotate=0;
public void paint(Graphics g){
Graphics2D g2=(Graphics2D)g;
g2.rotate(Math.toRadians(++rotate),150,150);
g2.drawImage(img, 0, 0, this);
}
}
下面的截图是图片存放的位置 展开
1个回答
展开全部
你好。给你改好了。可以运行了。
import java.awt.*;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class rotataimage extends JFrame {
Image img;
mycanvas canvaspanel = null;
public rotataimage() {
initialize();
}
public void initialize() {
URL imgurl = rotataimage.class.getResource("img.jpg");
img = Toolkit.getDefaultToolkit().getImage(imgurl);
canvaspanel = new mycanvas(img);
this.setBounds(100, 100, 300, 300);
add(canvaspanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("图片旋转");
this.setVisible(true);
while (true) {
try {
Thread.sleep(10);
repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new rotataimage().setVisible(true);
}
}
class mycanvas extends JPanel {
Image img;
int rotate = 0;
public mycanvas(Image img) {
// TODO Auto-generated constructor stub
this.img=img;
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.rotate(Math.toRadians(++rotate), 150, 150);
g2.drawImage(this.img, 0, 0, this);
}
}
运行结果:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询