如何在 Java 中的 JPanel 中添加图形
How can I add graphics in the JPanel in Java
我有 JPanel GuiMap,现在我想在此面板中绘制一些不同的图形(首先是几行)。
起初我有起点 currentX=0
和 currentY=0
。然后我将新的点放在方法 updatePos 中。此方法更改点。并且方法 paintComponent 在新点和旧点之间绘制线。
我的问题是只有最后一行可见。
如何重新绘制或重绘或更新面板??
我想看看新旧显卡!
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JPanel;
public class GuiMap extends JPanel{
private int currentX = 0, currentY = 0;
private int prevX, prevY;
GuiMap(int xpos, int ypos){
this.currentX = xpos;
this.currentY = ypos;
}
public void updatePoint(int xpos, int ypos) {
prevX = currentX;
prevY = currentY;
currentX = xpos;
currentY = ypos;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.drawLine(prevX, prevY, currentX, currentY);
}
}
public class GuiMapFrame extends JFrame {
static GuiMap guiPanel;
static JFrame frame;
public static void main(String[] args) throws InterruptedException{
frame = new JFrame("SuperGui");
guiPanel = new GuiMap();
frame.setContentPane(guiPanel);
frame.setSize(600, 480);
frame.setLocation(100,100);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiPanel.updatePoint(20, 80);
guiPanel.updatePoint(130, 50);
guiPanel.updatePoint(60, 175);
guiPanel.repaint();
}
}
其实你的问题就是camickr说的那样。每次重新绘制时,它都会清除之前绘制的内容。所以你需要创建多个对象并在 paint 组件中渲染每个对象
My problem is only the last line is visible. …
您需要做以下两件事之一:
- 将每一行绘制到 BufferedImage 并绘制 BufferedImage
- 保留要绘制的所有线条的列表,然后遍历此列表
有关这两种方法的工作示例,请参阅 Custom Painting Approaches。
我有 JPanel GuiMap,现在我想在此面板中绘制一些不同的图形(首先是几行)。
起初我有起点 currentX=0
和 currentY=0
。然后我将新的点放在方法 updatePos 中。此方法更改点。并且方法 paintComponent 在新点和旧点之间绘制线。
我的问题是只有最后一行可见。
如何重新绘制或重绘或更新面板??
我想看看新旧显卡!
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JPanel;
public class GuiMap extends JPanel{
private int currentX = 0, currentY = 0;
private int prevX, prevY;
GuiMap(int xpos, int ypos){
this.currentX = xpos;
this.currentY = ypos;
}
public void updatePoint(int xpos, int ypos) {
prevX = currentX;
prevY = currentY;
currentX = xpos;
currentY = ypos;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.drawLine(prevX, prevY, currentX, currentY);
}
}
public class GuiMapFrame extends JFrame {
static GuiMap guiPanel;
static JFrame frame;
public static void main(String[] args) throws InterruptedException{
frame = new JFrame("SuperGui");
guiPanel = new GuiMap();
frame.setContentPane(guiPanel);
frame.setSize(600, 480);
frame.setLocation(100,100);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiPanel.updatePoint(20, 80);
guiPanel.updatePoint(130, 50);
guiPanel.updatePoint(60, 175);
guiPanel.repaint();
}
}
其实你的问题就是camickr说的那样。每次重新绘制时,它都会清除之前绘制的内容。所以你需要创建多个对象并在 paint 组件中渲染每个对象
My problem is only the last line is visible. …
您需要做以下两件事之一:
- 将每一行绘制到 BufferedImage 并绘制 BufferedImage
- 保留要绘制的所有线条的列表,然后遍历此列表
有关这两种方法的工作示例,请参阅 Custom Painting Approaches。