JPanel组件将不会显示

时间:2016-10-03 16:31:41

标签: java swing custom-painting

我尝试将一些组件添加到JPanel。调用public class YourConsumer<T> extends FlinkKafkaConsumerBase<T> { public static final long DEFAULT_POLL_TIMEOUT = 100L; private final long pollTimeout; public FlinkKafkaConsumer09(String topic, DeserializationSchema<T> valueDeserializer, Properties props) { this(Collections.singletonList(topic), valueDeserializer, props); } public FlinkKafkaConsumer09(String topic, KeyedDeserializationSchema<T> deserializer, Properties props) { this(Collections.singletonList(topic), deserializer, props); } public FlinkKafkaConsumer09(List<String> topics, DeserializationSchema<T> deserializer, Properties props) { this(topics, new KeyedDeserializationSchemaWrapper<>(deserializer), props); } public FlinkKafkaConsumer09(List<String> topics, KeyedDeserializationSchema<T> deserializer, Properties props) { super(topics, deserializer); this.properties = checkNotNull(props, "props"); setDeserializer(this.properties); // configure the polling timeout try { if (properties.containsKey(KEY_POLL_TIMEOUT)) { this.pollTimeout = Long.parseLong(properties.getProperty(KEY_POLL_TIMEOUT)); } else { this.pollTimeout = DEFAULT_POLL_TIMEOUT; } } catch (Exception e) { throw new IllegalArgumentException("Cannot parse poll timeout for '" + KEY_POLL_TIMEOUT + '\'', e); } } @Override protected AbstractFetcher<T, ?> createFetcher( SourceContext<T> sourceContext, List<KafkaTopicPartition> thisSubtaskPartitions, SerializedValue<AssignerWithPeriodicWatermarks<T>> watermarksPeriodic, SerializedValue<AssignerWithPunctuatedWatermarks<T>> watermarksPunctuated, StreamingRuntimeContext runtimeContext) throws Exception { boolean useMetrics = !Boolean.valueOf(properties.getProperty(KEY_DISABLE_METRICS, "false")); return new Kafka09Fetcher<>(sourceContext, thisSubtaskPartitions, watermarksPeriodic, watermarksPunctuated, runtimeContext, deserializer, properties, pollTimeout, useMetrics); } @Override protected List<KafkaTopicPartition> getKafkaPartitions(List<String> topics) { // read the partitions that belong to the listed topics final List<KafkaTopicPartition> partitions = new ArrayList<>(); int partition=Integer.valueOf(this.properties.get("partitions")); try (KafkaConsumer<byte[], byte[]> consumer = new KafkaConsumer<>(this.properties)) { for (final String topic: topics) { // get partitions for each topic List<PartitionInfo> partitionsForTopic = consumer.partitionsFor(topic); // for non existing topics, the list might be null. if (partitionsForTopic != null) { partitions.addAll(convertToFlinkKafkaTopicPartition(partitionsForTopic),partition); } } } if (partitions.isEmpty()) { throw new RuntimeException("Unable to retrieve any partitions for the requested topics " + topics); } // we now have a list of partitions which is the same for all parallel consumer instances. LOG.info("Got {} partitions from these topics: {}", partitions.size(), topics); if (LOG.isInfoEnabled()) { logPartitionInfo(LOG, partitions); } return partitions; } private static List<KafkaTopicPartition> convertToFlinkKafkaTopicPartition(List<PartitionInfo> partitions,int partition) { checkNotNull(partitions); List<KafkaTopicPartition> ret = new ArrayList<>(partitions.size()); //for (PartitionInfo pi : partitions) { ret.add(new KafkaTopicPartition(partitions.get(partition).topic(), partitions.get(partition).partition())); // } return ret; } private static void setDeserializer(Properties props) { final String deSerName = ByteArrayDeserializer.class.getCanonicalName(); Object keyDeSer = props.get(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG); Object valDeSer = props.get(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG); if (keyDeSer != null && !keyDeSer.equals(deSerName)) { LOG.warn("Ignoring configured key DeSerializer ({})", ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG); } if (valDeSer != null && !valDeSer.equals(deSerName)) { LOG.warn("Ignoring configured value DeSerializer ({})", ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG); } props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, deSerName); props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, deSerName); } } ,但不显示组件。 paintComponent()无效。

结构如下所示:

revalidate()

我认为这是相关的代码。像public class AbstractView extends JPanel implements ViewInterface { } public class GraphView extends AbstractView { public void doSomeStuff(){ this.add(new ElementView()); this.revalidate(); } } public class ElementView extends AbstractView { @Override public void paintComponent(Graphics g) { // this method is called but has no effect super.paintComponent(g); g.fillRect(0,0,100,100); this.repaint(); } } 甚至getPreferredSize()getX()getY()getWidth()这样的方法已经实施,似乎可以像预期的那样工作。我搜索谷歌几个小时,但似乎没有任何帮助。我在正确的位置搜索错误吗?

编辑:

以下是一些代码:

1:

getHeight()

2:

public class AbstractView extends JPanel implements ViewInterface {
@Override
public void update() {}
@Override
public boolean containsPoint(Point p) {
    return false;
}
@Override
public ApplicationView topContainer() {
    return null;
}
@Override
public Dimension getPreferredSize() {
    return new Dimension(this.getBounds().getSize());
}
@Override Rectangle getBounds(){
    return Rectangle(0,0,0,0);
}
@Override
public int getX() {
   return this.getBounds().x;
}
@Override
public int getY() {
    return this.getBounds().y;
}
@Override
public int getWidth() {
    return this.getBounds().width;
}
@Override
public int getHeight() {
    return this.getBounds().height;
}

}

3:

class BodyView extends AbstractView {

Body body;

BodyView(Body b, AbstractView c){
    this.body = b;
    this.setLayout(null);
}

private int radius(){
    return (int) body.radius();
}

@Override
public void paintComponent(Graphics g) {
    // called but without effect
    super.paintComponent(g);
    g.setColor(this.color);
    int x = (int) body.position.at(0);
    int y = (int) body.position.at(1);
    int radius = radius();
    g.fillOval(x-radius, y-radius, 2*radius, 2*radius);
}

@Override
public Rectangle getBounds(){
    int radius = this.radius();
    return new Rectangle(getX() - radius - 2, getY() - radius - 2, 2*radius + 2, 2*radius + 2);
}

@Override
public int getX() {
    return (int) body.position.at(0);
}
@Override
public int getY() {
    return (int) body.position.at(1);
}
}

如果这还不足以确定问题,我想引用this Github

0 个答案:

没有答案