如何在油漆使用的Graphics2D还是有更好的方式来做到这一点? [英] How do I use graphics2D in paint or is there a better way to do this?

查看:121
本文介绍了如何在油漆使用的Graphics2D还是有更好的方式来做到这一点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的Graphics2D,但我不能让我得到它,以显示我的图形。有没有更好的办法去这将允许我使用重绘()?最后,我想使有图像设置为背景,并能够利用它,然后在框架中的内容保存为图像。

 进口java.awt.image中的*。
进口javax.imageio.ImageIO中;
进口的javax.swing *。进口的java.io.File;
进口java.io.IOException异常;
进口的java.net.URL;进口javax.swing.JApplet中;
进口java.awt中的*。//假设绘图区域150由150
公共类测试扩展JApplet的
{
  最终诠释半径= 25;
  INT宽度= 200,高度= 200; IMG的BufferedImage =新的BufferedImage(
   宽度,高度,BufferedImage.TYPE_INT_ARGB);  公共无效漆()
  {
    Graphics2D的G = img.createGraphics();
    g.setColor(Color.orange);
    g.fillRect(0,0,150,150);
    g.setColor(Color.black);    g.drawOval((二分之一百五十○ - 半径),(二分之一百五十○ - 半径),半径* 2,半径* 2);
   }
}


解决方案

好了,所以,


  1. 您有公共无效漆()的到底是什么做的笑?你需要一个图形对象。 公共无效漆(图形G)就像是当你的小程序初始化这会导致自动调用默认的方法。


  2. 您有的Graphics2D G = img.createGraphics(); 当你需要使用默认的图形摹对象,并将其铸造成一个Graphics2D对象,像这样的Graphics2D G2D =(Graphics2D的)克;


  3. 您要搜索的双缓冲多一点信息也:)


反正...这code ++工程,所以从它带到你想要什么:)

P.S注意我是如何实现Runnable接口;你不需要做这个,如果你只是想使用的Graphics2D code。它只是使类线程和用于游戏帧频:)

希望这有助于。

 进口java.applet中的*。
进口java.awt中的*。
公共类测试扩展的Applet实现Runnable {公共布尔isRunning = FALSE;
公众诠释半径= 25;
公共无效的start(){
    isRunning = TRUE;
    新的Thread(本)。开始();
}公共无效停止(){
    isRunning = FALSE;
}公共无效漆(图形G){
    //创建Graphics2D对象,演员将AS一个Graphics2D
    Graphics2D的G2D =(Graphics2D的)克;
    g2d.setColor(Color.ORANGE);
    g2d.fillRect(0,0,150,150);    g2d.setColor(Color.BLACK);
    g2d.drawOval((二分之一百五十○ - 半径),(二分之一百五十○ - 半径),半径* 2,半径* 2);
}公共无效的run(){    而(isRunning){
        重绘();
        尝试{
            视频下载(17);
        }赶上(InterruptedException的E){
            e.printStackTrace();
        }
    }
}
}

I want to use graphics2D but I can't get I get it to display my graphics. Is there a better way to go about this that would allow me to use repaint()? Eventually I want to make have a image set as a background and be able to draw on it then save the contents of the frame as a image.

import java.awt.image.*;
import javax.imageio.ImageIO;
import javax.swing.*;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.swing.JApplet;
import java.awt.*;

// assume that the drawing area is 150 by 150
public class test extends JApplet
{
  final int radius = 25;
  int width = 200, height = 200;

 BufferedImage img = new BufferedImage(
   width, height, BufferedImage.TYPE_INT_ARGB);

  public void paint (  )
  { 
    Graphics2D g = img.createGraphics();
    g.setColor( Color.orange );
    g.fillRect( 0, 0, 150, 150 );
    g.setColor( Color.black );

    g.drawOval( (150/2 - radius), (150/2 - radius), radius*2, radius*2 );
   }
}

解决方案

Ok so,

  1. You have public void paint( ) what the hell is this doing lol? You need a graphics object. public void paint(Graphics g) is like the default method which gets called automatically when your applet is initialised.

  2. You have Graphics2D g = img.createGraphics(); when you need to use your default Graphics g object and cast it into a Graphics2D object like so Graphics2D g2d = (Graphics2D) g;

  3. You should search for a little more information on double buffering too :)

anyway... This code works so take from it what you want :)

P.S Note how I implemented Runnable; You do not need to do this if you only want to use the Graphics2D code. It is just making the class a thread and is used for game frame rates :)

Hope this helped.

import java.applet.*;
import java.awt.*;


public class Test extends Applet implements Runnable{

public boolean isRunning = false;
public int radius = 25;


public void start() {
    isRunning = true;
    new Thread(this).start();
}

public void stop() {
    isRunning = false;
}

public void paint(Graphics g) {
    //Create Graphics2D object, cast g as a Graphics2D
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.ORANGE);
    g2d.fillRect(0, 0, 150, 150);

    g2d.setColor(Color.BLACK);
    g2d.drawOval((150/2 - radius), (150/2 - radius), radius * 2, radius * 2);
}

public void run() {

    while (isRunning) {
        repaint();
        try {
            Thread.sleep(17);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


}

这篇关于如何在油漆使用的Graphics2D还是有更好的方式来做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆