jscrollpane has trailing image - java

I have a JPanel inside the JScrollPane and it does that whenever I try to scroll. Please help! How do I fix this?
EDIT
JScrollPane pane;
....
pane = new JScrollPane(GC.createGraph());
pane.setPreferredSize(new Dimension(480,480*2/3));

Placing as an answer for others to see. If you don't call the super.paintComponent, you'll get those artifacts. e.g.,
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;
public class ScrollPaneArtifacts extends JPanel {
private static final int SPA_WIDTH = 600;
private static final int SPA_HEIGHT = SPA_WIDTH;
#Override
protected void paintComponent(Graphics g) {
//super.paintComponent(g);
g.setColor(Color.red);
g.drawLine(0, 0, getWidth(), getHeight());
g.drawLine(getWidth(), 0, 0, getHeight());
}
#Override
public Dimension getPreferredSize() {
return new Dimension(SPA_WIDTH, SPA_HEIGHT);
}
private static void createAndShowUI() {
JScrollPane scrollpane = new JScrollPane(new ScrollPaneArtifacts());
scrollpane.getViewport().setPreferredSize(new Dimension(400, 400));
JFrame frame = new JFrame("ScrollPaneArtifacts");
frame.getContentPane().add(scrollpane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}

You do not need to call super.paintComponent you can simply clear the area to ensure no artifacts are left on the panel from the previous render (which calling super.paintComponent will do).
#Override
protected void paintComponent(Graphics g) {
g.clearRect(0,0,getWidth(),getHeight());
g.setColor(Color.red);
g.drawLine(0, 0, getWidth(), getHeight());
g.drawLine(getWidth(), 0, 0, getHeight());
}
Try this in Hovercrafts code if you like.

Related

How do I set the size of a JPanel? [duplicate]

I have a JPanel in my java code and I want to set its size, I have used JPanel.setSize(500,500); and JPanel.setSize(new Dimension(500,500)); but both are not working. Please tell how I can set the size of JPanel?
Most Swing layout managers respect a component's preferredSize and not its size. You could call setPreferredSize(new Dimension(500, 500)), but this can be overridden later in your code, and can lead to an inflexible GUI. Better to override the JPanel's getPreferredSize() method and return a calculated Dimension that works best in all situations.
For example:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.*;
public class PrefSizePanel extends JPanel {
private static final int PREF_W = 500;
private static final int PREF_H = PREF_W;
public Dimension getPreferredSize() {
// update as per Marco:
if (super.isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
// just for fun
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(new GradientPaint(0, 0, Color.red, 20, 20, Color.blue, true));
g2.fillOval(0, 0, 2 * getWidth(), 2 * getHeight());
}
private static void createAndShowGUI() {
PrefSizePanel paintEg = new PrefSizePanel();
JFrame frame = new JFrame("PrefSizePanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(paintEg);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Which displays as

Painted component bigger than frame

I am using the same numbers to set the size of my frame as I am to paint the rectangle, yet the graphics are larger than my JFrame. Why is this?
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test {
public static void main(String[] arguments) {
Test test = new Test();
JFrame frame = new JFrame();
DrawPane contentPane = test.new DrawPane();
frame.setContentPane(contentPane);
frame.setSize(300, 400);
frame.setVisible(true);
}
private class DrawPane extends JPanel {
#Override
protected void paintComponent(Graphics g) {
g.setColor(Color.YELLOW);
g.fillRect(0, 0, 300, 400);
}
}
}
It's because of border. And it's a good example why you shouldn't explicitly determine size for your JFrame. Instead calling setSize override getPreferredSize method from your JPanel:
private class DrawPane extends JPanel {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.YELLOW);
g.fillRect(0, 0, 300, 400);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 400);
}
}
Then call pack for your JFrame instead setSize and your JFrame will adjust it's size according to its content.

jFrame doesn't show the image

So guys I want to use the code I have to set the background of the jFrame without adding anything from another class (like using this code in a jPanel then adding that panel to a jFrame). I wanna do everything in this class. I really have no idea what to do, so I tried this out but this code is not displaying the image!
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class panel extends JFrame{
Image img;
public void paintComponent(Graphics g){
super.paintComponents(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
g.dispose();
}
public panel(){
img=new ImageIcon(getClass().getResource("bg_login.jpg")).getImage();
setExtendedState(JFrame.MAXIMIZED_BOTH);
pack();
setVisible(true);
}
public static void main(String[] args){
new panel();
}
}
there is no paintcomponent() method for jframe as it's not a jcomponent but a container .you can make a panel and overide paintcomponent method then setcontentpane of jframe to that panel
example
public class panel extends JPanel {
Image img;
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
}
// g.dispose();
}
public panel() {
img=new ImageIcon(getClass().getResource("bg_login.jpg")).getImage();
}
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
panel panel = new panel();
jFrame.setContentPane(panel);
jFrame.pack();
jFrame.setVisible(true);
}
}

how to set the size of JPanel in java

I have a JPanel in my java code and I want to set its size, I have used JPanel.setSize(500,500); and JPanel.setSize(new Dimension(500,500)); but both are not working. Please tell how I can set the size of JPanel?
Most Swing layout managers respect a component's preferredSize and not its size. You could call setPreferredSize(new Dimension(500, 500)), but this can be overridden later in your code, and can lead to an inflexible GUI. Better to override the JPanel's getPreferredSize() method and return a calculated Dimension that works best in all situations.
For example:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.*;
public class PrefSizePanel extends JPanel {
private static final int PREF_W = 500;
private static final int PREF_H = PREF_W;
public Dimension getPreferredSize() {
// update as per Marco:
if (super.isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
// just for fun
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(new GradientPaint(0, 0, Color.red, 20, 20, Color.blue, true));
g2.fillOval(0, 0, 2 * getWidth(), 2 * getHeight());
}
private static void createAndShowGUI() {
PrefSizePanel paintEg = new PrefSizePanel();
JFrame frame = new JFrame("PrefSizePanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(paintEg);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Which displays as

Why paint border is not being called?

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");
}
}
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.

Categories

Resources