Domanda

I am working on learning how to create custom components. I would like to be able to include all of the methods that it calls to start with and to be able to change even the border method. Below my code does not repaint the paintBorder(...) method

    public void paintBorder(Component t, Graphics g, int x, int y, int h, int w) {
         super.paintBorder(g);
         g.setColor(Color.YELLOW);
         g.fillOval(100, 100, 50, 50);
         System.out.println("PaintBorder");
    }

Why would this not be being painted. The code in the paintComponent(...) does work and paint the circle but what if I want to set the border to something different or even if I just want to see a message go to the console with a println(...).

Paint Call:

    There are three methods called

          paintComponent()
          paintBorder()
          paintChildren()

How can I get my paintBorder() to be called? I would imagine that if I make a instance of this in another class than I should be able to call repaint() which calls update which intern schedules a call to paint which calls the three methods listed above (paintComponent, paintBorder, paintChildren)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class testBall {
    public static void main(String[] args) {
        new testBall();
    }

    public testBall() {
           JPanel testPane = new JPanel();
           testPane.setBackground(Color.green);
           testPane.setLayout(new BorderLayout());
           testPane.add(new Ball(30,30,10));
           JFrame frame = new JFrame("Testing");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.setLayout(new BorderLayout());
           frame.add(testPane);
           frame.pack();
           frame.setSize(300, 200); 
           frame.setLocationRelativeTo(null);
           frame.setVisible(true);
    }
}

class MyBall extends JComponent{
    private static final long serialVersionUID = 1L;
    public static Color colorBall = Color.red;

    public MyBall() { 
        super();
        System.out.println("MyBall (0)");
    }

    public MyBall(int x, int y, int diameter){
        super();
        this.setLocation(x, y);
        this.setSize(diameter, diameter);
        System.out.println("MyBall (1)");
    }

    public void paintBorder(Graphics g) {
         super.paintBorder(g);
         g.setColor(Color.YELLOW);
         g.fillOval(100, 100, 50, 50);
         System.out.println("PaintBorder");
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(colorBall);
        g.fillOval(0, 0, 50, 50);
        System.out.println("paintComponent");
    }

    public void paint(Graphics g) {
        super.paint(g);
        paintComponent(g);
        paintBorder(this, g,10,10,10,10);
        paintChildren(g);
        System.out.println("Paint");
    }
}

Working Code: I was creating an instance of a different ball class which was something that I did not see. If a person is going to over write the paintBorder(...) method in a class the extends JComponent(...) how should the border be painted? Does anyone have any good links for such a task?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class testBall 
{
    public static void main(String[] args) 
    {
        new testBall();
    }

    public testBall() 
    {
           JPanel testPane = new JPanel();
           testPane.setBackground(Color.green);
           testPane.setLayout(new BorderLayout());
           testPane.add(new MyBall(30,30,10));



           JFrame frame = new JFrame("Testing");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.setLayout(new BorderLayout());
           frame.add(testPane);
           frame.pack();
           frame.setSize(300, 200); 
           frame.setLocationRelativeTo(null);
           frame.setVisible(true);
    }
}

class MyBall extends JComponent
{
    private static final long serialVersionUID = 1L;
    private Color colorBall = Color.red;

    public void setColorBall(Color c)
    {
        this.colorBall = c;
    }

    public MyBall() 
    { 
        super();
        System.out.println("MyBall (0)");
    }

    public MyBall(int x, int y, int diameter)
    {
        super();
        this.setLocation(x, y);
        this.setSize(diameter, diameter);
        System.out.println("MyBall (1)");
    }

    public void paintBorder(Graphics g) 
    {
         super.paintBorder(g);
         g.setColor(Color.YELLOW);
         g.fillOval(100, 100, 50, 50);
         System.out.println("PaintBorder");
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(colorBall);
        g.fillOval(0, 0, 50, 50);
        System.out.println("paintComponent");
    }

    public void paint(Graphics g) 
    {
        super.paint(g);
        paintComponent(g);
        paintBorder(g);
        paintChildren(g);
        System.out.println("Paint");
    }
}
È stato utile?

Soluzione

There is no such method in JComponent

public void paintBorder(Component t, Graphics g, int x, int y, int h, int w)

It's method of border so it's never invoked in JComponent. Override

protected void paintBorder(Graphics g)

Instead and add your code there.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top