因此,我正在尝试创建一个可以删除包含敏感信息的调查部分的应用程序.但是我遇到了一些问题.
我想要做的是在BufferedImage上绘制填充的黑色矩形,给定x,y,宽度和所需区域的高度为黑色,然后将该新图像写回我的文件系统.这是我的代码.
        File imageFile = new File("images/template.jpg");
        BufferedImage img = ImageIO.read(imageFile);
        Graphics2D graph = img.createGraphics();
        graph.setColor(Color.BLACK);
        graph.fill(new Rectangle(x, y, width, height));
        graph.dispose();
        ImageIO.write(img, "jpg", new File("images/template.jpg"));
无论出于何种原因,资源中的图像在此代码段之后不会更改.关于我做错了什么的任何想法?
再一次,我做Java的图形(Graphics2D的),但我注意到,没有Polygon.Double或Polygon.Float类,而有Rectangle2D.Float和Rectangle2D.Double类.
有人知道为什么吗?我只需要使用双精度作为点绘制三角形.
我正在尝试编写需要使用Java中的Graphics2D类绘制许多字符串的应用程序.我需要获取每个String对象的大小(以计算每个字符串的确切位置).在调用paint()方法之前应该完成很多字符串,并且在程序开始时只执行一次(所以我还没有Graphics2D对象).我知道有一个方法Font.getStringBounds()但它需要一个FontRenderContext对象作为参数.
当我试图创建自己的对象时:
FontRenderContext frc = new FontRenderContext(MyFont.getTransform(), true, true)
然后获取字符串边界我总是得到不同的大小,而不是我在paint()方法中使用Graphics2D.getFontRenderContext()方法获得FontRenderContext.差异不大(约1E-3),但我想知道为什么会有任何区别?
但是,有没有更好更安全的方法来获取字符串的大小?
Thnx提前提供任何帮助!
我是Java的新手,并且一直在尝试用屏幕上的大量图像制作一些简单的游戏.从那时起,我一直在使用'Graphics'类来绘制这些图像,字符串和形状,但我最近遇到了Graphics2D,这似乎是同样的事情..
谁能告诉我他们之间的区别是哪一个最好用?如果我想使用Graphics2D,我是否必须为我的代码执行一些大的更新?
嗨我想将包含标签和按钮等组件的面板转换为图像文件.
我已经完成了以下代码.图像已保存.但面板的内容不可见或保存.任何人都可以告诉我如何使用其组件保存面板.
码:
package PanelToImage;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
public class sample extends JPanel {
public JPanel firstpanel;
public JPanel secondpanel;
JLabel label1, label2;
JButton button1, button2;
public sample() {
    firstpanel = new JPanel();
    firstpanel.setSize(400,300); 
    firstpanel.setBackground(Color.RED);
    secondpanel = new JPanel();
    secondpanel.setBackground(Color.GREEN);
    secondpanel.setSize(400,300); 
    label1 = new JLabel("label1");
    label2 = new JLabel("label2");
    button1 = new JButton("button1");
    button2 = new JButton("button2");
    firstpanel.add(label1);
    firstpanel.add(button1);
    secondpanel.add(label2);
    secondpanel.add(button2);
    saveImage(firstpanel);
    add(firstpanel);
    // add(secondpanel);
}
public static void main(String args[]) {
    JFrame …我无法调整线条粗细.我可以在图形中执行此操作,还是必须在Graphics2D中执行此操作?如果是这样,我该如何改变程序以使其运行?
谢谢!
import java.applet.Applet;
import java.awt.*;
public class myAppletNumberOne extends Applet {
    public void paint (Graphics page) {
        //Something here???
    }
}
我在Linux/XWindows上的简单Java2D应用程序中遇到系统事件和窗口刷新率之间的意外交互.最好用下面的小例子来说明.
该程序创建一个小窗口,其中以不同的旋转显示半圆.图形以每秒60帧的速度更新,以产生闪烁的显示.这是通过a BufferStrategy,即通过调用其show方法来实现的.
但是,我注意到当我(a)将鼠标移到窗口上以便窗口接收鼠标悬停事件或(b)按住键盘上的键以使窗口接收键盘事件时,闪烁明显增加.
由于BufferStrategy.show()调用的速率不受这些事件的影响,可以通过控制台上的打印输出看到(它们应该始终保持在60 fps左右).然而,更快的闪烁表明显示实际更新的速率确实改变了.
在我看来,除非生成鼠标或键盘事件,否则无法实现实际的,即每秒可见的60帧.
public class Test {
    // pass the path to 'test.png' as command line parameter
    public static void main(String[] args) throws Exception {
        BufferedImage image = ImageIO.read(new File(args[0]));
        // create window
        JFrame frame = new JFrame();
        Canvas canvas = new Canvas();
        canvas.setPreferredSize(new Dimension(100, 100));
        frame.getContentPane().add(canvas);
        frame.pack();
        frame.setVisible(true);
        int fps = 0;
        long nsPerFrame = 1000000000 / 60; // 60 = target fps
        long showTime = …BufferedImage逐像素存储),以在JPanel上绘制图像。AffineTransform。小图片(<.5GB)
1.1当我增加执行缩放的比例因子时,
  会抛出点异常:-    
java.lang.OutOfMemoryError:Java堆空间。
以下是用于缩放的代码-
    scaled = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    Graphics2D g2d = (Graphics2D)scaled.createGraphics();
    AffineTransform transformer = new AffineTransform();
    transformer.scale(scaleFactor, scaleFactor); 
    g2d.setTransform(transformer);
BigBufferedImage image = BigBufferedImage.create(newCol,newRow, BufferedImage.TYPE_INT_ARGB);但这不能传递给g2d.drawImage(image, 0, 0, this); 
JPanel的repaint方法由于某种原因而停止。
我尝试以低分辨率加载图像,其中读取像素,并且跳过/跳过了几列和几行。但是问题是如何确定随着图像大小的变化要跳过多少像素,因此我无法决定如何确定跳转参数。
    MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY,0, inChannel.size());
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    FloatBuffer floatBuffer = buffer.asFloatBuffer();
    for(int i=0,k=0;i<nrow;i=i+jump)  /*jump is the value to be skipped, nrow is height of …我正在尝试为特定目的创建一些特殊组件,在该组件上我需要绘制一个HTML字符串,这是一个示例代码:
 public class MyComponent extends JComponent{
     public MyComponent(){
        super();
     }
     protected void paintComponent(Graphics g){
        //some drawing operations...
        g.drawString("<html><u>text to render</u></html>",10,10);
     }
 }
不幸的是,drawString方法似乎没有识别HTML格式,它愚蠢地绘制字符串就像它一样.
有没有办法让这项工作?
我正在尝试编写一个2D游戏引擎,我正在尝试实现一个视口系统,这样当我在某个视口中绘图时,游戏坐标将转换为屏幕坐标,而无需手动进行转换.
我想要做的是创建一个Graphics2D添加setViewport方法的包装器.
我看到的方式有两种选择:
创建一个具有a实例Graphics2D并且具有与Graphics2Dplus 相同的方法的类,setViewport并且只调用Graphics2D实例上的相应方法.
子类Graphics2D,只需添加一个setViewport方法,然后只是转换Graphics2D为这个新类
我试过#2,因为#1似乎非常不切实际但遇到了一个ClassCastException.我无法演员Graphics或Graphics2D这个新班级.当我打印的图形对象投(之前Graphics或Graphics2D),两者出来的sun.java2d.SunGraphics2D.
尝试子类化和强制转换时,我做了一些根本错误的事情吗?如果没有,我该如何解决这个问题?
graphics2d ×10
java ×10
graphics ×2
swing ×2
applet ×1
awt ×1
bounds ×1
frame-rate ×1
html ×1
image ×1
inheritance ×1
jcomponent ×1
paint ×1
panel ×1
string ×1
x11 ×1