[英]repaint() not calling PaintComponent to use Graphics2D
我現在花了幾天時間試圖讓Graphics2D類在我的代碼中工作。 我以這樣的方式構造它:注冊了click事件時,完成了對重新繪制的調用,但是,僅當它到達調用repaint()的階段時,才產生nullpointer異常。
調試時,它都按預期方式工作,而不是從paintComponent方法中調用,但是,當嘗試使用paintComponent和repaint()正確調用代碼以允許Graphics2D類顯示到每個點的線時,它都無法正常工作。
我已經將我的代碼部分包括在內,但我上班時遇到了困難。 任何幫助將不勝感激。 先感謝您。
下面是包含我的mouseListener的GUI類。
public class GUI extends JPanel implements MouseListener {
private JLabel label;
public BufferedImage getImg() {
return img;
}
public void mouseClicked(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
label = new JLabel();
//set point equal to the location of the mouse click on the image label
Point b = e.getPoint();
//place we are going to print the dots
segmentation.x = b.x; //gets the x coordinate
segmentation.y = b.y; //gets the y coordinate
System.out.println("x = " + segmentation.x);
System.out.println("y = " + segmentation.y);
//set the global img in the segmentation class equal to that of the one in the current tab
segmentation.setImg(tabbedPane.getSelectedIndex(), getImg());
segmentation.paintUpdate();
label = segmentation.getLabel();
//if i run this line of code the existing label I already have with simply vanish because of the paintComponent method not being called upon properly.
//tabbedPane.setComponentAt(tabbedPane.getSelectedIndex(), label);
}
這是Segmentation類,其中包含我無法正確調用的paintComponent方法。
public class Segmentation extends JLabel {
public int[] xpoints = new int[50];
public int[] ypoints = new int[50];
private int npoints = 0;
public int x;
public int y;
GeneralPath polyline;
Graphics2D g2;
JLabel label;
BufferedImage img;
ImageIcon icon;
public void paintUpdate() {
repaint();
}
public void setImg(int tabNum, BufferedImage img) {
this.img = img;
}
public GeneralPath createPath() {
// if (npoints > 0) {
polyline.moveTo(xpoints[0], ypoints[0]);
for(int i = 1; i < xpoints.length; i++) {
//add the position of the point to the respective x and y arrays
polyline.lineTo(xpoints[i], ypoints[i]);
}
// }
return polyline;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("we're in the paint component method");
//set up for the new jlabel
label = new JLabel();
label.setIcon(new javax.swing.ImageIcon(img));
label.setHorizontalAlignment(JLabel.LEFT);
label.setVerticalAlignment(JLabel.TOP);
//add the position of the point to the respective x and y arrays
xpoints[npoints] = x;
ypoints[npoints] = y;
if (npoints == 0) {
JOptionPane.showMessageDialog(null, "Your first point has been added successfully");
}
else {
JOptionPane.showMessageDialog(null, "Your " + npoints + " rd/th" + " point has been added successfully");
}
polyline = createPath();
// Draws the buffered image to the screen.
g2.drawImage(img, 0, 0, this);
g2.draw(polyline);
npoints++;
}
您的Segmentation類有兩個JLabel,一個是該類本身的對象,它具有paintComponent重寫,並且可能從未使用過:
public class Segmentation extends JLabel {
另一個JLabel稱為label,它由類中的composition持有,該類實際上沒有使用方法重寫,並且似乎在“遮蓋”該類的實例:
JLabel label
如果要調用paintComponent,則需要使用實際上覆蓋該方法的label對象。
同樣,您似乎在paintComponent(...)
方法(??)中創建組件。 永遠不要這樣做,也不要在此方法中包含程序邏輯。
編輯:
要繪制圖像,通常會覆蓋paintComponent並使用其Graphics / Graphics2D對象進行繪制。 例如:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.*;
public class DrawOnLabel extends JLabel {
public static final String GIRAFFE_IMG_URL = "http://upload.wikimedia.org/wikipedia/commons/thumb" +
"/9/9e/Giraffe_Mikumi_National_Park.jpg/474px-Giraffe_Mikumi_National_Park.jpg";
private static final Color LINES_COLOR = Color.red;
private static final Color CURRENT_LINE_COLOR = new Color(255, 200, 200);
private List<Line2D> lineList = new ArrayList<Line2D>();
private Line2D currentLine = null;
public DrawOnLabel() throws IOException {
URL giraffeUrl = new URL(GIRAFFE_IMG_URL);
BufferedImage img = ImageIO.read(giraffeUrl);
ImageIcon icon = new ImageIcon(img);
setIcon(icon);
MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
addMouseListener(myMouseAdapter);
addMouseMotionListener(myMouseAdapter);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(LINES_COLOR);
for (Line2D line : lineList) {
g2.draw(line);
}
if (currentLine != null) {
g2.setColor(CURRENT_LINE_COLOR);
g2.draw(currentLine);
}
}
private class MyMouseAdapter extends MouseAdapter {
Point p1 = null;
@Override
public void mousePressed(MouseEvent e) {
p1 = e.getPoint();
}
@Override
public void mouseReleased(MouseEvent e) {
if (currentLine != null) {
currentLine = new Line2D.Double(p1, e.getPoint());
lineList.add(currentLine);
currentLine = null;
p1 = null;
repaint();
}
}
@Override
public void mouseDragged(MouseEvent e) {
if (p1 != null) {
currentLine = new Line2D.Double(p1, e.getPoint());
repaint();
}
}
}
private static void createAndShowGui() {
DrawOnLabel mainPanel = null;
try {
mainPanel = new DrawOnLabel();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
JFrame frame = new JFrame("DrawOnLabel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.setResizable(false);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.