[英]Java Repaint - JComponet needs to repaint the class when the repaint() is called from another class
我仍然試圖讓一個repaint()方法在一個單獨的類中工作,該類具有擴展JComponent的類。 我在這里放了幾個帖子,到目前為止我還沒能讓代碼工作。 我得到了一些好的建議。 我現在放在我的目標之下。
主要等級1:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class DDHGenericFrame extends JFrame {
private static final long serialVersionUID = 1L;
DDHGenericPanel d = new DDHGenericPanel(); /*User defined class that is above*/
public DDHGenericFrame() {
initUI();
}
public final void initUI() {
add(d);//Adds the panel to the JFrame
setSize(650,350);
setTitle("Lines");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
DDHGenericFrame ex = new DDHGenericFrame();
ex.setVisible(true);
}
}
第2類:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
class DDHGenericPanel extends JPanel {
private static final long serialVersionUID = 1L;
public JButton aButton1;
public JButton aButton2;
public TestPane tPane = new TestPane();
DDHGenericPanel(){
System.out.println("DDH Generic JPanel");
aButton1 = new JButton();
aButton1.setText("Button 1");
aButton1.addActionListener(new myButtonActionListener1());
add(aButton1);
aButton2 = new JButton();
aButton2.setText("Button 2");
aButton2.addActionListener(new myButtonActionListener2());
add(aButton2);
System.out.println("Before the setDraw!!!");
tPane.setDraw();
System.out.println("After the setDraw!!!");
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("paintComponent of DDHGenericPanel.java");
}
}
class myButtonActionListener1 implements ActionListener {
public TestPane tPane = new TestPane();
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("Button 1 -- Before the setDraw!!!");
tPane.setDraw();
System.out.println("Button 1 -- After the setDraw!!!");
}
}
class myButtonActionListener2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("Button1 clicked 2");
}
}
第3類:(我把這個嵌入到與上面類相同的文件中 - 當我有完成的代碼時它將是獨立的)
/**
* This class will draw a cricle with the repaint method
* @author DDH
*/
class TestPane extends JComponent {
private static final long serialVersionUID = 1L;
private static final int LINE_THICKNESS = 4;
private static final int LINE_GAP = 10;
private Color lineColor = Color.red;
/**
* This method will draw the circle with coordinated (0,0)
* @param none
* @return none
*/
public void setDraw() {
repaint();//This should call the paintComponent() that is below and paint a circe but it does not for some reason.
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int radius = 10;
BufferedImage buffer = new BufferedImage(radius, radius, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = buffer.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint (RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
Ellipse2D circle = new Ellipse2D.Float(0, 0, radius,radius);
Shape clip = g2d.getClip();
g2d.setClip(circle);
AffineTransform at = g2d.getTransform();
g2d.setTransform(AffineTransform.getRotateInstance(
Math.toRadians(45),
radius / 2, radius / 2));
int gap = LINE_GAP;
g2d.setColor(Color.WHITE);
g2d.fill(circle);
g2d.setColor(lineColor);
//g2d.setStroke(new BasicStroke(LINE_THICKNESS));
for (int index = 0; index < 10; index++) {
int x1 = index*gap-(LINE_THICKNESS/2);
int y1 = 0;
int x2 = index*gap+(LINE_THICKNESS/2);
int y2 = radius;
int width = x2 - x1;
int height = y2 - y1;
g2d.fillRect(x1, y1, width, height);
//g2d.drawLine(index * gap, 0, index * gap, getRadius());
}
g2d.setTransform(at);
g2d.setClip(clip);
g2d.dispose();
g.drawImage(buffer, 0, 0, this);
}
}
弗羅姆我所讀到的以及人們發布的內容應該有效。 有沒有辦法迫使它立即油漆。 重繪()有時會有一點延遲。 我想用它作為游戲的開始,我必須能夠創建一個圓圈的ArrayList,然后立即重新繪制它們。 目前,這只會在頂部(0,0)坐標中繪制一個圓圈。
Doug Deines Hauf
有沒有辦法迫使它立即油漆。
一旦GUI可見,它就會立即繪制。 你不需要做什么特別的事情。 不需要setDraw()方法。 顯示GUI時,將自動繪制所有組件。
System.out.println("Before the setDraw!!!");
tPane.setDraw();
System.out.println("After the setDraw!!!");
那段代碼什么也沒做。 GUI尚未可見,因此無需繪制任何內容。 除非您實際更改可見GUI上組件的屬性,否則沒有理由調用重繪。
public void setDraw() {
repaint();
}
沒有理由創建一個簡單地執行repaint()的方法,擺脫這個方法。 這不是我在你上一篇文章中建議的內容。 我說你創建了一個方法來改變一個會影響組件繪制結果的屬性。
我給你舉了一個例子,就像你使用setForeground()時一樣,該方法改變了要繪制的文本的顏色,因此當顏色改變時會自動調用repaint()。
擺脫塗料組件中所有復雜的繪畫代碼,然后嘗試做一個簡單的
graphics.drawString();
不要玩旋轉和剪輯(即使我有這些概念的問題,如果沒有正確完成,你可能得不到任何顏色),直到你得到一些基本的工作。 然后,一旦你開始工作,你就會做一些更復雜的事情,一步一步,直到你理解基礎知識。 在得到簡單的工作之前,不要編寫復雜的程序。
此外,我不知道你為什么試圖從緩沖圖像繪制。 只需使用傳遞給paintComponent()方法的Graphics對象進行繪制。 沒有必要使用BufferedImage,Swing已經是雙緩沖的,因此您只是使代碼復雜化。
你讀過自定義繪畫教程了嗎? 它包含一個工作示例。
編輯:
說完以上所有內容,你仍然有兩個基本問題:
即使這兩個修復程序也無法解決復雜繪畫的問題,但至少現在我可以使用簡單的束帶(...)來實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.