有两个代码?
我主要是想看看
Graphics g = frame.getGraphics(); 的用法
其中一个代码运行正常如下:public class GraphicsFrameDemo extends JFrame {
/**
*
*/
private JLabel label;
private static int w = 256;
private static int h = 256;
private static final int[] rgb = new int[w * h];
private Random random = new Random();
private int repeat = 100;
private static final long serialVersionUID = 2865351937976283987L; public GraphicsFrameDemo(){
super("example");
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.setPreferredSize(new Dimension(200,200));
JButton b1 = new JButton("run");
b1.addActionListener(EventHandler.create(ActionListener.class, this, "start"));
label = new JLabel("Label");
this.getContentPane().add(label,BorderLayout.CENTER);
this.getContentPane().add(b1,BorderLayout.SOUTH);
super.pack();
// super.setLocationRelativeTo(null);
super.setVisible(true);
}
public void start(){
System.out.println("start");
final Object that = this;
new Thread(new Runnable(){
public void run(){
String result = "<html>";
Method[] methods = that.getClass().getDeclaredMethods();
for(int i=0;i<methods.length;i++){
String temp = methods[i].getName();
// System.out.println(temp);
if(temp.startsWith("test")){
long t1 = System.currentTimeMillis();
try {
methods[i].invoke(that);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long t2 = System.currentTimeMillis();
result += String.format("%s: %d<br>",temp,t2-t1);
}
}
final String text = result;
SwingUtilities.invokeLater(new Runnable(){
public void run(){
label.setText(text);
repaint();
}
}); }
}).start();
}
// public void paint(Graphics g){
//
// super.paint(g);
//// g.drawString("msg", 50, 50);
//
// }
private void genNoise(){
for(int j=0;j<rgb.length;j++){
rgb[j] = random.nextInt();
}
}
private long doTestBufferedImage(BufferedImage image){
Graphics g = this.getGraphics();
long t0 = System.nanoTime();
for(int i=0;i<repeat;i++){
genNoise();
image.setRGB(0,0,w,h,rgb,0,w);
g.drawImage(image,0,0,null); //注意坐标
g.drawString("hello", 50, 50);
}
long t1 = System.nanoTime();
g.dispose();
return t1-t0;
}
public long testINT_RGB(){
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
return doTestBufferedImage(image);
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(false);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GraphicsFrameDemo demo = new GraphicsFrameDemo();
com.sun.awt.AWTUtilities.setWindowShape(demo, new Ellipse2D.Double(0, 10, demo.getWidth(), demo.getHeight()));
AWTUtilities.setWindowOpacity(demo , 0.9f);
}
});
}
}
我主要是想看看
Graphics g = frame.getGraphics(); 的用法
其中一个代码运行正常如下:public class GraphicsFrameDemo extends JFrame {
/**
*
*/
private JLabel label;
private static int w = 256;
private static int h = 256;
private static final int[] rgb = new int[w * h];
private Random random = new Random();
private int repeat = 100;
private static final long serialVersionUID = 2865351937976283987L; public GraphicsFrameDemo(){
super("example");
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.setPreferredSize(new Dimension(200,200));
JButton b1 = new JButton("run");
b1.addActionListener(EventHandler.create(ActionListener.class, this, "start"));
label = new JLabel("Label");
this.getContentPane().add(label,BorderLayout.CENTER);
this.getContentPane().add(b1,BorderLayout.SOUTH);
super.pack();
// super.setLocationRelativeTo(null);
super.setVisible(true);
}
public void start(){
System.out.println("start");
final Object that = this;
new Thread(new Runnable(){
public void run(){
String result = "<html>";
Method[] methods = that.getClass().getDeclaredMethods();
for(int i=0;i<methods.length;i++){
String temp = methods[i].getName();
// System.out.println(temp);
if(temp.startsWith("test")){
long t1 = System.currentTimeMillis();
try {
methods[i].invoke(that);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long t2 = System.currentTimeMillis();
result += String.format("%s: %d<br>",temp,t2-t1);
}
}
final String text = result;
SwingUtilities.invokeLater(new Runnable(){
public void run(){
label.setText(text);
repaint();
}
}); }
}).start();
}
// public void paint(Graphics g){
//
// super.paint(g);
//// g.drawString("msg", 50, 50);
//
// }
private void genNoise(){
for(int j=0;j<rgb.length;j++){
rgb[j] = random.nextInt();
}
}
private long doTestBufferedImage(BufferedImage image){
Graphics g = this.getGraphics();
long t0 = System.nanoTime();
for(int i=0;i<repeat;i++){
genNoise();
image.setRGB(0,0,w,h,rgb,0,w);
g.drawImage(image,0,0,null); //注意坐标
g.drawString("hello", 50, 50);
}
long t1 = System.nanoTime();
g.dispose();
return t1-t0;
}
public long testINT_RGB(){
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
return doTestBufferedImage(image);
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(false);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GraphicsFrameDemo demo = new GraphicsFrameDemo();
com.sun.awt.AWTUtilities.setWindowShape(demo, new Ellipse2D.Double(0, 10, demo.getWidth(), demo.getHeight()));
AWTUtilities.setWindowOpacity(demo , 0.9f);
}
});
}
}
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JLabel;public class AwtFrame {
private Frame f;
public void createAndShowUI(){
f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
Label label = new Label("hello");
Button b1 = new Button("click");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("click");
Graphics g = f.getGraphics();
g.drawString("msg: ", 100, 100); //该段失效
g.dispose();
f.repaint();
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
// System.out.println("done");
}
});
f.setPreferredSize(new Dimension(200, 200));
f.add(label,BorderLayout.CENTER);
f.add(b1,BorderLayout.SOUTH);
f.pack();
Graphics g = f.getGraphics();
g.drawString("hello", 50, 50);
g.dispose();
// f.paint(g);
f.setVisible(true);
}
public static void main(String[] args) {
AwtFrame af = new AwtFrame();
af.createAndShowUI();
}
}
想看看代码怎么改
改下坐标就可以显示出来了~~~public class AwtFrame {
private Frame f;
public void createAndShowUI(){
f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
f.setPreferredSize(new Dimension(800, 600));
Label label = new Label("label");
Button b1 = new Button("click");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
System.out.println("click");
Graphics g = f.getGraphics();
// g.setColor(Color.RED);
// System.out.println(g.getColor());
BufferedImage img1 = new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB);
try {
img1 = ImageIO.read(new File("F:\\picture\\chart1.jpg"));
} catch (IOException ioe) {
// TODO Auto-generated catch block
ioe.printStackTrace();
}
g.drawImage(img1, 50, 50, null);
// g.drawString("msg: ", 100, 100); //该段失效
g.dispose();
}
});
}
});
f.add(label,BorderLayout.NORTH);
f.add(b1,BorderLayout.SOUTH);
f.pack();
// f.paint(g);
f.setVisible(true);
}
public static void main(String[] args) {
AwtFrame af = new AwtFrame();
af.createAndShowUI();
}
}