简体   繁体   English

JApplet不显示绘画或绘制方法

[英]JApplet not showing paint or draw methods

I have a animation class which simply draws two circles and a line (no main method), in another class i have a main method which is passed the animation class as a object and should show the two circles that I have drawn but it doesn't show, it only shows the window none of the circles or the line that I have done, if i was to put the main method in my animation class it will work perfectly, this is a user error somehow but I'm not sure what or why. 我有一个动画类,它只画了两个圆和一条线(没有main方法),在另一个类中,我有一个main方法,该方法将animation类作为对象传递给了我,并且应该显示我画过的两个圆,但是它没有t show,它只显示窗口,没有任何圆或线,如果我将main方法放在我的动画类中,它将很好地工作,这是用户错误,但我不确定还是为什么。

The animation method in separate class. 动画方法在单独的类中。

import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
import java.awt.Graphics.*;
import javax.swing.JApplet;

public class Animation extends JApplet{

    public void init(){

}
public static void createAndShowGUI(JApplet j)
{
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.pack();
    frame.setVisible(true);
    frame.setSize(500,500);

    frame.getContentPane().add("Center", j);
}

public void paint(Graphics g){
    Graphics2D g2 = (Graphics2D) g;
    int x=50;
    int y=10;
    g2.setPaint(Color.black);

    g2.draw(new Line2D.Double(x,y,50,400));

    drawT(g2);
    drawH(g2);  
        //create a method that translates
    }

public void drawH(Graphics2D g2)
{
    int y=25;
    g2.setColor(Color.blue);
    drawCircle(y,g2);
}

public void drawT(Graphics2D g2){
    int y=100;
    g2.setColor(Color.green);
    drawCircle(y,g2);
}/*
public void raceLoop(){
    long startingTime = System.currentTimeMillis();
    long cumTime=startingTime;
    while(mve.hposition<70){
        long timePassed = System.currentTimeMillis()-cumTime;   
        cumTime += timePassed;
        //mve.update(timePassed);   
    }
}*/

public void drawCircle(int y, Graphics2D g2)
{
g2.fillOval(50,y,50,50);
}
}

The Main Method 主要方法

import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
import java.awt.Graphics.*;
import javax.swing.JApplet;

public class Race {

public static void main(String[] args) {
    JApplet applet = new JApplet();
    Animation animie = new Animation();
    animie.createAndShowGUI(applet);

}
}
  1. You don't need to (or want) extend from JApplet if all you're going to is use a JFrame . 如果您只想使用JFrame ,则不需要(或不想)从JApplet扩展。 You're better of (in any case), extending from JPanel (as this can then be added to a JApplet or JFrame ) 在任何情况下,您最好从JPanel扩展(因为可以将其添加到JAppletJFrame
  2. You've not added anything to the JFrame , so, in fact, you program is doing exactly what you told it to. 您没有向JFrame添加任何内容,因此,实际上,您的程序确实按照您的指示进行了操作。
  3. You should very rarely need to override the paint method of a top level container (like JApplet or JFrame ), instead, use JPanel and override it's paintComponent method. 您几乎不需要覆盖顶级容器(例如JAppletJFrame )的paint方法,而是使用JPanel并覆盖其paintComponent方法。
  4. You should always call super.paintXxx , the paint methods chain together to paint the various aspects of the application and it will end in tears if you break this chain. 您应该始终调用super.paintXxx ,paint方法链一起绘制应用程序的各个方面,如果您中断了此链,它将流泪。

You might like to have a read through 您可能需要通读

For some more background 有关更多背景

Looks like you're passing the wrong reference to the createAndShowGui(..) method. 似乎您将错误的引用传递给createAndShowGui(..)方法。

Try this on your Race class: 在您的Race类上尝试以下操作:

import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
import java.awt.Graphics.*;
import javax.swing.JApplet;

public class Race {

    public static void main(String[] args) {
        Animation animie = new Animation();
        animie.createAndShowGUI(animie);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM