I found similar problems on the internet but the solutions provided didn't work.
I want to clear the JPanel. To do that I call repaint()(from the clear() method) with a flag set to false to avoid calling my drawing method (drawLines()). Drawn lines are still on the Panel.
I tried to repaint same lines again with the background color. This also didn't work.
public class WektPanel extends JPanel{
boolean check = false;
Color c = Color.BLUE;
boolean oval = false;
public WektPanel() {
setBackground(c);
setVisible(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(check)
drawLines(3, g);
}
void clear(){
check=false;
repaint();
}
void draw(){
check=true;
repaint();
}
void drawLines(int stroke, Graphics g){
g.drawLine(0,0,getWidth(),getHeight());
g.drawLine(0,getHeight(),getWidth(),0);
for(int i=1; i<stroke;i++){
g.drawLine(0+i,0,getWidth(),getHeight()-i);
g.drawLine(0,0+i,getWidth()-i,getHeight());
g.drawLine(0,getHeight()-i,getWidth()-i,0);
g.drawLine(0+i,getHeight(),getWidth(),0+i);
}
}
}
You're overriding paint(), but calling super.paintComponent(g). You should override paintComponent(g).
Edit: I couldn't follow your check logic. This worked for me.
public class Test {
private class WektPanel extends JPanel {
boolean clear;
public WektPanel() {
setBackground(Color.BLUE);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawLines(3, g);
}
public void clear() {
clear = true;
repaint();
}
public void draw() {
clear = false;
repaint();
}
private void drawLines(int stroke, Graphics g) {
if (!clear) {
g.drawLine(0, 0, getWidth(), getHeight());
g.drawLine(0, getHeight(), getWidth(), 0);
for (int i = 1; i < stroke; i++) {
g.drawLine(0 + i, 0, getWidth(), getHeight() - i);
g.drawLine(0, 0 + i, getWidth() - i, getHeight());
g.drawLine(0, getHeight() - i, getWidth() - i, 0);
g.drawLine(0 + i, getHeight(), getWidth(), 0 + i);
}
}
}
}
private void showGUI() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final WektPanel wektPanel = new WektPanel();
f.add(wektPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.add(new JButton(new AbstractAction("Clear") {
#Override
public void actionPerformed(ActionEvent e) {
wektPanel.clear();
}
}));
buttonPanel.add(new JButton(new AbstractAction("Draw") {
#Override
public void actionPerformed(ActionEvent e) {
wektPanel.draw();
}
}));
f.add(buttonPanel, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Test().showGUI();
}
});
}
}
The first line in the paint method should be something like:
g.clearRect(0, 0, getWidth(), getHeight());
This way you make sure that the previous paints will not be there when you start painting again.
Related
I am having a problem about JScrollBar.
In my application I have 2 panels (one fixed, and one that changes depending on what the user chooses in the menu)
In a given panel I have a JScrollPane with a table. Since the standard java scrollbar was outside the theme of the application, I decided to try to create another one.
I basically create a JScrollPane and change its scrollBar to a custom one I created (scrollPane.setVerticalScrollBar (new CustomScrollBar ()) ;.
The first time I present the panel, the scrollbar is perfect. However, when I change to another panel and go back to it, the scrollbar model has reset.
What could this be/how do I solve it?
Creation JScrollPane code:
JScrollPane tablePanel = new JScrollPane(table);
tablePanel.setBounds(10, 148, 495, 200);
tablePanel.setBorder(new EmptyBorder(0, 0, 0, 0));
tablePanel.setBackground(Color.WHITE);
tablePanel.setVerticalScrollBar(new CustomScrollBar());
add(tablePanel);
CustomScrollBar Code:
public class CustomScrollBar extends JScrollBar {
public CustomScrollBar() {
setOpaque(false);
setUI(new BasicScrollBarUI() {
private final Dimension d = new Dimension();
#Override
protected JButton createDecreaseButton(int orientation) {
return new JButton() {
#Override
public Dimension getPreferredSize() {
return d;
}
};
}
#Override
protected JButton createIncreaseButton(int orientation) {
return new JButton() {
#Override
public Dimension getPreferredSize() {
return d;
}
};
}
#Override
protected void paintTrack(Graphics g, JComponent c, Rectangle r) {
}
#Override
protected void paintThumb(Graphics g, JComponent c, Rectangle r) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Color color = null;
JScrollBar sb = (JScrollBar) c;
if (!sb.isEnabled() || r.width > r.height) {
return;
} else if (isDragging) {
color = Colors.superdarkPurple;
} else if (isThumbRollover()) {
color = Colors.lightPurple;
} else {
color = Colors.darkPurple;
}
g2.setPaint(color);
g2.fillRoundRect(r.x, r.y, r.width, r.height, 10, 10);
g2.setPaint(Color.WHITE);
g2.drawRoundRect(r.x, r.y, r.width, r.height, 10, 10);
g2.dispose();
}
#Override
protected void setThumbBounds(int x, int y, int width, int height) {
super.setThumbBounds(x, y, width, height);
scrollbar.repaint();
}
});
}
public JScrollBar geCustomScrollBar() {
return this;
}
EDIT 1
This goes to a panel to add questions
public void gotoAddQuestionsPanel(Question q) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
currentQuestionsPanel = "add";
updateFrameComponentTreeUI();
remove(subPanel);
if (q != null) {
addQuestionsPanel.setQuestion(q);
}
subPanel = addQuestionsPanel.getPanel();
subPanel.setBackground(Color.WHITE);
subPanel.setBounds(304, 0, 515, 415);
getContentPane().add(subPanel);
subPanel.setLayout(null);
}
});
}
And this goes back to what has the customized ScrollBar
public void gotoQueryQuestionsPanel() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
currentQuestionsPanel = "query";
updateFrameComponentTreeUI();
remove(subPanel);
subPanel = queryQuestionsPanel.getPanel();
subPanel.setBackground(Color.WHITE);
subPanel.setBounds(304, 0, 515, 415);
getContentPane().add(subPanel);
subPanel.setLayout(null);
}
});
}
public void updateFrameComponentTreeUI() {
SwingUtilities.updateComponentTreeUI(this);
}
I want to have a JComponent on my JFrame, that has a custom shape in form of a polygon. Now i want to add a background image with colors in the same shape and blank color in the rest.
Is there a way to achieve this?
I have this test class:
public class Test extends JButton {
private final Polygon shape;
private final int provinceId;
private ImageIcon img;
public Test(Polygon p, int x, int y, int w, int h, int id, ImageIcon img) {
this.shape = p;
this.provinceId = id;
this.img = img;
setSize(w, h);
setLocation(x, y);
setIcon(img);
setContentAreaFilled(false);
addMouseListener(animation());
}
private MouseListener animation() {
return new MouseListener() {
public void mouseEntered(MouseEvent e) {
System.out.println("in");
}
public void mouseExited(MouseEvent e) {
System.out.println("out");
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
};
}
protected void paintComponent(Graphics g) {
g.drawPolygon(this.shape);
}
protected void paintBorder(Graphics g) {
g.drawPolygon(this.shape);
}
public boolean contains(int x, int y) {
return this.shape.contains(x, y);
}
public boolean isOpaque() {
return false;
}
public int getId() {
return this.provinceId;
}
public static void main(String[] args) {
JFrame f = new JFrame();
//Polygon p = new Polygon(new int[] {0, 400, 400, 0}, new int[] {0, 0, 300, 300}, 4);
Polygon p = new Polygon(new int[] {50, 150, 250, 350, 200, 50}, new int[] {0, 0, 50, 200, 300, 200}, 6);
ImageIcon ico = new ImageIcon("gfx/test.png");
Test t = new Test(p, 20, 20, 400, 300, 101, ico);
f.getContentPane().add(t);
f.setSize(500, 400);
f.setLayout(null);
f.setVisible(true);
}
}
but i only get this output: output
My original picture was: wanted output
You can do so by overriding the contains(Point p) method to only return true when p is in the bounds of your custom shape.
Then you best override isOpaque() to return false.
And finally you override paintComponent(Graphics g) to paint your component in whatever way you like (e.g. a background image with colors in the shape and blank color in the rest).
Sample code:
JPanel panel = new JPanel()
{
#Override
public boolean isOpaque()
{
return false;
}
#Override
public boolean contains(Point p)
{
// Use something that fits your shape here.
return p.getX() % 2 == 0;
}
#Override
public void paintComponent(Graphics g)
{
// do some painting
}
};
I want to draw a oval image in a JLabel, using Graphics. This is my code, but a dont know about Graphics.
class imagePanel extends JLabel {
//private PlanarImage image;
private BufferedImage buffImage = null;
private void drawFingerImage(int nWidth, int nHeight, byte[] buff) {
buffImage = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_BYTE_GRAY);
buffImage.getRaster().setDataElements(0, 0, nWidth, nHeight, buff);
Graphics g = buffImage.createGraphics();
g.drawImage(buffImage, 0, 0, 140, 150, null);
g.dispose();
repaint();
}
public void paintComponent(Graphics g) {
g.drawImage(buffImage, 0, 0, this);
}
}
I have this
you need the help of setClip() method as mentioned here and here.
when it comes to code it should look like this
public class OvalImageLabel extends JLabel {
private Image image;
public OvalImageLabel(URL imageUrl) throws IOException {
image = ImageIO.read(imageUrl);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setClip(new java.awt.geom.Ellipse2D.Float(0f,0f, getWidth(),getHeight()/2));
g.drawImage(image, 0, 0, this);
}
}
and a running application that using this class
public class UsageExample extends JPanel {
public UsageExample() {
super(new BorderLayout());
OvalImageLabel l;
try {
l = new OvalImageLabel(new File("/path/to/image.png").toURI().toURL());
} catch (Exception e) {
e.printStackTrace();
return;
}
add(l, BorderLayout.CENTER);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setContentPane(new UsageExample());
frame.setSize(200, 200);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
I have a set of JLabels, each containing a letter (via seText()), opaque and background set to white, on a JPanel with a GridLayout so the labels are forming a table.
I am doing a simple animation of highlighting certain rows and columns then there intersection. I can use the setBackground() of labels for this purpose, but thought I'd have more "choices" if a was able to use a Graphics object (maybe drawing a circle around intersection, then clearing it).
I tried to extend JLabel, or drawing on the JPanel directly(using getGraphics() in a method) but it didn't work, I think the drawing is behind the labels in this case. I can't figure out where should the "painting" code be placed in either case, nothing appeared on the screen.
in short, a method like the following, can be used to draw on top of labels?
should it be a JLabel or a JPanel method?
public void drawsomething() {
Graphics2D g2d = (Graphics2D) getGraphics();
g2d.fillRect(100, 100, 100, 100);
}
What if you override paintChildren() ?
protected void paintChildren(Graphics g) {
super.paintChildren(g);
//paint your lines here
}
You might want to try a JLayeredPane to paint your specific drawings on top of the existing JComponents
see example here http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html
I really don't know much about drawing stuff yet, but just created one small sample code for you to look at, hopefully you can get some information out of it. In order to paint on the JLabel you can use it's paintComponent(Graphics g) method.
A Sample Code :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawingOnJLabel extends JFrame
{
private CustomLabel label;
private int flag = 1;
private JPanel contentPane;
public DrawingOnJLabel()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
label = new CustomLabel(200, 200);
label.setLabelText("A");
label.setValues(50, 50, 100, 100, 240, 60);
final JButton button = new JButton("CLEAR");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
if (flag == 1)
{
label.setFlag(flag);
flag = 0;
button.setText("REPAINT");
contentPane.revalidate();
contentPane.repaint();
}
else if (flag == 0)
{
label.setFlag(flag);
flag = 1;
button.setText("CLEAR");
contentPane.revalidate();
contentPane.repaint();
}
}
});
}
});
contentPane.add(label);
add(contentPane, BorderLayout.CENTER);
add(button, BorderLayout.PAGE_END);
setSize(300, 300);
setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new DrawingOnJLabel();
}
});
}
}
class CustomLabel extends JLabel
{
private int sizeX;
private int sizeY;
private int x, y, width, height, startAngle, arcAngle;
private int flag = 0;
private String text;
public CustomLabel(int sX, int sY)
{
sizeX = sX;
sizeY = sY;
}
// Simply call this or any set method to paint on JLabel.
public void setValues(int x, int y, int width, int height, int startAngle, int arcAngle)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.startAngle = startAngle;
this.arcAngle = arcAngle;
repaint();
}
public void setFlag(int value)
{
flag = value;
repaint();
}
public Dimension getPreferredSize()
{
return (new Dimension(sizeX, sizeY));
}
public void setLabelText(String text)
{
super.setText(text);
this.text = text;
flag = 0;
repaint();
}
public void paintComponent(Graphics g)
{
if (flag == 0)
{
g.setColor(Color.RED);
g.drawString(text, 20, 20);
g.setColor(Color.BLUE);
g.drawOval(x, y, width, height);
g.fillOval(x + 20, y + 20, 15, 15);
g.fillOval(x + 65, y + 20, 15, 15);
g.fillRect(x + 40, y + 40, 5, 20);
g.drawArc(x + 20, y + 30, 55, 55, startAngle, arcAngle);
}
else if (flag == 1)
{
g.clearRect(x, y, width, height);
}
}
}
Use paintComponent(Graphics g) instead of paint(Graphics g). That will paint over the GUI
This is my code:
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel1 = new JPanel(){
#Override
protected void paintComponent(Graphics g) {
int w = getWidth();
int h = getHeight();
for (int i = 0; i < w; i+=100) {
g.drawLine(i, 0, i, h);
}
}
};
panel1.addMouseListener(new MouseListener() {
#Override
public void mouseReleased(MouseEvent arg0) {
}
#Override
public void mousePressed(MouseEvent arg0) {
}
#Override
public void mouseExited(MouseEvent arg0) {
}
#Override
public void mouseEntered(MouseEvent arg0) {
}
#Override
public void mouseClicked(MouseEvent arg0) {
int x = arg0.getX();
int y = arg0.getY();
Graphics g = (Graphics) panel1.getGraphics();
g.setColor(Color.black);
g.fillOval(x, y, 100, 100);
}
});
frame.add(panel1);
frame.setVisible(true);
What it does is when i click on frame a circle is drawn. after drawing some circles when i maximize or minimize or resize my frame the circles gets disappeared. how can I solve it?
You should override paintComponent() method of the panel. The circles should be added in a list. In the paintComponent() call super and then iterate through the list painting each circle.