java awt画布getInstance未定义(新手)
试着做Udacity课程第二课的练习。尽管导入了这些类(我现在在java.awt.*上,但我也分别尝试了java.awt.Color和java.awt.Canvas(还需要Shape))java awt画布getInstance未定义(新手),java,canvas,Java,Canvas,试着做Udacity课程第二课的练习。尽管导入了这些类(我现在在java.awt.*上,但我也分别尝试了java.awt.Color和java.awt.Canvas(还需要Shape)) package com.jul.udacity.lesson2; 公共类TestRectangle{ 公共静态void main(字符串[]args){ //TODO自动生成的方法存根 矩形rect1=新矩形(100.0,100.0,200.0,100.0); rect1.draw(); } }您可能需要仔细
package com.jul.udacity.lesson2;
公共类TestRectangle{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
矩形rect1=新矩形(100.0,100.0,200.0,100.0);
rect1.draw();
}
}
您可能需要仔细检查课程说明java.awt.Canvas
没有getInstance()
方法。您只需使用new
制作Canvas
。因此,你要么没有仔细阅读,使用了错误的画布,要么发生了其他事情
另外,show()
方法已被弃用,因此我倾向于您应该使用不同的Canvas类
另外,关于如何使用Swing对象的讨论是正确的:这里的Canvas不是java.awt中的类-它们使用自己的名为Canvas的类。我建议抓取课程文件并使用它们。下面是Java入门课程中的Canvas类:
import java.awt.image.BufferedImage;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.RescaleOp;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Canvas
{
private static Canvas canvas = new Canvas();
private ArrayList<Shape> shapes = new ArrayList<Shape>();
private BufferedImage background;
private JFrame frame;
private CanvasComponent component;
private static final int MIN_SIZE = 100;
private static final int MARGIN = 10;
private static final int LOCATION_OFFSET = 120;
class CanvasComponent extends JComponent
{
public void paintComponent(Graphics g)
{
g.setColor(java.awt.Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(java.awt.Color.BLACK);
if (background != null)
{
g.drawImage(background, 0, 0, null);
}
for (Shape s : new ArrayList<Shape>(shapes))
{
Graphics2D g2 = (Graphics2D) g.create();
s.paintShape(g2);
g2.dispose();
}
}
public Dimension getPreferredSize()
{
int maxx = MIN_SIZE;
int maxy = MIN_SIZE;
if (background != null)
{
maxx = Math.max(maxx, background.getWidth());
maxy = Math.max(maxx, background.getHeight());
}
for (Shape s : shapes)
{
maxx = (int) Math.max(maxx, s.getX() + s.getWidth());
maxy = (int) Math.max(maxy, s.getY() + s.getHeight());
}
return new Dimension(maxx + MARGIN, maxy + MARGIN);
}
}
private Canvas()
{
component = new CanvasComponent();
if (System.getProperty("com.horstmann.codecheck") == null)
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(component);
frame.pack();
frame.setLocation(LOCATION_OFFSET, LOCATION_OFFSET);
frame.setVisible(true);
}
else
{
final String SAVEFILE ="canvas.png";
final Thread currentThread = Thread.currentThread();
Thread watcherThread = new Thread()
{
public void run()
{
try
{
final int DELAY = 10;
while (currentThread.getState() != Thread.State.TERMINATED)
{
Thread.sleep(DELAY);
}
saveToDisk(SAVEFILE);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
};
watcherThread.start();
}
}
public static Canvas getInstance()
{
return canvas;
}
public void show(Shape s)
{
if (!shapes.contains(s))
{
shapes.add(s);
}
repaint();
}
public void repaint()
{
if (frame == null) return;
Dimension dim = component.getPreferredSize();
if (dim.getWidth() > component.getWidth()
|| dim.getHeight() > component.getHeight())
{
frame.pack();
}
else
{
frame.repaint();
}
}
/**
* Pauses so that the user can see the picture before it is transformed.
*/
public void pause()
{
if (frame == null) return;
JOptionPane.showMessageDialog(frame, "Click Ok to continue");
}
/**
* Takes a snapshot of the screen, fades it, and sets it as the background.
*/
public static void snapshot()
{
Dimension dim = getInstance().component.getPreferredSize();
java.awt.Rectangle rect = new java.awt.Rectangle(0, 0, dim.width, dim.height);
BufferedImage image = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(java.awt.Color.WHITE);
g.fillRect(0, 0, rect.width, rect.height);
g.setColor(java.awt.Color.BLACK);
getInstance().component.paintComponent(g);
float factor = 0.8f;
float base = 255f * (1f - factor);
RescaleOp op = new RescaleOp(factor, base, null);
BufferedImage filteredImage
= new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
op.filter(image, filteredImage);
getInstance().background = filteredImage;
getInstance().component.repaint();
}
public void saveToDisk(String fileName)
{
Dimension dim = component.getPreferredSize();
java.awt.Rectangle rect = new java.awt.Rectangle(0, 0, dim.width, dim.height);
BufferedImage image = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) image.getGraphics();
g.setColor(java.awt.Color.WHITE);
g.fill(rect);
g.setColor(java.awt.Color.BLACK);
component.paintComponent(g);
String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
try
{
ImageIO.write(image, extension, new File(fileName));
}
catch(IOException e)
{
System.err.println("Was unable to save the image to " + fileName);
}
g.dispose();
}
}
导入java.awt.image.buffereImage;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.image.RescaleOp;
导入java.io.IOException;
导入java.io.File;
导入java.util.ArrayList;
导入javax.imageio.imageio;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
导入javax.swing.JOptionPane;
公共类画布
{
私有静态画布=新画布();
私有ArrayList形状=新建ArrayList();
私有缓冲图像背景;
私有JFrame;
私人拉票组件;
私有静态最终整数最小值=100;
专用静态最终整型余量=10;
专用静态最终整数位置_偏移=120;
类CanvasComponent扩展了JComponent
{
公共组件(图形g)
{
g、 setColor(java.awt.Color.WHITE);
g、 fillRect(0,0,getWidth(),getHeight());
g、 setColor(java.awt.Color.BLACK);
如果(背景!=null)
{
g、 drawImage(背景,0,0,空);
}
对于(形状s:新阵列列表(形状))
{
Graphics2D g2=(Graphics2D)g.create();
s、 绘画形态(g2);
g2.dispose();
}
}
公共维度getPreferredSize()
{
int maxx=最小尺寸;
int maxy=最小尺寸;
如果(背景!=null)
{
maxx=Math.max(maxx,background.getWidth());
maxy=Math.max(maxx,background.getHeight());
}
用于(形状s:形状)
{
maxx=(int)Math.max(maxx,s.getX()+s.getWidth());
maxy=(int)Math.max(maxy,s.getY()+s.getHeight());
}
返回新维度(maxx+边距,maxy+边距);
}
}
私有画布()
{
组件=新的画布组件();
if(System.getProperty(“com.horstmann.codecheck”)==null)
{
frame=新的JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架。添加(组件);
frame.pack();
帧设置位置(位置偏移,位置偏移);
frame.setVisible(true);
}
其他的
{
最后一个字符串SAVEFILE=“canvas.png”;
最终线程currentThread=Thread.currentThread();
线程观察线程=新线程()
{
公开募捐
{
尝试
{
最终整数延迟=10;
while(currentThread.getState()!=Thread.State.TERMINATED)
{
睡眠(延迟);
}
saveToDisk(SAVEFILE);
}
捕获(例外情况除外)
{
例如printStackTrace();
}
}
};
watcherThread.start();
}
}
公共静态画布getInstance()
{
返回画布;
}
公众休憩用地展览(s形)
{
如果(!shapes.contains))
{
形状。添加(s);
}
重新油漆();
}
公共无效重绘()
{
if(frame==null)返回;
尺寸标注=组件。getPreferredSize();
if(dim.getWidth()>component.getWidth()
||dim.getHeight()>component.getHeight())
{
frame.pack();
}
其他的
{
frame.repaint();
}
}
/**
*暂停,以便用户可以在图片转换之前看到它。
*/
公共空间暂停()
{
if(frame==null)返回;
showMessageDialog(框架,“单击Ok继续”);
}
/**
*拍摄屏幕快照,淡入淡出,并将其设置为背景。
*/
公共静态void快照()
{
维度dim=getInstance().component.getPreferredSize();
java.awt.Rectangle rect=新的java.awt.Rectangle(0,0,dim.width,dim.height);
BuffereImage=新的BuffereImage(rect.width、rect.height、buffereImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
g、 setColor(java.awt.Color.WHITE);
g、 fillRect(0,0,矩形宽度,矩形高度);
g、 setColor(java.awt.Color.BLACK);
getInstance().component.paintComponent(g);
浮动系数=0.8f;
浮动底座=255f*(1f-系数);
重新缩放操作=新的重新缩放操作(因子、基数、空);
缓冲区图像过滤器图像
=新的BuffereImage(image.getWidth()、image.getHeight()、image.getType());
op.filter(图像、过滤器尺寸);
getInstance().background=filteredImage;
格廷斯