Repaint() nicht aufrufen von paint() in Java
Lassen Sie mich beginnen mit den Worten ich weiß, ich habe verletzt, einige grundlegende Java-Grundlagen in diesem chaotisch-code, aber ich bin verzweifelt versucht zu beenden ein Programm vom Dienstag für ein sozialwissenschaftliches experiment, und ich weiß nicht, Java, also ich bin im Grunde nur Fummelei durch für heute.
Mit Haftungsausschluss aus dem Weg, ich haben ein separates Programm zu arbeiten, wo ein Kreis bewegt sich auf dem Bildschirm und der Benutzer muss klicken Sie auf. Es funktioniert, wenn Ihr in einem eigenen class-Datei, aber wenn ich den code zu meinem main-Programm, es funktioniert nicht mehr. Ich weiß nicht einmal wirklich verstehen, warum repaint() ruft meine paint () - Funktion — soweit ich bin besorgt, es ist Magie, aber ich habe bemerkt, dass repaint() ruft paint() in meinem test-Programm, nicht aber in der komplizierter eigentlichen Programm, und ich nehme an, dass ist der Grund, warum der Kreis ist nicht mehr die Malerei auf meinem Programm.
Gesamte code ist unten:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;
public class Reflexology1 extends JFrame{
private static final long serialVersionUID = -1295261024563143679L;
private Ellipse2D ball = new Ellipse2D.Double(0, 0, 25, 25);
private Timer moveBallTimer;
int _ballXpos, _ballYpos;
JButton button1, button2;
JButton movingButton;
JTextArea textArea1;
int buttonAClicked, buttonDClicked;
private long _openTime = 0;
private long _closeTime = 0;
JPanel thePanel = new JPanel();
JPanel thePlacebo = new JPanel();
final JFrame frame = new JFrame("Reflexology");
final JFrame frame2 = new JFrame("The Test");
JLabel label1 = new JLabel("Press X and then click the moving dot as fast as you can.");
public static void main(String[] args){
new Reflexology1();
}
public Reflexology1(){
frame.setSize(600, 475);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Reflexology 1.0");
frame.setResizable(false);
frame2.setSize(600, 475);
frame2.setLocationRelativeTo(null);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setTitle("Reflexology 1.0");
frame2.setResizable(false);
button1 = new JButton("Accept");
button2 = new JButton("Decline");
//movingButton = new JButton("Click Me");
ListenForAcceptButton lForAButton = new ListenForAcceptButton();
ListenForDeclineButton lForDButton = new ListenForDeclineButton();
button1.addActionListener(lForAButton);
button2.addActionListener(lForDButton);
//movingButton.addActionListener(lForMButton);
JTextArea textArea1 = new JTextArea(24, 50);
textArea1.setText("Tracking Events\n");
textArea1.setLineWrap(true);
textArea1.setWrapStyleWord(true);
textArea1.setSize(15, 50);
textArea1.setEditable(false);
FileReader reader = null;
try {
reader = new FileReader("EULA.txt");
textArea1.read(reader, "EULA.txt");
} catch (IOException exception) {
System.err.println("Problem loading file");
exception.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException exception) {
System.err.println("Error closing reader");
exception.printStackTrace();
}
}
}
JScrollPane scrollBar1 = new JScrollPane(textArea1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
AdjustmentListener listener = new MyAdjustmentListener();
thePanel.add(scrollBar1);
thePanel.add(button1);
thePanel.add(button2);
frame.add(thePanel);
ListenForMouse lForMouse = new ListenForMouse();
thePlacebo.addMouseListener(lForMouse);
thePlacebo.add(label1);
frame2.add(thePlacebo);
ListenForWindow lForWindow = new ListenForWindow();
frame.addWindowListener(lForWindow);
frame2.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e){
if(e.getKeyChar() == 'X' || e.getKeyChar() == 'x') {moveBallTimer.start();}
}
});
frame.setVisible(true);
moveBallTimer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveBall();
System.out.println("Timer started!");
repaint();
}
});
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(frame2.isVisible()){ moveBallTimer.start(); }
}
});
}
private class ListenForAcceptButton implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getSource() == button1){
Calendar ClCDateTime = Calendar.getInstance();
System.out.println(ClCDateTime.getTimeInMillis() - _openTime);
_closeTime = ClCDateTime.getTimeInMillis() - _openTime;
//frame.getContentPane().remove(thePanel);
//thePlacebo.addKeyListener(lForKeys);
//frame.getContentPane().add(thePlacebo);
//frame.repaint();
//moveBallTimer.start();
frame.setVisible(false);
frame2.setVisible(true);
frame2.revalidate();
frame2.repaint();
}
}
}
private class ListenForDeclineButton implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getSource() == button2){
JOptionPane.showMessageDialog(Reflexology1.this, "You've declined the license agreement. DO NOT RESTART the program. Please go inform a researcher that you have declined the agreement.", "WARNING", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
}
private class ListenForWindow implements WindowListener{
public void windowActivated(WindowEvent e) {
//textArea1.append("Window is active");
}
//if this.dispose() is called, this is called:
public void windowClosed(WindowEvent arg0) {
}
//When a window is closed from a menu, this is called:
public void windowClosing(WindowEvent arg0) {
}
//Called when the window is no longer the active window:
public void windowDeactivated(WindowEvent arg0) {
//textArea1.append("Window is NOT active");
}
//Window gone from minimized to normal state
public void windowDeiconified(WindowEvent arg0) {
//textArea1.append("Window is in normal state");
}
//Window has been minimized
public void windowIconified(WindowEvent arg0) {
//textArea1.append("Window is minimized");
}
//Called when the Window is originally created
public void windowOpened(WindowEvent arg0) {
//textArea1.append("Let there be Window!");
Calendar OlCDateTime = Calendar.getInstance();
_openTime = OlCDateTime.getTimeInMillis();
//System.out.println(_openTime);
}
}
private class MyAdjustmentListener implements AdjustmentListener {
public void adjustmentValueChanged(AdjustmentEvent arg0) {
AdjustmentEvent scrollBar1;
//System.out.println(scrollBar1.getValue()));
}
}
public void paint(Graphics g) {
//super.paint(g);
frame2.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fill(ball);
System.out.println("Calling fill()");
}
protected void moveBall() {
//System.out.println("I'm in the moveBall() function!");
int width = getWidth();
int height = getHeight();
int min, max, randomX, randomY;
min =200;
max = -200;
randomX = min + (int)(Math.random() * ((max - min)+1));
randomY = min + (int)(Math.random() * ((max - min)+1));
//System.out.println(randomX + ", " + randomY);
Rectangle ballBounds = ball.getBounds();
//System.out.println(ballBounds.x + ", " + ballBounds.y);
if (ballBounds.x + randomX < 0) {
randomX = 200;
} else if (ballBounds.x + ballBounds.width + randomX > width) {
randomX = -200;
}
if (ballBounds.y + randomY < 0) {
randomY = 200;
} else if (ballBounds.y + ballBounds.height + randomY > height) {
randomY = -200;
}
ballBounds.x += randomX;
ballBounds.y += randomY;
_ballXpos = ballBounds.x;
_ballYpos = ballBounds.y;
ball.setFrame(ballBounds);
}
public void start() {
moveBallTimer.start();
}
public void stop() {
moveBallTimer.stop();
}
private class ListenForMouse implements MouseListener{
//Called when the mouse is clicked
public void mouseClicked(MouseEvent e) {
//System.out.println("Mouse Panel pos: " + e.getX() + " " + e.getY() + "\n");
if (e.getX() >=_ballXpos && e.getX() <= _ballXpos + 25 && e.getY() <=_ballYpos && e.getY() >= _ballYpos - 25 ) {
System.out.println("TRUE");
}
System.out.println("{e.getX(): " + e.getX() + " /" + "_ballXpos: " + _ballXpos + " | " + "{e.getY(): " + e.getY() + " /" + "_ballYpos: " + _ballYpos);
}
public void mouseEntered(MouseEvent arg0) {
//TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
//TODO Auto-generated method stub
}
public void mousePressed(MouseEvent arg0) {
//TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent arg0) {
//TODO Auto-generated method stub
}
}
// System.out.println("e.getX(): " + e.getX() + " /" + "_ballXpos: " + _ballXpos);
//Mouse over
public void mouseEntered(MouseEvent arg0) {
//TODO Auto-generated method stub
}
//Mouse left the mouseover area:
public void mouseExited(MouseEvent arg0) {
//TODO Auto-generated method stub
}
public void mousePressed(MouseEvent arg0) {
//TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent arg0) {
//TODO Auto-generated method stub
}
}
Könnte mir jemand sagen was ich tun muss, um repaint() aufrufen, um die paint () - Methode im obigen Programm? Ich gehe davon aus, das mehrere Bilder, die das problem verursacht, aber das ist nur eine Vermutung. Danke.
super.paint
. Es gibt eine Menge "Magie" geht es, das muss erlaubt sein zu laufen. Having said, die, es wird nicht fix Ihre unmittelbare problem, aber es wird verhindern, dass künftige Probleme 😉Dank für, die. Ich hatte, kommentierte es aus, weil ich dachte, es war versuchen zu ziehen, auf meinem ersten Bild.
InformationsquelleAutor allocate | 2012-10-28
Du musst angemeldet sein, um einen Kommentar abzugeben.
Sollten Sie nicht die Farbe direkt auf
JFrame
.JPanel
oder Erweiterung vonJComponent
verwendet werden soll. Für die Malerei überschreibenpaintComponent()
eher alspaint()
. Vergessen Sie nicht, rufensuper.paintComponent(g);
.Beachten Sie auch, dass
repaint()
nur plant component update, es löst keine unmittelbarenpaint()
.Weitere details siehe Performing Custom Painting und Malen in AWT und Swing.
Beispielsweise können Sie die folgenden änderungen.
TestPanel
wird paint ball.Dann update
thePlacebo
panel:Hinzufügen repaint in
moveBall()
:Hier ist ein Beispiel, basierend auf den original-code:
thePlacebo.repaint();
heißt inmoveBall
Ihre Funktion nicht aufgerufen. Es heißt, wenn das erste Bild geladen, aber nie wieder danach :\sind Sie sicher, dass
paintComponent
ist nicht genannt? Es könnte ein mix-up mit den Koordinaten-Berechnung inmoveBall
. Sie haben versucht, eine Probe von meinem letzten edit?Ja, das Beispiel funktioniert. Und ich habe tatsächlich hatte mein code funktioniert, wenn es nicht integriert in diese große Sauerei, aber die Probleme scheinen zu entstehen, wenn die Arbeit mit mehreren JFrames — es scheint mir, dass der Fokus irgendwie immer auf dem Haupt-JFrame (und leider brauche ich diese arbeiten auf der einen zeige ich, nachdem der Benutzer eine Taste drückt). Ich bin sicher, dass
paintComponent
wird nicht aufgerufen, wenn die zweite JFrame sichtbar ist, obwohl — ich habe println sicher zu stellen.die Koordinaten in
moveBall
fallen aus dem Bildschirm. Hier ist eine aktualisierte version mit minimale änderungen, um den ball auf dem Bildschirm erscheinenoh, Sie sind meine neuen Lieblings-person. Vielen Dank für die Hilfe.
InformationsquelleAutor tenorsax