Java Applet Changed With Paint

user3587186

I have an applet that's only purpose is to create a box and each time it's painted it changes color. Right now it is not changing color at all, it simply creates a random background color to start and sticks with it whenever painted but I need it to change. Any help on what I'm doing wrong would be appreciated.

import java.applet.*;
import java.awt.*;
import java.util.*;

public class AppletSubClass2 extends Applet {
public void init() {
    System.err.println("Hello from AnAppletSubClass.init");
    setBackground(color);
}
public void paint(Graphics g) {
    System.err.println("Hello from .paint!This time the applet will change colors when painted");
    setBackground(new Color(randomNum1, randomNum2, randomNum3));
}
Random rand = new Random();
int randomNum1 = rand.nextInt(251);
int randomNum2 = rand.nextInt(251);
int randomNum3 = rand.nextInt(251);
Color color = new Color(randomNum1, randomNum2, randomNum3);
}
MadProgrammer

You've basically broken the paint chain, nothing is actually painting your background color...

You could do something like...

public void paint(Graphics g) {
    int randomNum1 = rand.nextInt(251);
    int randomNum2 = rand.nextInt(251);
    int randomNum3 = rand.nextInt(251);
    Color color = new Color(randomNum1, randomNum2, randomNum3);
    setBackground(color);
    super.paint(g);
}

But this will set up a infinite cycle of repaint requests which will eventually consume your CPU cycles and make you PC unusable (not to mention flicker like crazy)...

A better solution might be to override the getBackgroundColor method...

@Override
public Color getBackground() {
    int randomNum1 = rand.nextInt(251);
    int randomNum2 = rand.nextInt(251);
    int randomNum3 = rand.nextInt(251);
    Color color = new Color(randomNum1, randomNum2, randomNum3);
    return color;
}

This will mean that each time this method is called, it will generate a random color. You can then use, some other process, to force the applet to repaint...

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Can the size of the workspace switcher applet be changed?

분류에서Dev

Java Clock Applet

분류에서Dev

Blank java applet

분류에서Dev

Using Java.applet.Applet in Eclipse

분류에서Dev

paint (graphics g) Java 무시

분류에서Dev

NoClassDefFoundError : java.applet.Applet .... 피카소

분류에서Dev

NoClassDefFoundError : java.applet.Applet .... 피카소

분류에서Dev

Why Java Applet isn't displaying?

분류에서Dev

Alternative Java applet network drive access

분류에서Dev

Why has changed both sides of equation in java?

분류에서Dev

Is there a way to check if java object changed after submit

분류에서Dev

paint () 함수를 실행하지 않는 Java AWT 창 구현

분류에서Dev

Java Applet Windows-MY keystore PrivateKey getEncoded is null

분류에서Dev

JAVA paint () 메소드가 작동하지 않는 이유는 무엇입니까?

분류에서Dev

Paint, Repaint 및 PaintComponent

분류에서Dev

How to paint permanently on JComponent

분류에서Dev

Collision detection system not working after porting from Java applet to Android app

분류에서Dev

다른 클래스에서 Java Applet 다시 그리기

분류에서Dev

PHP에 대한 Java Applet의 URLConnection이 효과가 없습니다.

분류에서Dev

Chrome에서 NPAPI로 실행되는 Java Applet의 대안

분류에서Dev

Javascript, Java Applet 메서드 호출시 TypeError 발생

분류에서Dev

Can Windows 7 Paint (aka Microsoft Paint) handle transparency?

분류에서Dev

Java anti-hacking. How protect .class file from being changed in my jar

분류에서Dev

OS X Applet에서 java.awt.Toolkit.getDefaultToolkit () 호출시 널 포인터 예외

분류에서Dev

java.policy 파일을 편집하지 않고 Java Applet을 실행할 수 있습니까?

분류에서Dev

Java Applet java.awt.Choice 항목 추가가 두 번째로 작동하지 않음

분류에서Dev

QStyledItemDelegate로 paint ()하는 방법

분류에서Dev

How can I paint a Widget on a Canvas in Flutter?

분류에서Dev

Paint images on PictureBox inside a UserControl WPF

Related 관련 기사

  1. 1

    Can the size of the workspace switcher applet be changed?

  2. 2

    Java Clock Applet

  3. 3

    Blank java applet

  4. 4

    Using Java.applet.Applet in Eclipse

  5. 5

    paint (graphics g) Java 무시

  6. 6

    NoClassDefFoundError : java.applet.Applet .... 피카소

  7. 7

    NoClassDefFoundError : java.applet.Applet .... 피카소

  8. 8

    Why Java Applet isn't displaying?

  9. 9

    Alternative Java applet network drive access

  10. 10

    Why has changed both sides of equation in java?

  11. 11

    Is there a way to check if java object changed after submit

  12. 12

    paint () 함수를 실행하지 않는 Java AWT 창 구현

  13. 13

    Java Applet Windows-MY keystore PrivateKey getEncoded is null

  14. 14

    JAVA paint () 메소드가 작동하지 않는 이유는 무엇입니까?

  15. 15

    Paint, Repaint 및 PaintComponent

  16. 16

    How to paint permanently on JComponent

  17. 17

    Collision detection system not working after porting from Java applet to Android app

  18. 18

    다른 클래스에서 Java Applet 다시 그리기

  19. 19

    PHP에 대한 Java Applet의 URLConnection이 효과가 없습니다.

  20. 20

    Chrome에서 NPAPI로 실행되는 Java Applet의 대안

  21. 21

    Javascript, Java Applet 메서드 호출시 TypeError 발생

  22. 22

    Can Windows 7 Paint (aka Microsoft Paint) handle transparency?

  23. 23

    Java anti-hacking. How protect .class file from being changed in my jar

  24. 24

    OS X Applet에서 java.awt.Toolkit.getDefaultToolkit () 호출시 널 포인터 예외

  25. 25

    java.policy 파일을 편집하지 않고 Java Applet을 실행할 수 있습니까?

  26. 26

    Java Applet java.awt.Choice 항목 추가가 두 번째로 작동하지 않음

  27. 27

    QStyledItemDelegate로 paint ()하는 방법

  28. 28

    How can I paint a Widget on a Canvas in Flutter?

  29. 29

    Paint images on PictureBox inside a UserControl WPF

뜨겁다태그

보관