有一种简单的方法可以在Java中操作PNG吗?我知道我可以读入BufferedImage并将其写回来,但我需要在图像边缘添加清晰的像素.是否有捷径可寻?
我的图像旋转有问题
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
AffineTransform at = new AffineTransform();
at.setToIdentity();
at.translate(x, y);
at.rotate(Math.toRadians(angle));
g2.transform(at);
image.paintIcon(c, g2);
Run Code Online (Sandbox Code Playgroud)
我使用此代码在绘制之前旋转图片(图像是我创建的一个类,以帮助我处理图片的加载.
不幸的是,我遇到了图像边缘变得非常糟糕的问题(参见图片)

我有什么想法可以提高抽奖的质量?
贾森
我有一个扩展jlabel并使用paintComponent绘制它的类,如下所示:paintPhotos.java
package myApp;
import java.awt.*;
import javax.swing.*;
/**
*
* @author PAGOLINA
*/
public class paintPhotos extends javax.swing.JLabel {
public Image img; int w; int h;
public paintPhotos(Image img, int w, int h) {
this.img = img; this.w = w; this.h = h;
System.out.println("am paintclass");
}
@Override
public void paintComponent(Graphics p) {
System.out.println("am here");
super.paintComponent(p);
Graphics2D g2 = (Graphics2D) p;
p.drawImage(img, 0, 0, w, h, this);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试从另一个类的构造函数中绘制时(AddScore.java).
public AddScore() {
initComponents();
setLocationRelativeTo(null);
removeNotify();
setUndecorated(true);
Image imag = new …Run Code Online (Sandbox Code Playgroud) 大家好,我正在做一个线程来更新JFrame上的球,所以我重新绘制屏幕......然后将球更新到它的位置......然后再次绘制屏幕......画出球和同样的周期......这是代码
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
Thread t = new Thread()
{
public void run()
{
while(true)
{
repaint();
b2.update(ob,2);
b2.paint(ob.getGraphics());
b2.setT(b2.getT() + 1);
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
System.out.println("Error in Sleeping");
}
}
}
};
t.start();
}
Run Code Online (Sandbox Code Playgroud)
但问题是我没有看到球...屏幕的油漆总是覆盖球,球就像在Jframe下面.
我有一个有趣的问题.使用Java图形,我想画一个圆圈中的平行线,这些圆线间隔有一些预定义的间隙常数.圆具有已知的x和y位置以及半径.线应该从圆的中心开始并且应该向外移动.例如,

我想成像,最简单的方法是首先绘制线条以填满整个方块,如下所示:

然后可能绘制4个多边形并用白色填充它们.4个多边形标记如下:

如您所见,在这种情况下,多边形由左顶点(x,y)定义,宽度和高度的边由圆的半径定义,然后是(x +半径,y +半径)的弧.
需要反馈:
重要提示:请注意,虽然此解决方案具有垂直线,但应根据某个角度θ来定义线条.也就是说,它们可以成角度(但彼此平行).
如果有人能提供实际绘制的代码,我将永远感激不尽!:)
public void draw(Graphics g, int x, int y, int radius, int lineGap, int lineThickness, int theta) {
//g = the graphics object
//x,y = the top left coordinate of the square
//radius = the radius of the circle, the width of the rectangle, the height of the rectangle
//lineGap = the gap in between each of the lines
//lineThickness = the thickness of the lines in …Run Code Online (Sandbox Code Playgroud) 这是代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* Winter Assignment for Java 2012/13
*/
// Class definition
public class Main extends JPanel implements ActionListener {
private JSlider sizeslider;
private int startX, startY, endX, endY, val;
private Color shapeColor = Color.black;
private JComboBox shapeChoice, colorChoice;
private Point firstPoint;
private Circle firstCircle;
private Square firstSquare;
private Doughnut firstDoughnut;
private Triangle firstTriangle;
private Pentagon firstPentagon;
private Point aShape;
static final int SlideMin = 10;
static final int SlideMax = …Run Code Online (Sandbox Code Playgroud) 所以我试图改变我在教程中制作的全屏游戏的背景,我正在尝试将背景更改为绿色,但它保持黑色,代码有什么问题?
Screen.java
package debels;
import java.awt.*;
import javax.swing.JFrame;
public class Screen {
private GraphicsDevice vc;
public Screen(){
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc = env.getDefaultScreenDevice();
}
public void setFullScreen(DisplayMode dm, JFrame window){
window.setUndecorated(true);
window.setResizable(false);
vc.setFullScreenWindow(window);
if (dm != null && vc.isDisplayChangeSupported()){
try{
vc.setDisplayMode(dm);
}catch (Exception e){}
}
}
public Window getFullScreen(){
return vc.getFullScreenWindow();
}
public void restoreScreen(){
Window w = vc.getFullScreenWindow();
if (w != null){
w.dispose();
}
vc.setFullScreenWindow(null);
}
}
Run Code Online (Sandbox Code Playgroud)
Main.java
package debels;
import java.awt.*;
import javax.swing.*;
public class Main extends …Run Code Online (Sandbox Code Playgroud) 我正试图将一个字符串集中在一个Panel中.
目前我这样做:
public void paintComponent(Graphics g) {
super.paintComponent(g);
int stringWidth = 0;
int stringAccent = 0;
int xCoordinate = 0;
int yCoordinate = 0;
// get the FontMetrics for the current font
FontMetrics fm = g.getFontMetrics();
/** display new message */
if (currentMessage.equals(message1)) {
removeAll();
/** Centering the text */
// find the center location to display
stringWidth = fm.stringWidth(message2);
stringAccent = fm.getAscent();
// get the position of the leftmost character in the baseline
xCoordinate = getWidth() / 2 - …Run Code Online (Sandbox Code Playgroud) 我知道如何绘制圆角矩形,但我想分别为每个角定义圆度,并绘制如下图所示的内容:

我正在尝试用Java实现Floyd Steinberg算法,使用java.awt.image.BufferedImage.
我使用了这里描述的算法 和自定义调色板,我希望得到或多或少与维基百科示例中相同的图像(或者由Gimp生成的图像),但是我得到了一个非常不同的版本.
你可以看到我得到的东西

我显然缺少一些东西(输出图像的颜色不属于我的调色板),但我无法弄清楚是什么.
我做错了什么?
这是代码:
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.io.File;
import java.io.IOException;
public class FloydSteinbergTest {
private static final Color[] PALETTE = new Color[]{
new Color(221, 221, 221),
new Color(19, 125, 62),
new Color(179, 80, 188),
new Color(107, 138, 201),
new Color(177, 166, 39),
new Color(65, 174, 56),
new Color(208, 132, 153),
new Color(64, 64, 64),
new Color(154, 161, 161),
new Color(46, 110, 137),
new Color(126, 61, 181),
new Color(46, …Run Code Online (Sandbox Code Playgroud)