Swing 图形重新定位 drawString
Swing graphics reposition drawString
我正在用户屏幕上绘制一个字符串,我想移动该字符串,但它不会改变位置,这是我的代码
public static int x = 200, y = 200;
public static Window draw() {
Window w = new Window(null) {
@Override
public void paint(Graphics g) {
super.paint(g);
System.out.println("repainting");
final Font font = getFont().deriveFont(48f);
g.setFont(font);
g.setColor(Color.WHITE);
final String message = "Hello";
FontMetrics metrics = g.getFontMetrics();
g.drawString(message, x, y);
}
@Override
public void update(Graphics g) {
paint(g);
}
};
w.setAlwaysOnTop(true);
w.setBounds(w.getGraphicsConfiguration().getBounds());
w.setBackground(new Color(0, true));
w.setVisible(true);
return w;
}
public static void main(String[] args) throws AWTException {
Window window = draw();
x = 500;
y = 500;
window.repaint();
window.invalidate();
}
}
它似乎没有改变文本位置,它打印出 repainting
所以点方法被调用,我在 paint 方法中打印了 x, y
似乎是也更新了,所以图形有问题,不想绘制新字符串。
只需删除您的 Window class 并将其替换为 JFrame。然后自定义 class 应该是一个 JPanel 并且只是覆盖 paintComponent。我猜它是行不通的,所以你正在努力让它工作,但你最终得到了一些非常狡猾的代码。
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import javax.swing.Timer;
import java.awt.EventQueue;
public class Confusion{
static int x = 100;
static int y = 0;
static double theta = 0;
public static void startGui(){
JFrame frame = new JFrame("title");
JPanel panel = new JPanel(){
public void paintComponent(Graphics g){
g.drawString("string", x, y);
}
};
frame.setSize(640, 480);
frame.add(panel);
frame.setVisible(true);
Timer timer = new Timer( 30, (e)->{
x = (int)(300 + Math.sin(theta)*200);
y = (int)(300 - Math.cos(theta)*200);
theta += 0.1;
panel.repaint();
} );
timer.start();
}
public static void main(String[] args) throws Exception {
EventQueue.invokeAndWait( Confusion::startGui );
}
}
我正在用户屏幕上绘制一个字符串,我想移动该字符串,但它不会改变位置,这是我的代码
public static int x = 200, y = 200;
public static Window draw() {
Window w = new Window(null) {
@Override
public void paint(Graphics g) {
super.paint(g);
System.out.println("repainting");
final Font font = getFont().deriveFont(48f);
g.setFont(font);
g.setColor(Color.WHITE);
final String message = "Hello";
FontMetrics metrics = g.getFontMetrics();
g.drawString(message, x, y);
}
@Override
public void update(Graphics g) {
paint(g);
}
};
w.setAlwaysOnTop(true);
w.setBounds(w.getGraphicsConfiguration().getBounds());
w.setBackground(new Color(0, true));
w.setVisible(true);
return w;
}
public static void main(String[] args) throws AWTException {
Window window = draw();
x = 500;
y = 500;
window.repaint();
window.invalidate();
}
}
它似乎没有改变文本位置,它打印出 repainting
所以点方法被调用,我在 paint 方法中打印了 x, y
似乎是也更新了,所以图形有问题,不想绘制新字符串。
只需删除您的 Window class 并将其替换为 JFrame。然后自定义 class 应该是一个 JPanel 并且只是覆盖 paintComponent。我猜它是行不通的,所以你正在努力让它工作,但你最终得到了一些非常狡猾的代码。
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import javax.swing.Timer;
import java.awt.EventQueue;
public class Confusion{
static int x = 100;
static int y = 0;
static double theta = 0;
public static void startGui(){
JFrame frame = new JFrame("title");
JPanel panel = new JPanel(){
public void paintComponent(Graphics g){
g.drawString("string", x, y);
}
};
frame.setSize(640, 480);
frame.add(panel);
frame.setVisible(true);
Timer timer = new Timer( 30, (e)->{
x = (int)(300 + Math.sin(theta)*200);
y = (int)(300 - Math.cos(theta)*200);
theta += 0.1;
panel.repaint();
} );
timer.start();
}
public static void main(String[] args) throws Exception {
EventQueue.invokeAndWait( Confusion::startGui );
}
}